GridView 不记得回发之间的状态 [英] GridView doesn't remember state between postbacks

查看:29
本文介绍了GridView 不记得回发之间的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有数据绑定网格(绑定到对象源)的简单 ASP 页面.网格位于向导页面内,每行都有一个选择"复选框.

I have a simple ASP page with databound grid (bound to an object source). The grid is within the page of a wizard and has a 'select' checkbox for each row.

在向导的一个阶段,我绑定了 GridView:

In one stage of the wizard, I bind the GridView:

protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
    {
...
        // Bind and display matches
        GridViewMatches.EnableViewState = true;
        GridViewMatches.DataSource = getEmailRecipients();
        GridViewMatches.DataBind();

当点击完成按钮时,我遍历行并检查选择了什么:

And when the finish button is clicked, I iterate through the rows and check what's selected:

protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
{
    // Set the selected values, depending on the checkboxes on the grid.
    foreach (GridViewRow gr in GridViewMatches.Rows)
    {
        Int32 personID = Convert.ToInt32(gr.Cells[0].Text);
        CheckBox selected = (CheckBox) gr.Cells[1].FindControl("CheckBoxSelectedToSend");

但是在这个阶段 GridViewMatches.Rows.Count = 0!我不重新绑定网格,我应该不需要,对吧?我希望视图状态保持状态.(另外,如果我重新绑定网格,我的选择复选框将被清除)

But at this stage GridViewMatches.Rows.Count = 0! I don't re-bind the grid, I shouldn't need to, right? I expect the view-state to maintain the state. (Also, if I do rebind the grid, my selection checkboxes will be cleared)

注意:此页面还在 OnInit 方法中动态添加用户控件.我听说它可能会扰乱视图状态,但据我所知,我做对了,那些添加控件的视图状态似乎有效(值在回发之间保持不变)

NB: This page also dynamically adds user controls in OnInit method. I have heard that it might mess with the view state, but as far as I can tell, I am doing it correctly and the viewstate for those added controls seems to work (values are persisted between postbacks)

非常感谢您的帮助!

瑞恩

更新:这可能与我以编程方式设置数据源的事实有关吗?我想知道 asp 引擎是否在页面生命周期期间将网格数据绑定到尚未定义的数据源.(在测试页面中,GridView 是自动"数据绑定的.我不希望网格重新绑定,我只想要上一篇文章中视图状态中的值!

UPDATE: Could this be to do with the fact I am setting the datasource programatically? I wondered if the asp engine was databinding the grid during the page lifecycle to a datasource that was not yet defined. (In a test page, the GridView is 'automatically' databound'. I don't want the grid to re-bound I just want the values from the viewstate from the previous post!

此外,我在 asp 标头中有这个:ViewStateEncryptionMode="Never" - 这是为了解决偶尔出现的无效的 Viewstate 验证 MAC"消息

Also, I have this in the asp header: ViewStateEncryptionMode="Never" - this was to resolve an occasional 'Invalid Viewstate Validation MAC' message

作为参考,我的GridView定义如下:

For reference, my GridView is defined as follows:

<asp:GridView ID="GridViewMatches" runat="server" AllowSorting="True" AutoGenerateColumns="False" 
    BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
    OnDataBinding="GridViewMatches_OnBinding">
        <Columns>
            <asp:BoundField DataField="PersonID"><ItemStyle CssClass="hidden"/></asp:BoundField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBoxSelectedToSend" runat="server"
                        Checked='<%# DataBinder.Eval(Container.DataItem, "SelectedToSend") %>'/>
                </ItemTemplate>
...

推荐答案

在 PreInit 事件中迭代控件(以检测是否按下了添加另一个控件"或删除另一个控件"按钮)使视图状态无效!!

Iterating the controls in the PreInit event (to detect if the 'add another control' or 'remove another control' button was pressed) invalidates the view state!!

这是从 PreInit 调用的方法

Here is the method called from PreInit

public Control GetPostBackControl(Page thePage)
    {
        //return null;

        Control myControl = null;
        string ctrlName = thePage.Request.Params.Get("__EVENTTARGET");
        if (((ctrlName != null) & (ctrlName != string.Empty)))
        {
            myControl = thePage.Master.FindControl(ctrlName);
        }
        else
        {
            foreach (string Item in thePage.Request.Form)
            {
                Control c = thePage.Master.FindControl(Item);
                if (((c) is System.Web.UI.WebControls.Button))
                {
                    myControl = c;
                }
            }

        }

        return myControl;
    }

(我不认为这种方法是我在网上找到的)

(I take no credit for this methd, I found it on the web)

如果第一行没有注释,则保持视图状态.

If the first line is uncommented, the viewstate is maintained.

糟糕!

这篇关于GridView 不记得回发之间的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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