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

Hi

 Using  JS code I have dynamically created some HTML controls to  asp.net 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.

 

 

0
Romita
12/27/2008 9:37:58 AM
asp.net.client-side 24353 articles. 2 followers. Follow

2 Replies
697 Views

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


Pascarello.com | Twitter epascarello | LinkedIn
0
A1ien51
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">
</form>

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

 document.getElementById('contentDiv').appendChild(controlRef);
 document.getElementById('<%= hiddenControl.ClientID %>').value = 'Y';
}

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

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.

NC...

0
NC01
12/27/2008 1:44:43 PM
Reply: