3

我有一个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;
    }
}

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

  • this.grdPractices.Rows[e.NewEditIndex].Controls[0].FindControl("lstSites") as DropDownList;
  • this.grdPractices.Rows[e.NewEditIndex].FindControl("lstSites") as DropDownList;
  • this.grdPractices.Rows[e.NewEditIndex].FindControlRecursive("lstSites") as DropDownList;

结果总是一样的,NULL返回a。

在 OnRowEditing 事件中,您到底应该如何连续获得控件?

4

2 回答 2

1

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

this.grdPractices.EditIndex = e.NewEditIndex;

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

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

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

于 2012-10-26T10:54:24.663 回答
0

尝试使用GridViewRow如下...

protected void grd_RowEditing(object sender, GridViewEditEventArgs e) 
{
     GridViewRow selectRow = grd.Rows(e.NewEditIndex);
     DropDownList sitesDropDown =(DropDownList)selectRow.Cells[2].FindControl("lstSites"); 
}
于 2012-10-25T14:47:37.477 回答