我在 GridView 中有 5 个下拉列表。我将网格与 ObjectDataSource 绑定,并将属于 GridView 的 DropDownLists 与 5 个 ObjectDataSource 绑定。每件事都运行良好。我在 C# 中捕获了第一个 DropDownList SelectedIndexChanged 事件,我想绑定第二个 DropDownList。这是我用来绑定第二个下拉列表的代码:
protected void Level1DropdownList_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList level1List = sender as DropDownList;
if (level1List != null)
{
if (level1List.SelectedValue != "0")
{
// Bind Level 2 List
GridViewRow currentRow = AnnouncementLevelsGrid.Rows[AnnouncementLevelsGrid.EditIndex];
if (currentRow != null)
{
DropDownList level2List = currentRow.FindControl("Level2DropdownList") as DropDownList;
if (level2List != null)
{
level2List.DataSource = AnnouncementHelper.GetLevel2(level1List.SelectedValue, EmployerHelper.EmployerID);
level2List.DataTextField = "Level2";
level2List.DataValueField = "Level2Value";
level2List.DataBind();
}
}
}
}
}
这是 asp.net 代码示例。
<asp:GridView ID="AnnouncementLevelsGrid" runat="server" Width="99%" DataSourceID="AnnouncementLevelsDataSource"
CssClass="Grid" AutoGenerateColumns="False" GridLines="None" DataKeyNames="AnnouncementLevelID,AnnouncementId"
ViewStateMode="Enabled" OnRowCommand="AnnouncementLevelsGrid_RowCommand" OnRowDataBound="AnnouncementLevelsGrid_RowDataBound"
OnRowUpdating="AnnouncementLevelsGrid_RowUpdating">
<HeaderStyle CssClass="GridHeader" />
<RowStyle CssClass="GridRow" />
<AlternatingRowStyle CssClass="GridAlternating" />
<Columns>
<asp:TemplateField HeaderText="Level 1">
<ItemTemplate>
<asp:Label ID="Level1Label" runat="server" Text='<%# Eval("Level1").ToString() == "0" ? "ALL" : Eval("Level1") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="Level1DropdownList" runat="server" AppendDataBoundItems="True"
DataTextField="Level1" DataValueField="Level1Value" SelectedValue='<%# Eval("Level1") %>'
AutoPostBack="True" DataSourceID="Level1DataSource" OnSelectedIndexChanged="Level1DropdownList_SelectedIndexChanged">
<asp:ListItem Text="All" Value="0"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<HeaderStyle HorizontalAlign="Center" Width="18%" />
<ItemStyle HorizontalAlign="Center" Width="18%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Level 2">
<ItemTemplate>
<asp:Label ID="Level2Label" runat="server" Text='<%# Eval("Level2").ToString() == "0" ? "ALL" : Eval("Level2") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="Level2DropdownList" runat="server" AppendDataBoundItems="True"
DataTextField="Level2" DataValueField="Level2Value" SelectedValue='<%# Eval("Level2") %>'
AutoPostBack="True" DataSourceID="Level2DataSource" OnSelectedIndexChanged="Level2DropdownList_SelectedIndexChanged">
<asp:ListItem Text="All" Value="0"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<HeaderStyle HorizontalAlign="Center" Width="18%" />
<ItemStyle HorizontalAlign="Center" Width="18%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Level 3">
<ItemTemplate>
<asp:Label ID="Level3Label" runat="server" Text='<%# Eval("Level3").ToString() == "0" ? "ALL" : Eval("Level3") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="Level3DropdownList" runat="server" AppendDataBoundItems="True"
DataTextField="Level3" DataValueField="Level3Value" SelectedValue='<%# Eval("Level3") %>'
AutoPostBack="True" DataSourceID="Level3DataSource" OnSelectedIndexChanged="Level3DropdownList_SelectedIndexChanged">
<asp:ListItem Text="All" Value="0"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<HeaderStyle HorizontalAlign="Center" Width="18%" />
<ItemStyle HorizontalAlign="Center" Width="18%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Level 4">
<ItemTemplate>
<asp:Label ID="Level4Label" runat="server" Text='<%# Eval("Level4").ToString() == "0" ? "ALL" : Eval("Level4") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="Level4DropdownList" runat="server" AppendDataBoundItems="True"
DataTextField="Level4" DataValueField="Level4Value" SelectedValue='<%# Eval("Level4") %>'
AutoPostBack="True" DataSourceID="Level4DataSource" OnSelectedIndexChanged="Level4DropdownList_SelectedIndexChanged">
<asp:ListItem Text="All" Value="0"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<HeaderStyle HorizontalAlign="Center" Width="18%" />
<ItemStyle HorizontalAlign="Center" Width="18%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Level 5">
<ItemTemplate>
<asp:Label ID="Level5Label" runat="server" Text='<%# Eval("Level5").ToString() == "0" ? "ALL" : Eval("Level5") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="Level5DropdownList" runat="server" AppendDataBoundItems="True"
DataTextField="Level5" DataValueField="Level5Value" SelectedValue='<%# Eval("Level5") %>'
DataSourceID="Level5DataSource">
<asp:ListItem Text="All" Value="0"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<HeaderStyle HorizontalAlign="Center" Width="18%" />
<ItemStyle HorizontalAlign="Center" Width="18%" />
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="EditLink" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit" ToolTip="Edit"><img src="images/edit.png" alt="Edit" style="border-width: 0px;" /></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="UpdateLink" runat="server" CausesValidation="True" CommandName="Update"
Text="Update" ToolTip="Update"><img src="images/Update-Grd.png" alt="Update" style="border-width: 0px;" /></asp:LinkButton>
<asp:LinkButton ID="CancelLink" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel" ToolTip="Cancel"><img src="images/Cancel-Grd.png" alt="Cancel" style="border-width: 0px;" /></asp:LinkButton>
</EditItemTemplate>
<HeaderStyle HorizontalAlign="Center" Width="10%" />
<ItemStyle HorizontalAlign="Center" Width="10%" />
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
No Announcements Found.
</EmptyDataTemplate>
</asp:GridView>
<asp:ObjectDataSource ID="AnnouncementLevelsDataSource" runat="server" SelectMethod="GetAnnouncementLevels"
TypeName="LinkManagement.DataAccess.AnnouncementHelper" OnSelecting="AnnouncementLevelsDataSource_Selecting">
<SelectParameters>
<asp:Parameter Name="announcementID" DbType="Int64" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="Level1DataSource" runat="server" SelectMethod="GetLevel1"
TypeName="LinkManagement.DataAccess.AnnouncementHelper" OnSelecting="Level1DataSource_Selecting">
<SelectParameters>
<asp:Parameter Name="employerID" DbType="Int64" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="Level2DataSource" runat="server" SelectMethod="GetLevel2"
TypeName="LinkManagement.DataAccess.AnnouncementHelper" OnSelecting="Level2DataSource_Selecting">
<SelectParameters>
<asp:Parameter Name="employerID" DbType="Int64" />
<asp:Parameter Name="cc1" DbType="String" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="Level3DataSource" runat="server" SelectMethod="GetLevel3"
TypeName="LinkManagement.DataAccess.AnnouncementHelper" OnSelecting="Level3DataSource_Selecting">
<SelectParameters>
<asp:Parameter Name="employerID" DbType="Int64" />
<asp:Parameter Name="cc2" DbType="String" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="Level4DataSource" runat="server" SelectMethod="GetLevel4"
TypeName="LinkManagement.DataAccess.AnnouncementHelper" OnSelecting="Level4DataSource_Selecting">
<SelectParameters>
<asp:Parameter Name="employerID" DbType="Int64" />
<asp:Parameter Name="cc3" DbType="String" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="Level5DataSource" runat="server" SelectMethod="GetLevel5"
TypeName="LinkManagement.DataAccess.AnnouncementHelper" OnSelecting="Level5DataSource_Selecting">
<SelectParameters>
<asp:Parameter Name="employerID" DbType="Int64" />
<asp:Parameter Name="cc4" DbType="String" />
</SelectParameters>
</asp:ObjectDataSource>
当我在网格视图中单击编辑按钮时,所有下拉列表都会以编辑模式呈现它们。他们也正确地填充数据。现在,当我更改第一个下拉列表项以填充第二个时,它会引发以下异常:
Eval()、XPath() 和 Bind() 等数据绑定方法只能在数据绑定控件的上下文中使用。
任何人都可以告诉我如何摆脱这个问题。我是否需要处理另一个网格事件才能正确绑定下拉列表?我还希望在绑定下拉列表后网格行仍处于编辑模式。