【问题标题】:DropDownList inside a GridView is not being seen as an objectGridView 内的 DropDownList 不被视为对象
【发布时间】:2017-08-17 13:27:09
【问题描述】:

我无法弄清楚我在这里做错了什么。尝试在 gridview 中动态填充下拉列表,它一直告诉我对象引用未设置为对象的实例。

首先,我的 ASPX:

<asp:Panel runat="server" ID="ShowDiv3" Visible="false" BorderStyle="Solid" BorderWidth="0"  Width="1389px">
        <asp:Label ID="lblShowDiv3Title" runat="server" Text="Root Causes:    " Font-Bold="true"></asp:Label><asp:DropDownList ID="ddlRootCauses" runat="server" Width="300px" OnSelectedIndexChanged="ddlRootCauses_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
        <br />
        <%-- This line needed to be commented out when paging was removed: AllowPaging="True"  AllowCustomPaging="True" PageSize="10" --%>
        <div id="divGrid3" style='width:1290px; overflow:auto'>
    <asp:GridView ID="DataGrid_RootCauses" runat="server" 
        AllowSorting="True" OnSorting="DataGrid_RootCauses_Sorting" AutoGenerateColumns="False" ShowFooter ="true" CellPadding="1" 
        CssClass="hoverTable"
        HeaderStyle-BackColor="#4DA6A6" HeaderStyle-BorderColor="#4DA6A6" 
        HeaderStyle-Font-Size="Small" HeaderStyle-ForeColor="White"
        FooterStyle-BackColor="#4DA6A6" FooterStyle-BorderColor="#4DA6A6" FooterStyle-ForeColor="White"
        DataKeyNames="root_cause_id"
                        OnRowDataBound="DataGrid_RootCauses_RowDataBound"
        OnRowCancelingEdit="DataGrid_RootCauses_CancelCommand"   
        OnRowEditing="DataGrid_RootCauses_EditCommand" 
        OnRowDeleting="DataGrid_RootCauses_DeleteCommand"
        OnRowUpdating="DataGrid_RootCauses_UpdateCommand">  
            <Columns>  

            <asp:TemplateField HeaderText="LPI Due">  
            <ItemStyle HorizontalAlign="Center"></ItemStyle>
            <ItemTemplate>  
                <asp:Label ID="lbl_LPI_DUE" runat="server" Width="70px" Text='<%#Eval("LPI_DUE") %>'></asp:Label>  
            </ItemTemplate>  
            <EditItemTemplate>  
                <asp:DropDownList ID="ddl_RC_LPI_DUE" runat="server" Width="70px" Text='<%#Eval("LPI_DUE") %>'>
                    <asp:ListItem Value=""> - </asp:ListItem>
                    <asp:ListItem Value="N"> N </asp:ListItem>
                    <asp:ListItem Value="Y"> Y </asp:ListItem>
                </asp:DropDownList>  
            </EditItemTemplate>  
            <FooterTemplate>  
                <asp:DropDownList ID="nddl_RC_LPI_DUE" runat="server" Width="70px" >
                    <asp:ListItem Value=""> - </asp:ListItem>
                    <asp:ListItem Value="N"> N </asp:ListItem>
                    <asp:ListItem Value="Y"> Y </asp:ListItem>
                </asp:DropDownList>  
            </FooterTemplate>
        </asp:TemplateField>                    

        <asp:TemplateField HeaderText="Root Cause Category">  
            <ItemTemplate>  
                <asp:Label ID="lbl_LPI_Category" runat="server" Width="170px" Text='<%#Eval("LPI_Category") %>'></asp:Label>  
            </ItemTemplate>  
            <EditItemTemplate>  
                <asp:DropDownList ID="ddl_RC_LPI_Category" runat="server" Width="170px" Text='<%#Eval("LPI_Category") %>'></asp:DropDownList>
            </EditItemTemplate>  
            <FooterTemplate>  
                <asp:DropDownList ID="nddl_RC_LPI_Category" runat="server" Width="170px" ></asp:DropDownList>
            </FooterTemplate>
        </asp:TemplateField>                   

        <asp:TemplateField HeaderText="Root Cause Reason">  
            <ItemTemplate>  
                <asp:Label ID="lbl_LPI_Reason" runat="server" Width="370px" Text='<%#Eval("LPI_reason") %>'></asp:Label>  
            </ItemTemplate>  
            <EditItemTemplate>  
                <asp:TextBox ID="txt_RC_LPI_Reason" runat="server" Width="370px" Text='<%#Eval("LPI_reason") %>'></asp:TextBox>  
            </EditItemTemplate>  
            <FooterTemplate>  
                <asp:TextBox ID="ntxt_RC_LPI_Reason" runat="server" Width="370px" ></asp:TextBox>  
            </FooterTemplate>
        </asp:TemplateField>                 

        <asp:TemplateField HeaderText="Business Owned">  
            <ItemStyle HorizontalAlign="Center"></ItemStyle>
            <ItemTemplate>  
                <asp:Label ID="lbl_Business" runat="server" Width="70px" Text='<%#Eval("Business") %>'></asp:Label>  
            </ItemTemplate>  
            <EditItemTemplate>  
                <asp:DropDownList ID="ddl_RC_Business" runat="server" Width="70px" Text='<%#Eval("Business") %>'>
                    <asp:ListItem Value=""> - </asp:ListItem>
                    <asp:ListItem Value="N"> N </asp:ListItem>
                    <asp:ListItem Value="Y"> Y </asp:ListItem>
                </asp:DropDownList>  
            </EditItemTemplate>  
            <FooterTemplate>  
                <asp:DropDownList ID="nddl_RC_Business" runat="server" Width="70px" >
                    <asp:ListItem Value=""> - </asp:ListItem>
                    <asp:ListItem Value="N"> N </asp:ListItem>
                    <asp:ListItem Value="Y"> Y </asp:ListItem>
                </asp:DropDownList>  
            </FooterTemplate>
        </asp:TemplateField>   

        <asp:TemplateField HeaderText="CP Owned">  
        <ItemStyle HorizontalAlign="Center"></ItemStyle>
            <ItemTemplate>  
                <asp:Label ID="lbl_CP" runat="server" Width="70px" Text='<%#Eval("CP") %>'></asp:Label>  
            </ItemTemplate>  
            <EditItemTemplate>  
                <asp:DropDownList ID="ddl_RC_CP" runat="server" Width="70px" Text='<%#Eval("CP") %>'>
                    <asp:ListItem Value=""> - </asp:ListItem>
                    <asp:ListItem Value="N"> N </asp:ListItem>
                    <asp:ListItem Value="Y"> Y </asp:ListItem>
                </asp:DropDownList>  
            </EditItemTemplate>  
            <FooterTemplate>  
                <asp:DropDownList ID="nddl_RC_CP" runat="server" Width="70px" >
                    <asp:ListItem Value=""> - </asp:ListItem>
                    <asp:ListItem Value="N"> N </asp:ListItem>
                    <asp:ListItem Value="Y"> Y </asp:ListItem>
                </asp:DropDownList>  
            </FooterTemplate>
        </asp:TemplateField>                    

        <asp:TemplateField HeaderText="Non-CP Owned">  
        <ItemStyle HorizontalAlign="Center"></ItemStyle>
            <ItemTemplate>  
                <asp:Label ID="lbl_Non_CP" runat="server" Width="70px" Text='<%#Eval("Non_CP") %>'></asp:Label>  
            </ItemTemplate>  
            <EditItemTemplate>  
                <asp:DropDownList ID="ddl_RC_Non_CP" runat="server" Width="70px" Text='<%#Eval("Non_CP") %>'>
                    <asp:ListItem Value=""> - </asp:ListItem>
                    <asp:ListItem Value="N"> N </asp:ListItem>
                    <asp:ListItem Value="Y"> Y </asp:ListItem>
                </asp:DropDownList>  
            </EditItemTemplate>  
            <FooterTemplate>  
                <asp:DropDownList ID="nddl_RC_Non_CP" runat="server" Width="70px" >
                    <asp:ListItem Value=""> - </asp:ListItem>
                    <asp:ListItem Value="N"> N </asp:ListItem>
                    <asp:ListItem Value="Y"> Y </asp:ListItem>
                </asp:DropDownList>  
            </FooterTemplate>
        </asp:TemplateField> 

        <asp:TemplateField HeaderText="Proclaim">  
        <ItemStyle HorizontalAlign="Center"></ItemStyle>
            <ItemTemplate>  
                <asp:Label ID="lbl_Proclaim" runat="server" Width="70px" Text='<%#Eval("Proclaim") %>'></asp:Label>  
            </ItemTemplate>  
            <EditItemTemplate>  
                <asp:DropDownList ID="ddl_RC_Proclaim" runat="server" Width="70px" Text='<%#Eval("Proclaim") %>'>
                    <asp:ListItem Value=""> - </asp:ListItem>
                    <asp:ListItem Value="N"> N </asp:ListItem>
                    <asp:ListItem Value="Y"> Y </asp:ListItem>
                </asp:DropDownList>  
            </EditItemTemplate>  
            <FooterTemplate>  
                <asp:DropDownList ID="nddl_RC_Proclaim" runat="server" Width="70px" >
                    <asp:ListItem Value=""> - </asp:ListItem>
                    <asp:ListItem Value="N"> N </asp:ListItem>
                    <asp:ListItem Value="Y"> Y </asp:ListItem>
                </asp:DropDownList>  
            </FooterTemplate>
        </asp:TemplateField> 

        <asp:TemplateField HeaderText="PMHS">  
        <ItemStyle HorizontalAlign="Center"></ItemStyle>
            <ItemTemplate>  
                <asp:Label ID="lbl_PMHS" runat="server" Width="70px" Text='<%#Eval("PMHS") %>'></asp:Label>  
            </ItemTemplate>  
            <EditItemTemplate>  
                <asp:DropDownList ID="ddl_RC_PMHS" runat="server" Width="70px" Text='<%#Eval("PMHS") %>'>
                    <asp:ListItem Value=""> - </asp:ListItem>
                    <asp:ListItem Value="N"> N </asp:ListItem>
                    <asp:ListItem Value="Y"> Y </asp:ListItem>
                </asp:DropDownList>  
            </EditItemTemplate>  
            <FooterTemplate>  
                <asp:DropDownList ID="nddl_RC_PMHS" runat="server" Width="70px" >
                    <asp:ListItem Value=""> - </asp:ListItem>
                    <asp:ListItem Value="N"> N </asp:ListItem>
                    <asp:ListItem Value="Y"> Y </asp:ListItem>
                </asp:DropDownList>  
            </FooterTemplate>
        </asp:TemplateField> 

        <asp:TemplateField HeaderText="Facets">  
        <ItemStyle HorizontalAlign="Center"></ItemStyle>
            <ItemTemplate>  
                <asp:Label ID="lbl_Facets" runat="server" Width="70px" Text='<%#Eval("Facets") %>'></asp:Label>  
            </ItemTemplate>  
            <EditItemTemplate>  
                <asp:DropDownList ID="ddl_RC_Facets" runat="server" Width="70px" Text='<%#Eval("Facets") %>'>
                    <asp:ListItem Value=""> - </asp:ListItem>
                    <asp:ListItem Value="N"> N </asp:ListItem>
                    <asp:ListItem Value="Y"> Y </asp:ListItem>
                </asp:DropDownList>  
            </EditItemTemplate>  
            <FooterTemplate>  
                <asp:DropDownList ID="nddl_RC_Facets" runat="server" Width="70px" >
                    <asp:ListItem Value=""> - </asp:ListItem>
                    <asp:ListItem Value="N"> N </asp:ListItem>
                    <asp:ListItem Value="Y"> Y </asp:ListItem>
                </asp:DropDownList>  
            </FooterTemplate>
        </asp:TemplateField> 

        <asp:TemplateField ItemStyle-Width="90px">  
        <ItemTemplate>  
            <asp:Button ID="btn_Edit" runat="server" Text="Edit" CommandName="Edit" />  
        </ItemTemplate>  
        <EditItemTemplate>  
            <asp:Button ID="btn_Update" runat="server" Text="Update" CommandName="Update"/>  
            <asp:Button ID="btn_Cancel" runat="server" Text="Cancel" CommandName="Cancel"/>  
        </EditItemTemplate>  
        <FooterTemplate>
            <asp:Button ID="btn_Add" runat="server" Text="Add" OnClick="DataGrid_RootCauses_RowCreated" />
        </FooterTemplate>
        </asp:TemplateField>  
        <asp:TemplateField>  
        <ItemTemplate>  
            <asp:Button ID="btn_Delete" runat="server" OnClientClick="javascript:return confirm('Are you sure?');" Text="Delete" CommandName="Delete" />  
        </ItemTemplate>  
        </asp:TemplateField>        
        <asp:TemplateField HeaderText="Cause ID">  
        <ItemTemplate>  
            <asp:Label ID="lbl_Cause_ID" runat="server" Text='<%#Eval("root_cause_id") %>'></asp:Label>  
        </ItemTemplate>  
        </asp:TemplateField>        

        </Columns>  
    </asp:GridView>  
    <asp:Label ID="lblEmpty1" runat="server" Visible="false" Style="font-weight:bold; font-size:large;"></asp:Label>
    </div> 
</asp:Panel>

现在,代码隐藏:

protected void  ddlRootCauses_SelectedIndexChanged(object sender, EventArgs e)
{
    LoadRootCauseGrid();

    OracleConnection conn = GetConnection();
    try
    {
        {
            // ddlRootCauses
            OracleCommand cmd6 = new OracleCommand();
            cmd6.CommandType = CommandType.StoredProcedure;
            cmd6.CommandText = "SP_LPI_MAINT_LIST_ROOTCAUSE";
            cmd6.Connection = conn;

            //cmd3.Parameters.Add("v_Lookup_Value", OracleType.VarChar, 20).Value = "LEAD";
            cmd6.Parameters.Add("vRootCauseList", OracleType.Cursor).Direction = ParameterDirection.Output;

            var SearchAdapter6 = new OracleDataAdapter(cmd6);
            var ds6 = new DataTable();
            SearchAdapter6.Fill(ds6);

            DataView view = new DataView(ds6);
            DataTable distinctValues = new DataTable();
            distinctValues = view.ToTable(true, "LPI_Category");

            // Fill all of the dropdowns from the same data adapter
            {
                //This one works fine
                ddlRootCauses.DataSource = distinctValues;
                ddlRootCauses.DataTextField = "LPI_Category";
                ddlRootCauses.DataValueField = "LPI_Category";
                ddlRootCauses.DataBind();
                ddlRootCauses.Items.Insert(0, new ListItem("All", ""));

                //This one send me down to the Catch block on the first line
                ddl_RC_LPI_Category.DataSource = distinctValues;
                ddl_RC_LPI_Category.DataTextField = "LPI_Category";
                ddl_RC_LPI_Category.DataValueField = "LPI_Category";
                ddl_RC_LPI_Category.DataBind();
                ddl_RC_LPI_Category.Items.Insert(0, new ListItem("All", ""));

                nddl_RC_LPI_Category.DataSource = distinctValues;
                nddl_RC_LPI_Category.DataTextField = "LPI_Category";
                nddl_RC_LPI_Category.DataValueField = "LPI_Category";
                nddl_RC_LPI_Category.DataBind();
                nddl_RC_LPI_Category.Items.Insert(0, new ListItem("All", ""));

            }

            ds6.Dispose();
            cmd6.Dispose();
            SearchAdapter6.Dispose();

            conn.Close();
        }
    }
    catch (Exception ex)
    {
            Response.Write(ex.Message);
            conn.Close();
    }

}

奇怪的是,ddlRootCauses 的填充没有问题。但是,该下拉列表在网格视图之外。我不明白为什么网格视图中的下拉列表有问题。 而且,这不仅仅是因为 gridviews 不喜欢下拉菜单;带有硬编码列表项的 Y/N 可以正常工作。

【问题讨论】:

  • 您是否尝试从 ddlRootCauses_SelectedIndexChanged 事件填充网格下拉列表?
  • 是的。 ddlRootCauses 是一个下拉菜单,用作网格视图的过滤器。选择它并过滤数据后,下拉菜单 ddl_RC_LPI_Category 和 nddl_RC_LPI_Category 应仅填充适当的项目,因此一旦更改 ddlRootCauses 就需要重新填充它们。

标签: c# asp.net gridview drop-down-menu


【解决方案1】:

由于控件位于 GridView 内部,因此您无法像访问外部的 Contol 那样访问它们(例如 ddlRootCauses

要在 GridView 中填充 DropDownList,您需要先使用 FindControl 找到它们。

protected void ddlRootCauses_SelectedIndexChanged(object sender, EventArgs e)
{
    //find the dropdownlist in the footer row
    DropDownList ddl = DataGrid_RootCauses.FooterRow.FindControl("nddl_RC_LPI_Category") as DropDownList;

    //or in a normal row you need to use an index
    DropDownList ddl = DataGrid_RootCauses.Rows[5].FindControl("ddl_RC_LPI_Category") as DropDownList;

    ddl.DataSource = distinctValues;
    ddl.DataTextField = "LPI_Category";
    ddl.DataValueField = "LPI_Category";
    ddl.DataBind();
    ddl.Items.Insert(0, new ListItem("All", ""));
}

RowDataBound 示例

首先是aspx

<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" ShowFooter="true">
    <Columns>
    <asp:TemplateField HeaderText="Root Cause Reason">  
        <ItemTemplate>  
            <asp:DropDownList ID="DropDownListItem" runat="server"></asp:DropDownList>
        </ItemTemplate>  
        <EditItemTemplate>  
            <asp:DropDownList ID="DropDownListEditItem" runat="server"></asp:DropDownList> 
        </EditItemTemplate>  
        <FooterTemplate>  
            <asp:DropDownList ID="DropDownListFooter" runat="server"></asp:DropDownList>
        </FooterTemplate>
    </asp:TemplateField>  
    </Columns>
</asp:GridView>

以及背后的代码

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        //the header row
    }
    else if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //normal row
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
            //normal row in edit mode
            DropDownList ddl = e.Row.FindControl("DropDownListEditItem") as DropDownList;

            ddl.DataSource = distinctValues;
            ddl.DataTextField = "myValue";
            ddl.DataValueField = "ID";
            ddl.DataBind();
        }
        else
        {
            //normal row
            DropDownList ddl = e.Row.FindControl("DropDownListItem") as DropDownList;

            ddl.DataSource = distinctValues;
            ddl.DataTextField = "myValue";
            ddl.DataValueField = "ID";
            ddl.DataBind();
        }
    }
    else if (e.Row.RowType == DataControlRowType.Footer)
    {
        //footer row
        DropDownList ddl = e.Row.FindControl("DropDownListFooter") as DropDownList;

        ddl.DataSource = distinctValues;
        ddl.DataTextField = "myValue";
        ddl.DataValueField = "ID";
        ddl.DataBind();
    }
}

或在按钮中单击。

protected void Button1_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddl = row.FindControl("DropDownListItem") as DropDownList;

            ddl.DataSource = Common.LoadFromDB();
            ddl.DataTextField = "field01";
            ddl.DataValueField = "itemID";
            ddl.DataBind();
        }
    }
}

【讨论】:

  • 仍然得到同样的错误。而且,其中一个问题是我永远不知道他们将选择哪一行来编辑,因此硬编码 Rows[5] 不会是最优的。前端只有一个控件; ddl_RC_LPI_Category。我真的需要选择一行吗?有没有办法选择控件,以便返回数据的所有记录都有一个下拉列表,其中包含相同的项目?顺便说一句,页脚行的代码运行良好。
  • 您可能会收到错误消息,因为ddl_RC_LPI_Category 在编辑模板中。但是如果你想要所有的下拉菜单,你将不得不循环这些行或使用 OnRowDataBound 事件来填充它们。
  • 您能否编辑您的答案以显示如何完成?
  • 更新了我的答案。
猜你喜欢
  • 1970-01-01
  • 2013-08-14
  • 2019-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多