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

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

问题描述

我有数据绑定网格(绑定到对象源)一个简单的ASP页。网格是一个向导页面内有各行的选择复选框。

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

 保护无效Wizard1_NextButtonClick(对象发件人,WizardNavigationEventArgs E)
    {
...
        //绑定和显示的比赛
        GridViewMatches.EnableViewState = TRUE;
        GridViewMatches.DataSource = getEmailRecipients();
        GridViewMatches.DataBind();

和单击Finish按钮时,我通过迭代行和检查什么的选择:

 保护无效Wizard1_FinishButtonClick(对象发件人,WizardNavigationEventArgs E)
{
    //取决于网格上的复选框设置所选的值,。
    的foreach(在GridViewMatches.Rows GridViewRow GR)
    {
        INT32 PERSONID = Convert.ToInt32(gr.Cells [0]。文本);
        复选框选中=(复选框)gr.Cells [1] .FindControl(CheckBoxSelectedToSend);

但现阶段GridViewMatches.Rows.Count!= 0我不重新绑定网格,我不应该要吧?我期望的视图状态,以保持状态。 (另外,如果我做重新绑定网格,我的选择复选框将被清除)

注:该页面还增加了动态的用户控件的OnInit方法。我听说,这可能与视图状态一塌糊涂,但据我可以告诉大家,我做正确,并为那些添加的控件视图状态似乎工作(值回发之间持久化)

非常感谢您的任何帮助!

瑞安

更新:难道这是一个事实,我编程设定DataSource的呢?我想知道如果ASP引擎的页面生命周期尚未定义数据源数据绑定在网格。 (在测试页面时,GridView是自动数据绑定。我不想让电网重新绑定我只是想从previous后视图状态的值呢!

另外,我有这个在asp头:ViewStateEncryptionMode =从不 - 这是解决偶尔'无效视图状态MAC验证的消息

有关参考,我的GridView定义如下:

 < ASP:GridView控件ID =GridViewMatches=服务器AllowSorting =真的AutoGenerateColumns =FALSE
    背景色=白BORDERCOLOR =#E7E7FF边框=无边框宽度=1px的CELLPADDING =3
    OnDataBinding =GridViewMatches_OnBinding>
        <柱体和GT;
            < ASP:BoundField的数据字段=是PersonID>< ItemStyle的CssClass =隐藏/>< / ASP:BoundField的>
            < ASP:的TemplateField>
                <&ItemTemplate中GT;
                    < ASP:复选框ID =CheckBoxSelectedToSend=服务器
                        选中='<%#DataBinder.Eval的(的Container.DataItem,SelectedToSend)%>/>
                < / ItemTemplate中>
...


解决方案

遍历控制在preINIT事件(以检测是否添加其他控件或删除其他控件按钮是pressed)无效的视图状态!

下面是preINIT

调用的方法

 公共控制GetPostBackControl(页翻动书页)
    {
        //返回null;        控制myControl = NULL;
        字符串ctrlName = thePage.Request.Params.Get(__ EVENTTARGET);
        如果(((ctrlName = NULL)及!(ctrlName =的String.Empty)))
        {
            myControl = thePage.Master.FindControl(ctrlName);
        }
        其他
        {
            的foreach(在thePage.Request.Form字符串项)
            {
                控制C = thePage.Master.FindControl(项目);
                如果(((c)是System.Web.UI.WebControls.Button))
                {
                    myControl = C;
                }
            }        }        返回myControl;
    }

(我不采取信用为这个收作方法初探,我发现它在网络上)

如果第一行注释掉,视图状态保持不变。

可怕!

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.

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");

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)

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)

Thanks a lot in advance for any help!

Ryan

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!

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

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>
...

解决方案

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!!

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.

Awful!

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

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