简体   繁体   English

将值分配给javascript中的隐藏字段

[英]assigning value to hidden field from javascript

I am trying to do what seems to be simple but am unable to accomplish 我正在尝试做看似简单但无法完成的事情

I am trying to set the value of a column after a row focus change in a grid to a hidden value in java script. 我试图将网格中的行焦点更改为Java脚本中的隐藏值后,将列的值设置为。

My imbedded javascript code: 我嵌入的javascript代码:

function OnGridFocusedRowChanged() {
   grdA.GetRowValues(grdA.GetFocusedRowIndex(), 'ClientID', OnGetRowValues);
}

function OnGetRowValues(values) {
  //Set hidden value
  document.getElementById('<%=hdnClientID.ClientID%>').value = values[0];

  //Fire button click
  btnPopulateGrids_Click();        
}

where hdnClientID is the name of my hidden field 其中hdnClientID是我的隐藏字段的名称

In GridA I have the setting as such that OnGridFocusedRowChanged gets executed each time a row focus change takes place. 在GridA中,我具有这样的设置:每次发生行焦点更改时,都会执行OnGridFocusedRowChanged

To this point, it works fine, the values[0] in OnGetRowValues() contains the correct value from the corresponding row in GridA. 至此,它工作正常, OnGetRowValues()中的values[0]包含来自GridA中相应行的正确值。

But in the corresponding code behind, I cannot access the value from hidden field hdnClientID . 但是在后面的相应代码中,我无法从隐藏字段hdnClientID访问该值。 Always comes up null when accessing 访问时始终为空

Current_Client_ID = CInt(hdnClientID.Value);

cannot access or convert any value from hdnClientID.ClientID. 无法访问或转换hdnClientID.ClientID中的任何值。

either. 无论是。

I'm missing something simple. 我缺少一些简单的东西。

I had to complete a similar task recently and I too was unable to access the value from codebehind. 我最近不得不完成类似的任务,而且我也无法从codebehind访问该值。 I researched and found out that it is not that simple and that you can't do it with javascript. 我研究发现,它不是那么简单,并且您无法使用javascript做到这一点。 What I would recommend is to check if you have jQuery installed and if you do, change your function to this: 我建议您检查是否已安装jQuery,如果已安装,请将函数更改为此:

function OnGetRowValues(values) {
  //Set hidden value
  $('#<%=hdnClientID.ClientID%>').val(values[0]);

  //Fire button click
  btnPopulateGrids_Click();

  //If you change your HiddenField to have OnValueChange event, you can trigger it with this
  //__doPostBack('<%= CompanyCode.ClientID %>', '');        
}

Also, I guess you are firing some function from code behind with btnPopulateGrids_Click(); 另外,我猜您正在使用btnPopulateGrids_Click();从代码后面触发一些功能btnPopulateGrids_Click(); . I would recommend adding OnValueChanged="hdnClientID_OnValueChanged"/> to your <asp:HiddenField/> and using my supplied function triggering method. 我建议将OnValueChanged="hdnClientID_OnValueChanged"/>到您的<asp:HiddenField/>并使用我提供的函数触发方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM