无法通过OnRowEditing事件在编辑模板中找到控件

时间:2012-10-25 13:32:47

标签: asp.net gridview edit

我有GridView这样:

<asp:GridView runat="server" ID="grdPractices" PageSize="10" AutoGenerateColumns="False" DataKeyNames="Id" 
    CssClass="linkGrid" AllowSorting="True" OnSorting="grdPractices_OnSorting" OnRowDataBound="grdPractices_OnRowDataBound"
    OnRowEditing="grdPractices_OnRowEditing" OnRowCancelingEdit="grdPractices_OnRowCancelingEdit" OnRowUpdating="grdPractices_OnRowUpdating">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" meta:resourcekey="PracticeName" ReadOnly="True" SortExpression="Name" ItemStyle-Width="400px" />
        <asp:BoundField DataField="Code" HeaderText="Code" meta:resourcekey="Code" ReadOnly="True" SortExpression="Code" ItemStyle-Width="200px" />
        <asp:TemplateField meta:resourcekey="SiteName" ItemStyle-Width="200px" SortExpression="SiteName">
            <ItemTemplate>
                <asp:Literal runat="server" Text='<%# Eval("SiteName") %>' />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:DropDownList runat="server" ID="lstSites" />
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField meta:resourcekey="NumOfUsers" ItemStyle-Width="200px" SortExpression="NumOfUsers">
            <ItemTemplate>
                <asp:LinkButton runat="server" OnCommand="OnLinkButtonCommand" CommandName="ViewUsers" Text='<%# Eval("NumOfUsers") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowDeleteButton="False" ShowCancelButton="True" 
            ShowInsertButton="False" ShowEditButton="True" EditText="Edit"
            CancelText="Cancel" UpdateText="Update" meta:resourcekey="Edit" ItemStyle-Width="200px" />
    </Columns>
</asp:GridView>

以下是OnRowEditing事件的代码:

protected void grdPractices_OnRowEditing(object sender, GridViewEditEventArgs e)
{
    this.grdPractices.EditIndex = e.NewEditIndex;

    var sitesDropDown = this.grdPractices.Rows[e.NewEditIndex].Controls[0].FindControl("lstSites") as DropDownList;

    if (sitesDropDown == null)
    {
        return;
    }
}

我的问题是我无法控制住在EditTemplate中的lstSites控件。我尝试过使用以下内容:

  • this.grdPractices.Rows [e.NewEditIndex] .Controls [0] .FindControl( “lstSites”) 作为DropDownList;
  • this.grdPractices.Rows [e.NewEditIndex] .FindControl(“lstSites”)as DropDownList的;
  • this.grdPractices.Rows [e.NewEditIndex] .FindControlRecursive( “lstSites”) 作为DropDownList;

结果始终相同,返回NULL

在OnRowEditing事件中,你究竟应该连续获得一个控件?

2 个答案:

答案 0 :(得分:1)

好的,在这一行之后我没做什么:

this.grdPractices.EditIndex = e.NewEditIndex;

然后重新绑定网格的数据。因此在重新绑定数据之后,然后调用:

var sitesDropDown = this.grdPractices.Rows[e.NewEditIndex].FindControlRecursive("lstSites") as DropDownList;

我现在能够与siteDropDown变量进行互动,因为它现在包含对lstSites控件的引用。

答案 1 :(得分:0)

尝试使用GridViewRow,如下所示......

protected void grd_RowEditing(object sender, GridViewEditEventArgs e) 
{
     GridViewRow selectRow = grd.Rows(e.NewEditIndex);
     DropDownList sitesDropDown =(DropDownList)selectRow.Cells[2].FindControl("lstSites"); 
}