为什么“FindControl"找不到下拉列表?GridViewRow 的? [英] Why isn't the dropdownlist found by "FindControl" of GridViewRow?

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

问题描述

两个例外:

  1. 索引越界
  2. FindControl 返回 null(假装或未检测到控件)

cs 代码:(现在下拉列表只需要在编辑模式下填充)

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 代码:

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

参考文献:

编辑:

那些慷慨地尝试并花时间帮助我解决问题的人,请查看Jeff Atwood 的关于 Page.FindControl 的博客文章. 阅读它,我觉得我的下拉列表绝对是 Gridview 中的一个孩子...... 鉴于这篇文章,它更接近我遇到的情况..但我不是 100% 确定,如果同样的情况适用于我正在努力解决的问题,因为我有两个网格视图.然而,只有一个具有编辑模式控件 - 另一个是普通的 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.

这主要是由于 Text='<%# Bind("[columnx]")%>' of,

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

这篇关于为什么“FindControl"找不到下拉列表?GridViewRow 的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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