开发者

How to get value set in javascript from code-behind?

开发者 https://www.devze.com 2023-03-18 14:30 出处:网络
I set value in a hidden field like this: ((TextBox)ctrl).Attributes.Add(\"onchange\", \"document.getElementById(\'\" +

I set value in a hidden field like this:

((TextBox)ctrl).Attributes.Add("onchange", "document.getElementById('" + 
  ((BasePage)Page).GetControl(Page, "ChangedRowsIndicesHiddenField").ClientID + 
  "').value.concat('" + row.RowIndex + ",');");

In page source it looks like this:

onchange="document.getElementById('ctl00_CPHDefault_tcTPS_TPProd_ctl01_tcProduction_TPNewTitlesStatus_ChangedRowsIndicesHiddenField').value.concat('0,');" 

I want to be able to retrieve and use this value from code-behind on postback (button click):

string Chan开发者_开发百科gedRowsIndices = ChangedRowsIndicesHiddenField.Value.TrimEnd(',');

But because of some reason, ChangedRowsIndices is always empty. Could you please help me with this? What am I doing wrong? Here is the hidden field:

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

Thanks.


If not use the runat=server place a name here

    <input id="ChangedRowsIndicesHiddenField" type="hidden"    
name="ChangedRowsIndicesHiddenField_ID" />

and read the data on post back like

HttpContext.Current.Request.Form["ChangedRowsIndicesHiddenField_ID"]

If you left it with the runat="server" get it with the UniqueID

HttpContext.Current.Request.Form[ChangedRowsIndicesHiddenField.UniqueID]

One more

You have a bug on value, to set the value use document.getElementById('theid').value = 'new value'; get a render like

document.getElementById('ctl00_CPHDefault_tcTPS_TPProd_ctl01_tcProduction_TPNewT‌​itlesStatus_ChangedRowsIndicesHiddenField').value = 'new value'

Now you type it as jQuery way.

0

精彩评论

暂无评论...
验证码 换一张
取 消