诸如Eval()、XPath()和Bind()之类的数据绑定方法只能在数据绑定控件的上下文中使用
本文关键字:数据绑定 控件 上下文 方法 Bind XPath Eval 诸如 | 更新日期: 2023-09-27 17:49:20
我在GridView中有5个下拉列表。我将网格与ObjectDataSource绑定,并将属于GridView的下拉列表与5个ObjectDataSource绑定。一切正常。我在c#中捕获第一个下拉列表SelectedIndexChanged事件,我想绑定第二个下拉列表。下面是我用来绑定第二个下拉列表的代码:
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(),只能在数据绑定控件的上下文中使用。
谁能告诉我怎样才能摆脱这个问题?我是否需要处理网格的另一个事件来正确绑定下拉菜单?此外,我希望网格行仍然在编辑模式后绑定下拉。终于找到问题了。我删除了GridView周围的UpdatePanel并调试代码。我发现我的更新函数得到空值除了id值。然后我看到了gridview,发现我已经选择了使用Eval函数的网格中的每个下拉列表值,该函数用于单向数据绑定(只读)。我将它替换为Bind函数,它开始正常工作。