How to viewstate in normal HTML input in asp.net

亡梦爱人 提交于 2019-12-08 07:24:34

问题


How to use VIEWSTATE in normal HTML input elements in ASP.net. I am having a form with lot of input elements and i had normal(not asp.net input elements) input elements. Now i should i maintain viewstate for these elements.

Thank You


回答1:


Try writing javascript function, that store input values in asp:HiddenField before postback And restore values from it after postback.

Example:

function post(){
   var viewState = ...; // save input values
   $('#<%= Hidden1.ClientID %>').val(viewState);
   __doPostBack("<%=UniqueID%>");
}

$(function(){
    var viewState = $('#<%= Hidden1.ClientID %>').val();
    //restore input values
});



回答2:


There is also a property of "enableviewstate" in normal html elements, just set it to true.




回答3:


There is not reason to do that because after the post back the input elements post the value again to the page, and have the content again after the post back

The only reason to do that is to try to get the previous values, before the post back.




回答4:


It's been awhile since this post was made, but here's my solution which worked for me.

Instead of hidden fields, just set the runat="server" attribute for your html control in ASP.Net. E.g.

<input type="text" runat="server"/>

This will do two things:

  • Make the html control accessible in the ASP.NET code behind.
  • Automatically maintain viewstate unless you explicitly set the EnableViewState to be false, allowing for persistence between page postbacks.


来源:https://stackoverflow.com/questions/6436674/how-to-viewstate-in-normal-html-input-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!