为什么不将DropDownList由&QUOT找到;的FindControl" GridViewRow的? [英] Why isn't the dropdownlist found by "FindControl" of GridViewRow?

查看:88
本文介绍了为什么不将DropDownList由&QUOT找到;的FindControl" GridViewRow的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两个例外:


  1. 索引越界

  2. 的FindControl返回空值(这是pretending或不检测控制)

CS code:(现在DROPDOWNLIST只需要在编辑模式下进行填充)

cs code: (for now dropdownlist just needs to be populated at editing mode)

protected void GridView3_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView3.EditIndex = e.NewEditIndex;
            ShowData("a"); //bind data

            GridViewRow gVR = GridView3.Rows[GridView3.EditIndex];                                       

ASPX code:

aspx code:

                 <asp:TemplateField HeaderText="x" ItemStyle-CssClass="ix">
                     <EditItemTemplate>
                         <asp:DropDownList ID="xnList"  runat="server" Text='<%# Bind("[columnx]")%>'>
                         </asp:DropDownList>
                     </EditItemTemplate>
                     <ItemTemplate>
                         <asp:Label ID="Label3" runat="server" Text='<%# Bind("[columnx]") %>'></asp:Label>
                     </ItemTemplate>
                     <ItemStyle CssClass="ix" />
                 </asp:TemplateField>

由于上面的片段,就在我收到以下错误3号线。这是荒谬的一样很适合其他GridView和GridView控件这个有10个行,所以绝对不会越界。什么能在这里是问题?

Given above snippet, right at the 3rd line I am getting following error. This is absurd as the same works well for other gridview and this gridview has 10 rows, so definitely not out of bound. What could be the issue here?

参考文献:

  • Finding a control in GridView in EDIT Mode returns null.
  • FindControl in gridview returns null

修改

这些谁是慷慨地努力,保全自己的时间来帮助我一个解决方案,请查看杰夫阿特伍德有关博客文章 Page.FindControl 。读它,我觉得我的DropDownList肯定是在GridView的孩子... <一个href=\"http://stackoverflow.com/questions/13336839/asp-net-findcontrol-and-gridview-returning-null\">Given这个帖子,说到更接近我所遇到的。。
但我不是100%肯定,如果同一案件中适用于什么,我挣扎,因为我有两个GridView的。但是,只有一个人编辑模式控制 - ?另一种是普通纯的 gridvos 的有人能告诉我正确的方向

Those who are generously trying and sparing their time to help me out with a solution, please check out Jeff Atwood's blog post about Page.FindControl. Reading it, I feel my dropdownlist is definitely a child within Gridview... Given this post, it comes much closer to what I have encountered.. But I am not 100% sure, if same case applies to what I am struggling with, since I have two gridviews. However only one has edit mode controls - the other is plain plain gridvos. Can someone show me the right direction?

修改
我曾尝试上面的链接的答案/解决方案中的每一个。无工作,截至目前。

EDIT: I have tried each one of above link's answers/solution. None working as of now.

推荐答案

正如许多人所指出的RowDataBound()是正确的事件的挂钩的控件的数据了GridView控件中进行编辑,更新或显示模式。我绝望了,然后尝试了Row_Updating。然而,这不符合我正在错误的问题。

As many have pointed out the RowDataBound() is the correct event to hook data up for controls within gridview for edit, update or display modes. I was desperate and then tried out Row_Updating. HOwever that wasn't the issue with the error I was getting.

这主要是由于在文本='&LT;%#绑定([columnx])%&GT;

<asp:DropDownList ID="xnList" runat="server" Text='<%# Bind("[columnx]")%>'>

所以最终的解决方案是按照任何的答案贴在那里。

So the final solution is as per any of the answers posted out there.

CS:

    protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
    {            

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
            {
                DropDownList ddl = e.Row.FindControlRecursive("dhl") as DropDownList;
                DropDownList stageDDL = e.Row.FindControlRecursive("dhl") as DropDownList;
                stageDDL.DataSource = this.clservice.Getstuff("someparam");
                stageDDL.DataTextField = "columnx";
                stageDDL.DataValueField = "columnx";
                stageDDL.DataBind();

            }
        }
    }

ASPX:

            <asp:TemplateField HeaderText="x" ItemStyle-CssClass="ix">
                <EditItemTemplate>
                    <asp:DropDownList ID="xnList"  runat="server" DataTextField="columnx" DataValueField="columnx">
                    </asp:DropDownList>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("[columnx]") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle CssClass="ix" />
            </asp:TemplateField>

这篇关于为什么不将DropDownList由&QUOT找到;的FindControl&QUOT; GridViewRow的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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