javascript created control values are not posting to server

[亡魂溺海] 提交于 2019-12-11 08:49:07

问题


In my Html page I create a few textboxes in a loop with javascript.

code snippet from inside loop:

var row = myTable.tBodies[0].insertRow(rowCount);
var cell1 = row.insertCell(0);
cell1.style.width = '40%';
var element1 = document.createElement("input");
element1.type = "text";
element1.className = 'textSub';
element1.id = 'txtOndNr' + rowNr;
cell1.appendChild(element1); 

When I press the submit button I can't seem to acces their values server side. I am aware that they are not server controls etc. But I hoped to acces them through request.form("id"). Also when I check firebug, these values are not in the post values (the controls are located inside the form tags, that's not the issue).

Does anyone have an idea of what I'm doing wrong?

Extra info: ASP.NET 1.1, script is located in ASCX


回答1:


Asp.net keeps the states of control in view state for only which are created by asp.net. You need to use hidden field to store the state of newly created controls and later access that hidden field on server for the state/data.

In html

<input type="hidden" runat="server" id="hdnFornewlyCreated" />

In javascript

document.getElementById('<%= hdnFornewlyCreated.ClientID %>').value = "stringcontainState";

In code behind

string state = hdnFornewlyCreated.Value; // Use this to extract state and data



回答2:


I just found out that it's not the ID that is used to send form values but the NAME. If i change element1.id = 'txtOndNr' + rowNr; to element1.name = 'txtOndNr' + rowNr; I can acces these values with Request.form("txtOndNr0").

Looks like this when checking firebug http post request

txtOndNr0   qsdf
txtOndNr1   
txtOndNr2   qsdf

To me this looks like a slightly cleaner solution.



来源:https://stackoverflow.com/questions/13561473/javascript-created-control-values-are-not-posting-to-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!