0

我正在使用下面的代码来绑定另一个表中的下拉数据。并且还使用 rowindex 引用该控件名称。但它总是返回null。并且还返回错误消息。

  `Object reference not set to an instance of an object.` 

我使用这两种方法,但都返回控件名称 null

第一个代码:

 protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Control ctrl = e.Row.FindControl("DDL_STATUS_FT"); //It always return null
            if (ctrl != null)
            {
                DropDownList dd = ctrl as DropDownList;
                DataSet7TableAdapters.sp_getall_trv_masterTableAdapter TA = new DataSet7TableAdapters.sp_getall_trv_masterTableAdapter();
                DataSet7.sp_getall_trv_masterDataTable DS = TA.GetData();
                dd.DataTextField = "fld_TName";
                dd.DataValueField = "fld_id";
                dd.DataSource = DS;
                dd.DataBind();
            }

        }

    } 

第二 :

 In databind function



if (DS.Rows.Count > 0)
    {
        GridView2.DataSource = DS;
        GridView2.DataBind();

    foreach (GridViewRow grdRow in GridView2.Rows)
    {
        DataSet7TableAdapters.sp_getall_trv_masterTableAdapter TA1 = new DataSet7TableAdapters.sp_getall_trv_masterTableAdapter();
        DataSet7.sp_getall_trv_masterDataTable DS1 = TA1.GetData();
        // Nested DropDownList Control reference is passed to the DrdList object. This will allow you access the properties of dropdownlist placed inside the GridView Template column.  
        DropDownList drdList = (DropDownList)(GridView2.Rows[grdRow.RowIndex].Cells[4].FindControl("DDL_STATUS_FT"));//It always return null

        // DataBinding of nested DropDownList Control for each row of GridView Control.  
        drdList.DataSource = DS1;
        drdList.DataValueField = "fld_id";
        drdList.DataTextField = "fld_TName";
        drdList.DataBind();
    } 
}

请帮我这样做..

   <asp:TemplateField ItemStyle-Width="100px" HeaderText="TYPE">
                        <ItemTemplate>
                            <asp:DropDownList ID="DDL_STATUS" runat="server" AutoPostBack="true" Enabled="false" >
                            </asp:DropDownList>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="DDL_edit_STATUS" runat="server" AutoPostBack="true" SelectedValue='<%# Eval("fld_Type") %>'>
                            </asp:DropDownList>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:DropDownList ID="DDL_STATUS_FT" runat="server" AutoPostBack="true">
                            </asp:DropDownList>
                        </FooterTemplate>
                    </asp:TemplateField>
4

3 回答 3

1

DropDown“DDL_STATUS_FT”在Footer Template..你必须检查它如下..

if(e.Row.RowType == DataControlRowType.Footer)
{
  DropDownList ctrl =(DropDownList)e.Row.Cells[CellIndex].FindControl("DDL_STATUS_FT"); 
}
于 2012-09-25T07:35:32.870 回答
0

已编辑2

您必须dropdownlist在 gridview item-template 中有 aspx 代码

您发现 usinge.row.findcontrol没有如此滥用地声明它返回 null

因此,首先将下拉列表添加到您的网格视图中,这是您的下拉列表的示例

   <asp:TemplateField ItemStyle-Width="30px" HeaderText="DDL_STATUS_FT">
                        <ItemTemplate>
                            <asp:Dropdownlist ID="DDL_STATUS_FT" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>

如果你ctrl 有空 Control ctrl = e.Row.FindControl("DDL_STATUS_FT"); //它总是返回null

然后确保在您的 aspx 代码中 DDL_STATUS_FTControl 是runat="server"

于 2012-09-25T06:47:23.040 回答
0

尝试通过 cell 和 cellIndex 找到您的控件,例如...

Control ctrl = e.Row.Cells[yourCellIndex].FindControl("DDL_STATUS_FT");
于 2012-09-25T07:12:52.227 回答