Access value of dynamically created control(thru JScript) in .net page


 Using  JS code I have dynamically created some HTML controls to page. I am not able to set runat=server for them. As a result am not able to access their value. I assume because this property is not set, am not getting the values.

 If anyone can help me with the problem, pls post their answers. I need it urgently.



12/27/2008 9:37:58 AM 24353 articles. 2 followers. Follow

2 Replies

Similar Articles

[PageSpeed] 57

 runat=server is only for conrols that are rendered by the serverside code and not by the client. runat=server means nothing to the client.

If you want to reference the values, you need to use Request.Form like you would have done in classic ASP, php, etc.

Eric | Twitter epascarello | LinkedIn
12/27/2008 1:06:09 PM

As Eric posted, you need to use the Request object server-side. You also need to set and use the name property of the control and that should be a unique ID.

aspx file:

<form id="Form1" method="post" runat="server">
 <asp:button id="postBackButton" runat="server" text="Post Back"></asp:button>
 <input id="htmlButton" onclick="onButtonClick();" type="button" value="Add TextBox">
 <div id="contentDiv"></div>
 <input id="hiddenControl" type="hidden" name="hiddenControl" runat="server">

<script type="text/javascript">
function onButtonClick()
 var controlRef = document.createElement('input');
 controlRef.type = 'text'; = 'dynamicTextBox'; = 'dynamicTextBox';
 controlRef.size = 20;

 document.getElementById('<%= hiddenControl.ClientID %>').value = 'Y';

window.onload = function ()
 if ( document.getElementById('<%= hiddenControl.ClientID %>').value == 'Y' )
// -->

aspx.cs file:

private void Page_Load(object sender, System.EventArgs e)
 Response.Write("dynamicTextBox: " + this.Request["dynamicTextBox"]);

Also, you will need to persist the element somehow (here I simply did it with a hidden HTML element) and re-create it when the page re-loads, or the element will disappear on a PostBack.


12/27/2008 1:44:43 PM