C# GridView中的DropDownList未被视为对象
我搞不清楚我做错了什么。尝试动态填充gridview中的dropdownlist,它不断告诉我对象引用未设置为对象的实例 首先,我的ASPX:C# GridView中的DropDownList未被视为对象,c#,asp.net,gridview,drop-down-menu,C#,Asp.net,Gridview,Drop Down Menu,我搞不清楚我做错了什么。尝试动态填充gridview中的dropdownlist,它不断告诉我对象引用未设置为对象的实例 首先,我的ASPX: <asp:Panel runat="server" ID="ShowDiv3" Visible="false" BorderStyle="Solid" BorderWidth="0" Width="1389px"> <asp:Label ID="lblShowDiv3Title" runat="server" Text
<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>
奇怪的是,根本原因没有问题。但是,dropdownlist在gridview之外。我不明白为什么gridview中的dropdownlist会有问题。而且,这不仅仅是因为GridView不喜欢下拉菜单;带有硬编码列表项的Y/N控件工作正常。由于控件位于GridView内,因此无法像访问控件外的控件那样访问它们(如
ddlRootCause
)
要在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", ""));
}
行数据绑定示例
首先是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 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();
}
}
}
您正在尝试填充ddlRootCauses\u SelectedIndexChanged事件中的网格下拉列表吗?是。ddlRootCauss是一个下拉列表,用作gridview的过滤器。一旦选择并过滤数据,ddl_RC_LPI_类别和nddl_RC_LPI_类别的下拉列表应仅填充适当的项目,因此在ddl根原因更改后需要重新填充。仍然会出现相同的错误。还有一个问题是,我永远不知道他们将选择编辑哪一行,所以硬编码行[5]并不是最优的。前端只有一个控件;ddl_RC_LPI_类别。我真的需要挑一排吗?是否无法选择控件以使返回数据的所有记录都有一个包含相同项的dropdownlist?顺便说一句,页脚行的代码工作得很好。您可能会收到错误,因为编辑模板中有
ddl\u RC\u LPI\u Category
。但是,如果您需要所有下拉列表,则必须循环行或使用OnRowDataBound事件填充行。您是否可以编辑您的答案以显示如何完成此操作?更新了我的答案。
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();
}
}
}