<asp.隐藏字段>即使在javascript中设置了值,codeehind中的值也会变为空

<asp.HiddenField> value coming as empty in codebehind even after value is set in javascript

本文关键字:codeehind lt 设置 gt 字段 隐藏 asp javascript      更新时间:2023-09-26

这是我的问题。我有一个隐藏字段,通过javascript方法更改其值。每当按下ctrl键在网格视图中选择一行时,就会设置隐藏字段的值。

隐藏字段的值就是这样分配的(Javascript代码):

var curKey;
function checkKey()
{
    //Check if contrl key is pressed or not.
    if (curKey == 17)
    {                
         document.getElementById('<%= CtrlKeyPressed.ClientID %>').value = 'Y';
    }
}

在代码中,我在gridview_rowdatabound函数中调用了这个javasxcript,如下所示:

 gridview.Rows[i].Attributes.Add("onclick",ClientScript.GetPostBackEventReference(grvKanban, "Select$" + i) + ";checkKey()");

在.aspx页面的正文中,代码如下(如果在单击gridview中的任何一行时按下控制键,curKey的值将为17):

<body onkeyup = "curKey = null;" onkeydown = "curKey=event.keyCode;">

对于隐藏字段,代码为:

<asp:HiddenField ID="CtrlKeyPressed" runat="server"/>

基本上,我想知道在选择网格视图行时是否按下了Ctrl键,我想用Hidden字段的值来查找它。但是在后面的代码中,当我试图获取CtrlKeyPressed的值时,value是空字符串。

有人能帮忙解决这个问题吗?

谢谢!!!

您需要在返回post之前运行checkKey()。所以改变这个:

gridview.Rows[i].Attributes.Add("onclick",
    ClientScript.GetPostBackEventReference(grvKanban, "Select$" + i) +
    ";checkKey()");

到此:

gridview.Rows[i].Attributes.Add("onclick",
    "checkKey();" +
    ClientScript.GetPostBackEventReference(grvKanban, "Select$" + i));