我试图在Gridview edititemtemplate部分找到此控件。
<EditItemTemplate>
<ajaxToolkit:ComboBox ID="GridviewCategoryComboBox1" AppendDataBoundItems="true" runat="server" AutoCompleteMode="Suggest" DataSourceID="GridViewCategorySqlDataSource1" DataTextField="Name" DataValueField="Id" MaxLength="0" Style="display: inline;">
<asp:ListItem>Select Category</asp:ListItem>
</ajaxToolkit:ComboBox>
这是我尝试获取edititem模板中的控件的事件处理程序。
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs
e)
{
GridView1.EditIndex = e.NewEditIndex;
int id = (int)GridView1.DataKeys[e.NewEditIndex].Value;
ComboBox ddl = GridView1.Rows[e.NewEditIndex].Cells[1].FindControl("GridviewCategoryComboBox1") as ComboBox;
}
它返回null,无论我试图找到它。
我也尝试了其他变体,例如:
ComboBox ddl = GridView1.Rows[e.NewEditIndex].FindControl("GridviewCategoryComboBox1") as ComboBox;
您可以使用RowDataBound
事件:
protected void GridView1_RowDataBound(object sender, GridViewEditEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
if ((e.Row.RowState & DataControlRowState.Edit) > 0) {
ComboBox ddl = (ComboBox)e.Row.FindControl("GridviewCategoryComboBox1");
}
}
}
因为您可能在RowDataBound
事件中有其他代码,所以这允许您集中该事件中的所有代码并避免重复代码。