如何在回发后加载具有默认值的页面 [英] How to load a page with its default values after a postback

查看:26
本文介绍了如何在回发后加载具有默认值的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建用户控件,我会将这些控件放入更新面板,并仅在需要时使用触发器使它们可见.一旦可见,它就会漂浮在一个对话框中,所以它有一个关闭按钮,它只会隐藏客户端的控件.

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.

控件有多个回发状态,就像向导一样,我正在使用多视图控件来实现这一点.我的问题是,一旦用户处于控件中的第二步,如果用户关闭对话框,则再次打开对话框,(注意控件在服务器上可见并通过更新更新面板重新加载)第二步仍会显示.原因是 .因为每当有回发时,控件将使用视图状态加载其状态,即使 EnableViewState 为 false,它仍将使用 LoadControlState 方法加载它.所以我的问题是如何强制用户控件在没有任何回发数据的情况下使用其默认值加载新控件.

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.

推荐答案

这是可能的.

我在控件类中找到了一个秘密方法,叫做ClearChildState(),这是一个受保护的方法,会清除所有子控件的viewstate和controlstate.

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();
        }
    }
}

在我的页面初始化事件中,我检查 request[flag] 以重置控件

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();               
        }
    }
}

在客户端,我有一个 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>

这篇关于如何在回发后加载具有默认值的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆