递归遍历控件的OnInit在GridView的禁用和RowCommand失去视图状态 [英] Recursively traversing controls In OnInit disables GridView RowCommand and loses Viewstate

查看:133
本文介绍了递归遍历控件的OnInit在GridView的禁用和RowCommand失去视图状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我递归遍历页面控件,RowCommand事件不再受LinkBut​​tons在GridView解雇。事实上,它看起来像为GridView的ViewState中丢失。为什么?我该如何解决此问题?

,直至取消注释 //递归(this.Controls)线下code将正常工作。然后,当你点击链接时,GridView消失,RowCommand永远不会被解雇了。

完整<身体GT; 我的网页:

 <表ID =form1的=服务器>
    < ASP:GridView控件ID =GV的AutoGenerateColumns =假=服务器onrowcommand =gv_RowCommand>
        <柱体和GT;< ASP:的TemplateField的HeaderText =链接><&ItemTemplate中GT;
            < ASP:LinkBut​​ton的ID =LNK=服务器CommandArgument ='XXX'> XXX和LT; / ASP:LinkBut​​ton的>
        < / ItemTemplate中>< / ASP:的TemplateField>< /专栏>
    < / ASP:GridView的>
< /表及GT;

我的code背后:

 保护覆盖无效的OnInit(EventArgs的发送)
    {
        base.OnInit(E);
        //recurse(this.Controls);
    }
    私有静态无效递归(ControlCollection中控件)
    {
        的foreach(对照控制控制)
            递归(control.Controls);
    }
    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        如果(!this.IsPostBack)
        {
            VAR DT =新的DataTable();
            dt.Columns.Add(链接,typeof运算(字符串));
            DataRow的博士= dt.NewRow();
            博士[链接] =google.com
            dt.Rows.Add(DT);
            DataSet的DS =新的DataSet();
            ds.Tables.Add(DT);
            gv.DataSource = DS;
            gv.DataBind();
        }
    }
    保护无效gv_RowCommand(对象发件人,GridViewCommandEventArgs E)
    {
        如果(this.Application [计数器] == NULL)
            this.Application [计数器] = 0;
        this.Application [计数器] =(int)的this.Application [计数器] + 1;
        的Response.Write(垃圾+ this.Application [计数器]);
    }


解决方案

@jbl发现的东西,解释了这个不够好,我(感谢检索技巧!)。 http://forums.asp.net/t/1043999:这个问题以前在这里被视为的.aspx / 1

在总结该页面,在初始化阶段访问的GridView的 .Controls 属性以任何方式会破坏它的ViewState。有为什么没有解释,但它已被观察反正

有是网页,是我不够好,对一种解决方法。如果您检查每个控制,看它是否 .HasControls(),如果不访问其<$​​ C $ C> .Controls 属性这不,ViewState中不会丢失,因此事件将激发。

P.S。我猜这是一个错误,但当然不能被改变,因为永远向后兼容的:(

If I recursively traverse the controls of a page, the RowCommand event is no longer fired by LinkButtons in a GridView. In fact, it looks like the ViewState for the GridView is lost. Why? How can I work around this?

The code below will work fine until you uncomment the //recurse(this.Controls) line. Then, when you hit the link, the GridView disappears and the RowCommand is never fired.

The complete <body> of my page:

<form id="form1" runat="server">
    <asp:GridView ID="gv" AutoGenerateColumns="False" runat="server" onrowcommand="gv_RowCommand">
        <Columns><asp:TemplateField HeaderText="Link"><ItemTemplate>
            <asp:LinkButton ID="lnk" runat="server" CommandArgument = 'xxx'>xxx</asp:LinkButton>
        </ItemTemplate></asp:TemplateField></Columns>
    </asp:GridView>
</form>

My code behind:

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        //recurse(this.Controls);
    }
    private static void recurse(ControlCollection controls)
    {
        foreach (Control control in controls)
            recurse(control.Controls);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            var dt = new DataTable();
            dt.Columns.Add("Link", typeof(string));
            DataRow dr = dt.NewRow();
            dr["Link"] = "google.com";
            dt.Rows.Add(dt);
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            gv.DataSource = ds;
            gv.DataBind();
        }
    }
    protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (this.Application["counter"] == null)
            this.Application["counter"] = 0;
        this.Application["counter"] = (int)this.Application["counter"] + 1;
        Response.Write("JUNK" + this.Application["counter"]);
    }

解决方案

@jbl found something that explained this well enough for me (thanks for the search skills!). This problem has been seen before here: http://forums.asp.net/t/1043999.aspx/1

Summarizing that page, accessing the .Controls property of a GridView in any way during the Init phase will destroy its ViewState. There is no explanation for why, but it has been observed anyway.

There is a workaround on that page that is good enough for me. If you check for each control to see if it .HasControls() and don't access its .Controls property if it doesn't, the ViewState will not be lost, and consequently events will fire normally.

P.S. I'm GUESSING this is a bug, but of course it can't be changed forevermore because of backwards compatibility :(

这篇关于递归遍历控件的OnInit在GridView的禁用和RowCommand失去视图状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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