简体   繁体   English

如何防止AutoPostBack重置我的页面?

[英]How to prevent AutoPostBack from resetting my page?

I am building a page that includes some components that are static on the page (dropdowns, buttons, a table to hold them in), but one table cell is filled with variably generated CheckBoxes. 我正在构建一个页面,其中包含页面上静态的一些组件(下拉列表,按钮,用于保存它们的表),但是一个表格单元格填充了可变生成的CheckBoxes。 When a button is pressed, the code for the page calculates what checkboxes to place and creates a new CheckBox object for each one needed and then adds it to an existing Div on the page. 按下按钮时,页面代码会计算要放置的复选框,并为每个所需的CheckBox创建一个新的CheckBox对象,然后将其添加到页面上的现有Div中。

I am trying to trigger some code to run when any of these are checked or unchecked, but checkBoxName.CheckedChanged += cbCheckedChanged wasn't working. 我试图在检查或取消选中任何代码时触发一些代码运行,但checkBoxName.CheckedChanged += cbCheckedChanged无效。 I researched and found two suggestions: enabling viewstate and enabling autopostback. 我研究并发现了两个建议:启用viewstate并启用autopostback。

checkBoxName.EnableViewState = true; seems to make no difference, nor did checkBoxName.ViewStateMode = ViewStateMode.Enabled; 似乎没有区别, checkBoxName.ViewStateMode = ViewStateMode.Enabled; or any variations on that i tried. 或者我试过的任何变化。 checkBoxName.AutoPostBack = true; did SOMETHING, but it's not allowing it to run the code I want. 做了SOMETHING,但它不允许它运行我想要的代码。 I think that's because it doesn't reach that point because of the next problem: 我认为这是因为由于下一个问题,它没有达到这一点:

With AutoPostBack = true , whenever I check or uncheck a box, cbCheckedChanged is not being executed, and the entire page is reloading, resetting back to it's initial state, therefore removing the checkboxes completely from the table. 使用AutoPostBack = true ,每当我选中或取消选中一个框时, cbCheckedChanged都没有被执行,整个页面正在重新加载,重置回它的初始状态,因此从表中完全删除了复选框。

How can I fix these problems, or at least where might I start looking? 我该如何解决这些问题,或者至少我可以从哪里开始寻找?

Edit: 编辑:

This is where the checkboxes are created: 这是创建复选框的位置:

CheckBox cb = new CheckBox();
cb.Text = CBName;
cb.EnableViewState = true;
cb.ViewStateMode = ViewStateMode.Enabled;
cb.AutoPostBack = true;
cb.CheckedChanged += CBCheckedChanged;

and this is where CBCheckedChanged is: 这是CBCheckedChanged的地方:

private void CBCheckedChanged(object sender, EventArgs e)
{
\\Stuff
}

When I use breakpoints to step through it, it never reaches CBCheckedChanged. 当我使用断点来逐步执行它时,它永远不会到达CBCheckedChanged。 I have tried every possible combination of commenting out and leaving in the AutoPostBack, ViewStateMode, and EnableViewState lines. 我已经尝试了在AutoPostBack,ViewStateMode和EnableViewState行中注释和离开的所有可能组合。

Page_Load is currently empty, nothing runs until the user hits a button. Page_Load当前为空,在用户点击按钮之前不会运行任何内容。

ViewState is by default enabled for server side controls. 默认情况下,为服务器端控件启用ViewState You don't have to tinker around with ViewState to solve your problem. 您不必修改ViewState来解决您的问题。 ViewState is basically used to restore the state of a control after a postback happens. ViewState主要用于在回发发生后恢复控件的状态。 For example readding all entries to a ListBox control. 例如,读取ListBox控件的所有条目。 This is why most control population code is within such a construct in the Page_Load method. 这就是为什么大多数控制填充代码都在Page_Load方法中的这种构造中。

private void Page_Load()
{
    if (!Page.IsPostBack)
    {
        // populate controls here
    }
}

ViewState is a very misunderstood concept. ViewState是一个被误解的概念。 There is a great article here that goes into it in detail. 这里有一篇很棒的文章详细介绍了它。 But as I said, for your problem ViewState is not a concern. 但正如我所说,对于你的问题,ViewState不是一个问题。

To solve your problem: 要解决您的问题:

The problem in your case is that the triggered button is creating the CheckBox controls, but when the page is reloaded, because of the CheckedChanged event of these controls, the next page life cycle has no clue of the CheckBox controls that were placed on the page in the previous page life cycle. 您的情况下的问题是触发按钮正在创建CheckBox控件,但是当重新加载页面时,由于这些控件的CheckedChanged事件,下一页生命周期不知道放在页面上的CheckBox控件在上一页生命周期中。 Dynamic controls need to be generated for every page life cycle! 需要为每个页面生命周期生成动态控件!

So what I would do is create a method that: 所以我要做的是创建一个方法:

  • creates the CheckBox controls and 创建CheckBox控件和
  • sets AutoPostback = true for them and 为他们设置AutoPostback = true
  • sets the event handler for CheckedChanged 设置CheckedChanged的事件处理程序

Let's call this method AddDynamicCheckBoxes() . 让我们调用这个方法AddDynamicCheckBoxes() Now you need to call this method in the Page_Load event of your Page when the button was already pressed and also in the event handler of the button's click event. 现在,您需要在按下按钮时在PagePage_Load事件中调用此方法,并且还需要在按钮的click事件的事件处理程序中调用此方法。 You could do this like follows: 您可以这样做:

private void Page_Load()
{
    if (ViewState["button_was_clicked"] != null)
    {
        AddDynamicCheckBoxes();
    }
}

private void Button_OnClick()
{
    AddDynamicCheckBoxes();
    ViewState["button_was_clicked"] = true;
}

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

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