How to load a page with its default values after a postback

旧城冷巷雨未停 提交于 2019-12-11 07:43:41

问题


I'm creating user controls that i will put into an update panel and make them visible only when required using triggers. Once visible it will float in a dialog box so it has a close button which will just hide the control on client side.

The controls have multiple post back states, like a wizard, i'm using a multi view control to accomplish that. My problem is that once the user is at step number two in the control, if the user closes the dialog, than opens the dialog again, (note that the control is made visible on the server and reloaded by updating the updatepanel) the second step will still be displayed. The reason is . because whenever there is a postback the control will load its state using the viewstate, even if EnableViewState is false it will still load it using the LoadControlState method. so my quesion is how can i force a user control to load fresh with its default values without any postback data.


回答1:


We had a similar circumstance and the only way we found to reset our control was to do that in the code behind of the page. Not in an ajax call but on submit of the page because then we could set the properties and have them go into viewstate.

i'm pretty sure that that will break your design though.

Have you considered writing the page as RESTful? At least then you can ditch viewstate and read and write to a meaningful data store.

ViewState is probably the number one reason I went over to MVC and now avoid WebForms like the plague.




回答2:


If its a wizard that uses a dialog and each step is required, dont have a close button. If the user closes it you could refresh the whole page so the user has to start again?

I had so many issue like this with WebForms, where I was only using the UpdatePanel for ajax as it "looks" like an easy option. MVC sounds like a bit of a learning curve and it is, however you can acheive things by building pages with MVC and jQuery without MS ajax and the hassle of all the events in a page that constantly fighting with each other. Its difficult to make this step without knowing MVC and geetting your hand dirty, but its worth it.




回答3:


Its is possible.

I found a secret method in the control class, called ClearChildState(), this is a protected method and will clear the viewstate and the controlstate for all childcontrols.

So in my example, i created a class that inherits from panel

namespace MyControls
{
  public class Panel:System.Web.UI.WebControls.Panel
    {
        public void Reset()
        {
            ClearChildState();
        }
    }
}

In my page initialize event i check for request[flag] to reset the control

public partial class Test : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        if (!string.IsNullOrEmpty(Request["Reset"]) && Request["Reset"] == "1")
        {
            pnlCreateAccountDialog.Reset();               
        }
    }
}

OnClient side i have a Reset() function that i can call whenever i want the next postback to load a clean control

  <script type="text/javascript">
        function AddReset() {
            $('#Reset').val('1');
        }
        function RemoveReset() {
            $('#Reset').val('');
        }
    </script>


来源:https://stackoverflow.com/questions/2831148/how-to-load-a-page-with-its-default-values-after-a-postback

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