0

I am trying to create a web part that will accept data from a user and simply display it on the screen. I have researched and researched and i cannot get the data to display.

HTML

<div class="form-el-holder">
    <label for="oldHostUrl" id="old-host-url-label">Old Host URL</label>
    <input runat="server" type="text" id="oldHostUrl" name="oldHostUrl" />
</div>
<div class="form-el-holder">
    <asp:Button runat="server" id="Button1" Text="Submit" OnClick="Submit" />
</div>

I do not have the form elements wrapped in a <form> tag because i received an error because there can only be 1 runat="server" form.

C#

protected void Submit(object sender, EventArgs e)
{
    //MesgBox("Hi"); This just lets me know if the data will display in a message box
    // I have tried 
    Textbox obj = (Textbox)sender;
    // This does not work as i get an error when trying to access the .Text method.
    //I have even tried just 
    oldHostUrl.Text;
    // I receive an error because it doesn't know what to do with the .Text method.
}

There must be a simple solution but i cannot find one. Thanks for any and all help here.

2 Answers 2

2

You can use an <asp:TextBox> control for oldHostURL if you want to access the .Text property.

input elements are represented as HTML Input Controls in server side code. The specific type depends on the type attribute.

When using type="text" these become HtmlInputText which doesn't have a Text property. Instead you'll need to use Value.

oldHostURL.Value

1
  • Thanks for clearing that up for me. I figured <asp:TextBox /> and <input type="text" /> would be similar.
    – IE5Master
    Commented Jul 18, 2016 at 14:00
0

You should create a Visual Webpart in Visual Studio. Once this done, you can drag and drop the text box and button from Toolbox.

Next double click the button which automatically creates an event handler. In the handler code you can write

OldHostURL.Text = TextBox.Text;
3
  • As i kind of mentioned in my post, i cannot use .Text. If i do, i get an error.
    – IE5Master
    Commented Jul 15, 2016 at 20:30
  • It seems you are using wrong project type? I have always got a toolbox and canvas in case of visual webpart. Which is your visual studio version? Commented Jul 15, 2016 at 20:32
  • Apologies, I do have a canvas. Even when i had the button and double click it and add your suggested code, i get an error.
    – IE5Master
    Commented Jul 18, 2016 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.