简体   繁体   English

单击按钮后保留asp.net控件的UI状态

[英]preserve UI state of asp.net controls after button click

I have an ASP.NET web app with a dropdown list and a number of ASP.NET textboxes/labels. 我有一个带有下拉列表和许多ASP.NET文本框/标签的ASP.NET Web应用程序。 when the user chooses a particular item from the dropdownlist the labels change text and some of them are hidden etc using jquery. 当用户从下拉列表中选择特定项目时,标签会更改文本,其中一些标签会使用jquery隐藏等。 However after clicking on a button the controls are being changed back as when the page is first loaded. 但是,单击按钮后,控件将像第一次加载页面时一样变回原来的状态。

Please suggest 请建议

  1. How i Can preserve the new labels name etc? 我如何保留新标签名称等?
  2. Should i put the same jquery code i wrote for the dropdownlist on change event also in the on document load event? 我是否应该在文档加载事件中也将与为dropdownlist编写的相同的jQuery代码放到change事件中?
  3. Should i use hidden fields to store the value? 我应该使用隐藏字段存储值吗?

1: How i Can preserve the new labels name etc? 1:如何保存新标签名称等?

Yes, 是,

You can use hidden field to store. 您可以使用隐藏字段进行存储。

2: Should i put the same jquery code i wrote for the dropdownlist on change event also in the on document load event? 2:我是否应该在文档加载事件中也将与为dropdownlist编写的相同的jQuery代码放在change事件中?

No, 没有,

On page_load access hidden field, extract and use values. 在page_load访问隐藏字段上,提取并使用值。

3: Should i use hidden fields to store the value? 3:我应该使用隐藏字段来存储值吗?

Yes, 是,

You can use hidden field to store changes you made in GUI control using jQuery. 您可以使用隐藏字段来存储使用jQuery在GUI控件中所做的更改。 Make that hidden field server accessible by making it runat server. 通过使其成为运行服务器,使该隐藏域服务器可访问。 On server end when you get postback access the hidden field, extract and assign values to respective controls. 在服务器端,当您获得回发访问时,隐藏字段将提取并分配值给相应的控件。 You can see how it would work in this post . 你可以看到它是如何在这方面的工作职位

As you might be knowing , after clicking the button if it is submit type it will do the postback to the server , as HTTP is stateless , it is your responsbility to generate the label text and value again. 您可能知道,单击按钮后如果它是Submit类型,它将回发到服务器,因为HTTP是无状态的,您有责任再次生成标签文本和值。 Generally ASP.NET viewstate take care of doing so for input texts, but for labels you have to do by yourself. 通常,ASP.NET viewstate会针对输入文本执行此操作,但是对于标签,您必须自己执行。 I can suggest the solution like this 我可以建议这样的解决方案

1) Have a javascript method which will have the logic to change the value of label and hidden field based on the selected value of the dropdown 1)有一个javascript方法,该方法将具有根据下拉列表的选定值更改label和hidden字段的值的逻辑

     function MyFunction ( ddlSelectedValue)
     {
      if(ddlSelectedValue == "1")
       {
           $("#LABELID").html('YOUR VALUE');
           // rest other logics
       }
     }

2) ASP.NET viewstate will help you to preserve the dropdown state after button click . 2)ASP.NET viewstate将帮助您在单击按钮后保留下拉状态。 So on add this jquery script on the page top 因此,请在页面顶部添加此jquery脚本

      $(document).ready(function(){
       var ddlSelectedValue = $("#DDLID").val();
       MyFunction(ddlSelectedValue);

      });

3) attach the the point 1 method to onchange of the dropdown also. 3)也将点1方法附加到下拉列表的onchange。

4) Using the hidden field is Ok , because hidden fields will serve your requirement in using in the server side as they will also be posted back. 4)使用隐藏字段是可以的,因为隐藏字段将满足您在服务器端使用的要求,因为它们也会回发。

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

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