Post-back a value from text field that dynamically created [ASP.net]

1.5k views Asked by At

In my webpage i got a button, next to button there is a input type='text' text box that i create dynamically (so i can't use the runat="server")

I wan't to post back what the user has entered in the input box to the server.

This is what i have done:

<asp:LinkButton id="xx" OnClick="xx" runat="server">

And another button

<button onclick="some_js_function()">

And the text box that dynamically generated

And Another

<aspx:HiddenField>

This is what happens: The aspx button is hidden, when the user clicks the other button the js functions is called and copies the value from the javascript text box the hidden field box and then clicks (jQuery .click() ) the aspx hidden button.

In the Google Chrome console i can see that the post-back is happening, and the value successfully copied but the server-side function that the onclick in the aspx button that is hidden is never called.

This probably not the best way to do this..

If someone got a better idea or a way to fix this i will be happy :)

1

There are 1 answers

1
Mivaweb On

Every element in your form tag, if its added manually or dynamically, will be posted back to the server.

Manually added inputs eg: <asp:TextBox runat="server" id="txtBox"></asp:TextBox> can be called directly into your backend using:

SET Text:

this.txtBox.Text = "Some Text";

GET Text:

string valueBox = this.txtBox.Text;

Dynamically added controls need to be assigned with both id and name attributes. Example:

<input type="text" id="txtBox" name="txtBox" />

Then in your backend you can get the value of the textbox by using Request.Form:

string valueBox = Request.Form["txtBox"].ToString();