Javascript controls

A

ayende

I got the following JS function:

<script language="javascript" type="text/javascript">
function addUser()
{
var users = document.getElementById('users');
var hidden = document.createElement('input');
hidden.value = 'foo';
users.appendChild(hidden);
var i =0;
for(var node = users.firstChild;node !=null; node =
node.nextSibling)
{
if(node.nodeType != 1) //input
continue;
node.id = 'project.Users['+i+'].Id';
i++;
}
}
</script>

And the following form:
<form id="form1" runat="server">
<div id="users">
<input id="Button1" type="button" value="Add User"
onclick="javascript:addUser();" />
</div>
<asp:TextBox ID="PostBackValues" runat="server" Height="225px"
TextMode="MultiLine" Width="596px"></asp:TextBox><br />
<asp:Button ID="submit" runat="server" Text="submit"
OnClick="submit_Click" /><br />
</form>


I open the page, click on the Add User button a couple of times, and
then submit.
In my code behind file I have:
protected void submit_Click(object sender, EventArgs e)
{
foreach (string key in Request.Params.AllKeys)
{
PostBackValues.Text += string.Format(" {0}: {1}\r\n", key,
Request.Params[key]);
}
}

But I can't see the values I set using JS. In fact, when checking with
Fiddler, I can see that they aren't added to the POST varaibles at all.
Any ideas what this is?
 
I

intrader

I got the following JS function:

<script language="javascript" type="text/javascript">
function addUser()
{
var users = document.getElementById('users');
var hidden = document.createElement('input');
hidden.value = 'foo';
users.appendChild(hidden);
var i =0;
for(var node = users.firstChild;node !=null; node =
node.nextSibling)
{
if(node.nodeType != 1) //input
continue;
node.id = 'project.Users['+i+'].Id';
i++;
}
}
</script>

And the following form:
<form id="form1" runat="server">
<div id="users">
<input id="Button1" type="button" value="Add User"
onclick="javascript:addUser();" />
</div>
<asp:TextBox ID="PostBackValues" runat="server" Height="225px"
TextMode="MultiLine" Width="596px"></asp:TextBox><br />
<asp:Button ID="submit" runat="server" Text="submit"
OnClick="submit_Click" /><br />
</form>


I open the page, click on the Add User button a couple of times, and
then submit.
In my code behind file I have:
protected void submit_Click(object sender, EventArgs e)
{
foreach (string key in Request.Params.AllKeys)
{
PostBackValues.Text += string.Format(" {0}: {1}\r\n", key,
Request.Params[key]);
}
}

But I can't see the values I set using JS. In fact, when checking with
Fiddler, I can see that they aren't added to the POST varaibles at all.
Any ideas what this is?

The javascript is not creating the input elements properly or perhaps not
adding them to the DOM. I notice that they don' have a name or id!
Otherwise, you should see the values in the Params object. If the users
added all have the same name, you will see them as a comma-list of values.
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top