0

我正在使用从 SQL 数据库馈送的 gridview。我需要添加一个自定义 itemTemplate,它将由另一个数据源提供的复选框列表组成。这是我的 xml 部分:

        <asp:TemplateField HeaderText="Equipements" >
            <EditItemTemplate>
                <asp:CheckBoxList ID="myCB" runat="server">
                </asp:CheckBoxList>
            </EditItemTemplate>
        </asp:TemplateField>

我正在使用 gridview onEditing 事件尝试在后面的代码中访问“myCB”:

    protected void OnEditing(object sender, EventArgs e)
    {
        GridView gridview = sender as GridView;
        GridViewEditEventArgs  editEvent = e as GridViewEditEventArgs;
        ListViewItemEventArgs rowEvent = e as ListViewItemEventArgs;
        TableCell equipementsCell = gridview.Rows[editEvent.NewEditIndex].Cells[11];

        CheckBoxList equipements = gridview.Rows.FindControl("myCB") as CheckBoxList;
    }

此代码不起作用,未找到复选框列表。我尝试了很多事情都没有成功...

4

1 回答 1

0

您应该使用 GridView 的RowDataBound事件:

protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType  == DataControlRowType.DataRow
    &&  e.Row.RowState == DataControlRowState.Edit)
    {
        CheckBoxList equipements = (CheckBoxList )e.Row.FindControl("myCB"); 
        equipements.DataSource = getSomeData();
        equipements.DataTextField = "TextColumn"; 
        equipements.DataTextField = "IdColumn";
        equipements.DataBind();
    }
}
于 2012-10-17T09:06:55.740 回答