我無法弄清楚我在這裏做錯了什麼。試圖動態地填充GridView中的下拉列表,並一直告訴我對象引用未設置爲對象的實例。GridView內的DropDownList不被視爲對象
首先,我的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得到沒有問題的填充。但是,該下拉列表不在gridview中。我不明白爲什麼它在gridview中的下拉列表中遇到問題。 和,它不僅僅是因爲gridviews不喜歡下拉菜單;硬編碼列表項的Y/N編碼器工作正常。
你是否嘗試從ddlRootCauses_SelectedIndexChanged事件填充網格的下拉列表? –
是的。 ddlRootCauses是一個充當gridview過濾器的下拉菜單。一旦選擇了數據並對數據進行了過濾,下拉菜單ddl_RC_LPI_Category和nddl_RC_LPI_Category只能填充適當的項目,因此一旦ddlRootCauses更改就需要重新填充它們。 –