How to get value on postback Dynamically Created controls by javascript

  • Thread starter Amit Maheshwari
  • Start date
A

Amit Maheshwari

I have my aspx page on which i am creating <input type=text> on client side
using javascript. Now when i submit my page i need to access these controls
to get the value entered by the user.
the code is like
<script language="javascript">
<!--
//to count no of file upload controls
var intRowCount = 2;

function AddApp()
{
var strInner='';
var objTBody = document.getElementById('AppBody');

//creating columns
var objCell1 = document.createElement("TD");
var objCell2 = document.createElement("TD");
var objCell3 = document.createElement("TD");

//creating row
var objCurRow = document.createElement("TR");

objCurRow.appendChild(objCell1);

strInner = "<input type='text' id='App" + intRowCount +"' >";
objCell2.innerHTML = strInner;
//appending columns in the row
objCurRow.appendChild(objCell2);

strInner = "<input type='text' id='Desc" + intRowCount +"' >";
objCell3.innerHTML = strInner;
//appending columns in the row
objCurRow.appendChild(objCell3);

objCurRow.setAttribute("id","tr"+intRowCount);

//appending row in the TBody
objTBody.appendChild(objCurRow)

intRowCount = parseInt(intRowCount, 10) + 1;
}
-->
</script>

<input type="button" id="AddAppl" value="Add Application"
onclick="AddApp();">
 
G

Guest

Amit,

You can use Request.Form["App1"] (C#) or Request.Form("App1") (VB.NET) to
access values of controls created on the client side by javascript.

You may also want to create a hidden text field and update value of that
field with number of textboxes created on the client side.

Then retrieve this (hidden text box) value first on the server side for
count of fields created. Then use Request.Form("") to retrieve values of
textboxes created.

Regards,

Anand.
 
A

Amit Maheshwari

Thanks for replying Anand, but i could not getting any object using
Request.Form["App1"] and Request.Param["App1"], the error i m getting is
"Object reference not set to an instance of an object. "
And i also looked 'Request.Form' in QuickWatch, there is no key of
App1,App2 which i had created.
It seems that form is not remembring on post back what i have created using
javascript, Is there any way to resolve it?

Anand Rajagopalan said:
Amit,

You can use Request.Form["App1"] (C#) or Request.Form("App1") (VB.NET) to
access values of controls created on the client side by javascript.

You may also want to create a hidden text field and update value of that
field with number of textboxes created on the client side.

Then retrieve this (hidden text box) value first on the server side for
count of fields created. Then use Request.Form("") to retrieve values of
textboxes created.

Regards,

Anand.

Amit Maheshwari said:
I have my aspx page on which i am creating <input type=text> on client side
using javascript. Now when i submit my page i need to access these controls
to get the value entered by the user.
the code is like
<script language="javascript">
<!--
//to count no of file upload controls
var intRowCount = 1;

function AddApp()
{
var strInner='';
var objTBody = document.getElementById('AppBody');

//creating columns
var objCell1 = document.createElement("TD");
var objCell2 = document.createElement("TD");
var objCell3 = document.createElement("TD");

//creating row
var objCurRow = document.createElement("TR");

objCurRow.appendChild(objCell1);

strInner = "<input type='text' id='App" + intRowCount +"' >";
objCell2.innerHTML = strInner;
//appending columns in the row
objCurRow.appendChild(objCell2);

strInner = "<input type='text' id='Desc" + intRowCount +"' >";
objCell3.innerHTML = strInner;
//appending columns in the row
objCurRow.appendChild(objCell3);

objCurRow.setAttribute("id","tr"+intRowCount);

//appending row in the TBody
objTBody.appendChild(objCurRow)

intRowCount = parseInt(intRowCount, 10) + 1;
}
-->
</script>

<input type="button" id="AddAppl" value="Add Application"
onclick="AddApp();">
 
K

Kalpesh

Hi Amit,

I think - the suggestion by Anand should work.
However, check for 2 things

1) form is using POST as against GET
2) javascript code to create TR/TD is appending it to Body instead of a
Table tag

HTH
Kalpesh
 
A

Amit Maheshwari

Kalpesh form is using method post
<form id="Form1" method="post" runat="server">
and row is appending to body
objTBody.appendChild(objCurRow)

My form looks like this
<HTML>
<HEAD>
|
|
|
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table>
<tr><td>
<input type="button" id="AddAppl" value="Add Application"
onclick="AddApp();">
</td></tr>
<tbody id="AppBody" runat="server">
</tbody>

</table>
</form>
</body>
</HTML>

and the Javasscript code is written in my question.

Is there somthing i am missing ?
 
K

Kalpesh

Hi Amit,

It seems that this cannot be achieved.
See this link - http://www.mcse.ms/archive109-2004-10-1186902.html

Also, it will be better to have a hidden field inside your asp.net page
statically available
On user submitting this page, fill the hidden field value with values
of controls that you created dynamically on client side

HTH
Kalpesh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top