繁体   English   中英

网格视图下拉列表数据绑定错误

[英]Grid view Dropdown list data binding error

我正在使用以下代码从另一个表绑定下拉数据。 并且还使用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>

下拉Footer Template “ DDL_STATUS_FT”位于Footer Template您必须按以下步骤进行检查。

if(e.Row.RowType == DataControlRowType.Footer)
{
  DropDownList ctrl =(DropDownList)e.Row.Cells[CellIndex].FindControl("DDL_STATUS_FT"); 
}

编辑2

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

您正在发现使用e.row.findcontrol没有对其进行如此滥用地声明它返回null

因此,首先将dropdownlist添加到您的gridview中,这是您的dropdownlist的示例

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

如果您有空ctrl控件ctrl = e.Row.FindControl(“ DDL_STATUS_FT”); //它总是返回null

然后确保在您的aspx代码DDL_STATUS_FT控件中运行runat="server"

尝试通过cell和cellIndex查找控件,例如...

Control ctrl = e.Row.Cells[yourCellIndex].FindControl("DDL_STATUS_FT");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM