简体   繁体   English

通过JavaScript更新ASP.NET中的隐藏字段

[英]Updating a Hidden Field in ASP.NET via JavaScript

I inherited some JavaScript that I was told to integrate into our ASP.NET site. 我继承了一些JavaScript,并被告知要集成到我们的ASP.NET网站中。 I thought this would be straightforward but it's turning out to be a bit of a challenge. 我以为这很简单,但事实证明这是一个挑战。

The code looks something like this: 代码看起来像这样:

<SELECT id="Question1" name="Question" onchange="updateQuestion();">                 
<OPTION value="notChosen">--Please Select One--</OPTION>
<OPTION value="in">India</OPTION>
<OPTION value="de">Germany</OPTION>                     
<OPTION value="fr">France</OPTION>                
<OPTION value="us">United States</OPTION>                     
<OPTION value="ch">Switzerland</OPTION>                
</SELECT> 

The goal is to get the value from this HTML control into ASP.NET, however this control itself is being dynamically generated by another chunk of javascript, so I can't just change this to an asp.net control. 目标是从此HTML控件中获取值到ASP.NET,但是此控件本身是由另一部分JavaScript动态生成的,因此我不能仅将其更改为asp.net控件。 My solution was to add the onchange="updateQuestion();" 我的解决方案是添加onchange =“ updateQuestion();” method, this JS will take these SELECT tags and place the values into an ASP.NET control: 方法,此JS将采用以下SELECT标记并将其值放入ASP.NET控件中:

function updateSecQ() {
    var sQuestion = document.getElementById('<%=sQuestion.ClientID%>');
    sQuestion.Value = "";
    var questions = document.getElementsByName('Question');
    for (question in questions) {
        if (questions[question].value != null)
            sQuestion.Value += questions[question].value + ",";
    }
    alert(sQuestion.Value);
}

As you can see, that's looking to update an ASP.NET control: <asp:HiddenField ID="sQuestion" runat="server" value="" /> 如您所见,正在寻找更新ASP.NET控件的方法: <asp:HiddenField ID="sQuestion" runat="server" value="" />

This appears to all work, however when I goto the server side on the form submit I see that sQuestion.Value is still = "". 这似乎对所有工作都有效,但是当我在表单提交中进入服务器端时,我看到sQuestion.Value仍然=“”。

I'm not quite sure what I've done wrong here, any input would be much appreciated. 我不太确定自己在这里做错了什么,我们将不胜感激。 Thank you for your time. 感谢您的时间。

Dunno about ASP.net, but I know in the browser DOM, a hidden field's value is value all lower case, not Value. Dunno关于ASP.net,但我知道在浏览器DOM中,隐藏字段的值全部是小写值,而不是Value。 It's the value attribute that the browser will submit, and by setting sQuestion.Value, you've failed to set its submitted value. 这是浏览器将提交的value属性,通过设置sQuestion.Value,您无法设置其提交的值。

Is that JavaScript function on the same aspx page? 该JavaScript函数是否在同一aspx页面上? if not I believe that 如果没有,我相信

var sQuestion = document.getElementById('<%=sQuestion.ClientID%>');

will fail. 将失败。

If you use jQuery you can use: 如果您使用jQuery,则可以使用:

$("[id *= 'substringOfTheASPId']").val("new value")

You set sQuestion.Value instead of sQuestion.value. 您设置sQuestion.Value而不是sQuestion.value。

I found this in an answer to a similar question. 我在回答类似问题时发现了这一点。

I don't believe .NET expects that value of a HiddenField to be manipulated on the clientside, so it probably just reads the ViewState value back everytime. 我不相信.NET希望在客户端上操纵HiddenField的值,因此它可能每次都回读ViewState值。 You can use a regular hidden input and make it server control by adding the "runat" property like this: 您可以使用常规的隐藏输入,并通过添加“ runat”属性使其成为服务器控制,如下所示:

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

In the code behind, you can read the value in a manner similar to what you have above. 在后面的代码中,您可以按照与上面相同的方式读取值。 It will be of the type System.Web.UI.HtmlControls.HtmlInputHidden. 它将是System.Web.UI.HtmlControls.HtmlInputHidden类型。

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

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