Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a two buttons, on button1 click i m creating multiple html textbox so i need to save the values of dynamically created textbox to database by cliking on button2. so i just want to know how to access the dynamicallly created textbox value in aspx.cs file.

What I have tried:

JavaScript
here i am creating dynamic textbox using jQuery -

function CreateTextbox(){
var $ctrl = $('<input id="txtExtPersons1" name="txtExtPersons1" class="form-fieldContractEntry"').attr({ type: 'text', name: 'text' });
 $("#holder").append($ctrl);
}

C#
here i am trying to access the value of dynamically created textbox.

protected void btnSave_Click(object sender, EventArgs e)
{
       if (!string.IsNullOrEmpty(Request.Form["txtExtPersons1"]))
   {
               string a = Request.Form["txtExtPersons1"];
   }
}
Posted
Updated 9-Dec-16 0:16am

You access it using Request.Form as you currently are, the issue might be the fact that you're giving the component a "name" in the element and then attaching a different "name" via attr. Try removing one of the name allocations

JavaScript
function CreateTextbox(){
var $ctrl = $('<input id="txtExtPersons1" name="txtExtPersons1" class="form-fieldContractEntry"').attr({ type: 'text' });
 $("#holder").append($ctrl);
}
 
Share this answer
 
Only controls that have the runat="server" are accessible on code behind and this cannot be created on client-side. However, there is a work around.
If you want to pass some value from a client-side script-created control to code behind on some button click, pass this value to an asp:HiddenField through a client-side script via onClientClick event first. The code behind will then be able to receive this value through the asp:HiddenField control.
Check this out: creating Control using Javascript | The ASP.NET Forums[^]
 
Share this answer
 
v2
Comments
Dnyaneshwar Sable 10-Dec-16 4:07am    
Thnks Peter... I already done it after posting this question.. its working..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900