Usman
Usman

Reputation: 163

Getting values of html form elements in Asp.net added dynamically via Jquery

Hello there i have written some code in jquery to add new input type elements in aspx page! Now I want to fetch values of these elements via ASP.NET! I know if i want to achieve this i will have to store each values in hidden form elements n then fetching hidden element val in cs file! I am curious if i could get a direct/shortcut way to fetch the values of each dynamically added contols in Asp.Net incase there were dozens of elements which were added dynamically in jquery!

Thanks in advance

Upvotes: 0

Views: 1465

Answers (2)

Jeremy Elbourn
Jeremy Elbourn

Reputation: 2690

When you add inputs on the client-side, the server doesn't have an object created to access its POST data like it does with your runat="server" controls. There are a few options:

1) Use a script to set the value of a runat="server" HiddenField before postback.

2) Access Request.Form["YourInputName"].

Upvotes: 1

Troy Barlow
Troy Barlow

Reputation: 313

Give every element you add to your page a class, say "dynamic". Before postback, update a HiddenField like so:

var hiddenValues = "";
$(".dynamic").each(function(){
   hiddenValues += $(this).val() + ",";
});

$("#hiddenField").val(hiddenValues);

Then access the comma delimited hidden field value in the code behind.

Upvotes: 0

Related Questions