简体   繁体   English

使用Javascript更改HiddenField值不会改变C#代码隐藏

[英]Changing HiddenField value with Javascript not changing in C# codebehind

I'm trying to change a hidden field with javascript and then use that changed value in my codebehind. 我正在尝试使用javascript更改隐藏字段,然后在我的代码隐藏中使用该更改的值。 I have a breakpoint in Page_Load to check if the value of HiddenField1 has changed but it always remains 0 on postback. 我在Page_Load中有一个断点来检查HiddenField1的值是否已经改变但是在回发时它总是保持为0。

    <script type="text/javascript">
        $(document).ready(function () {
        var hiddenControl = '<%= HiddenField1.ClientID %>';
        var s = $('#cbox');

        $("#cbox").combobox({
            selected: function (event, ui) {
                alert(s.val());
                document.getElementById(hiddenControl).value = s.val();
                alert(document.getElementById(hiddenControl).value);
            }
        });
   });

   <asp:HiddenField ID="HiddenField1" runat="server" EnableViewState="False" Value="0" />

If I can't get this to work is there any other method to pass information between javascript and c# codebehind ? 如果我不能让它工作是否有任何其他方法来传递javascript和c#codebehind之间的信息?

You may want to note the client side ClientID of you HiddenField and then look at the corresponding value in the Request.Form collection server side during postback. 您可能需要记下HiddenField的客户端ClientID,然后在回发期间查看Request.Form集合服务器端的相应值。

This way you will check the value sent to the server. 这样您将检查发送到服务器的值。 If the value is correct, the problem might occur because something disturbs ProcessPostData (manual resetting or dynamic change of form organization for example) 如果值正确,可能会出现问题,因为某些事情扰乱了ProcessPostData(例如手动重置或表单组织的动态更改)

Though it is difficult to give an advice without the whole code, I agree with those saying that EnableViewState=false is surprising. 虽然很难在没有完整代码的情况下给出建议,但我同意那些说EnableViewState = false令人惊讶的人。

Although you can always use set get method to store the values from java script ... But here is some simple way to solve it... 虽然你总是可以使用set get方法来存储java脚本中的值...但是这里有一些简单的方法来解决它...

case 1 - If control is just for storing purpose , 案例1 - 如果控制仅用于存储目的,
put display : none in it's style and make visible = true in the attribute of the control. 将display:none放在它的样式中,并在控件的属性中使visible = true。

case 2 - if control is to be displayed but in disabled mode put ReadOnly="true" instead of 情况2 - 如果要显示控制但是在禁用模式下放入ReadOnly =“true”而不是

   eg..

    <asp:textbox runat="server" ID="textbox1" Enabled="false"></asp:textbox> will not work

    <asp:textbox runat="server" ID="textbox2" ReadOnly="true"></asp:textbox> will work

将您的HiddenField放在UpdatePanel中,并将HiddenField与ClientID一起使用。

My answer is similar to what others said to get a hidden variable/control that can be passed back to the Server Side Code Behind like Button Event or Page_Load, etc. I got the idea from them. 我的答案类似于其他人所说的获取隐藏变量/控件,可以将其传递回服务器端代码,如Button事件或Page_Load等。我从他们那里得到了这个想法。 But here it is: 但这里是:

<asp:TextBox ID="hdnErrorMsg" ClientIDMode="Static" style="display: none;" runat="server"/>

This can be set in JavaScript, and it will return to Server Side Asp.NET and be used and examined. 这可以在JavaScript中设置,它将返回到Server Side Asp.NET并被使用和检查。 If you want to preserve it when it goes back to the client, you may have to put it back and all other things that you need. 如果你想在它回到客户端时保留它,你可能不得不把它和你需要的所有其他东西放回去。 In my case it was an error message. 在我的情况下,这是一个错误消息。 I wanted it to stop in the ASP.NET button click event for the save and exit, but I needed to put the error stuff back so that when it went back to the client it would be there again. 我希望它停止在ASP.NET按钮单击事件中进行保存并退出,但是我需要将错误内容放回去,以便当它返回到客户端时它会再次存在。

Check out the example on the MS documentation for HiddenField: 查看HiddenField的MS文档中的示例:

Form1.HiddenField1.value = s.val();

Bottom of this page - http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hiddenfield.aspx 本页底部 - http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hiddenfield.aspx

try to do the same task using html hidden variable 尝试使用html隐藏变量执行相同的任务

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

As we have provided runat="server" we will be able to access this variable on server side and on hdName.Value we can get the value on server side. 由于我们提供了runat =“server”,我们将能够在服务器端和hdName.Value上访问此变量,我们可以在服务器端获取该值。 Thought it is html control we need to provide hdName.ClientID in javascript as we have given runat=server. 认为这是html控件我们需要在javascript中提供hdName.ClientID,因为我们已经给出了runat = server。

document.getElementById('<%= hdName.ClientID %>').value 

Hopefully this will solve your issue and give you desired result. 希望这将解决您的问题,并给您想要的结果。 Thanks 谢谢

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

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