“数据绑定方法(如Eval(),XPath()和Bind())只能在数据绑定控件的上下文中使用。”

时间:2012-12-24 13:11:16

标签: c# asp.net

我在GridView中有5个DropDownLists。我使用ObjectDataSource绑定网格,并使用5个ObjectDataSources绑定属于GridView的DropDownLists。一切都很好。我在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>
                                                                &nbsp;<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())只能在数据绑定控件的上下文中使用。

任何人都可以告诉我如何摆脱这个问题。我是否需要处理另一个网格事件才能正确绑定下拉列表?此外,我希望在绑定下拉列表后网格行仍处于编辑模式。

1 个答案:

答案 0 :(得分:0)

最后我能找到问题所在。我删除了围绕GridView的UpdatePanel并调试代码。我发现我的更新函数除了id值以外都是null值。然后我看到gridview并发现我使用Eval函数选择了网格中的每个下拉列表值,该函数用于单向数据绑定(Readonly)。我用Bind函数替换它,它开始正常工作。