ASP.NET多个数据绑定下拉列表问题

时间:2012-11-07 02:52:52

标签: asp.net exception gridview drop-down-menu

我有一个跟踪计算机资产的ASP.NET网站,特别是一个页面让用户更新数据。该页面包含GridView的Sqldatasource对象,以及GridView编辑模式中的每个下拉列表。此页面工作得很漂亮,直到我将一个字段从打开文本更改为下拉列表,其中下拉列表中的项目取决于另一列的选定项目。根据记录,有一个“网站”和“房间”列,房间的下拉列表将根据所选网站进行更改。我还需要在下拉列表的顶部添加一个空白项以支持新/空记录。为了支持空记录,我将room列的下拉列表更改为AppendDataBoundItems = true,以便我可以添加一个空白项,然后在sqldatasource中检索的项被加载。这个实现的问题是,随着站点下拉列表的每次更改(以及它的回发),新的房间号码列表将被附加到房间下拉列表中,这是不希望的,列表应该被清除并在每次更改时重新加载。网站专栏。为了解决这个问题,我添加了以下代码:

protected void DropDownListSite_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList drop = (DropDownList)GridView1.Rows[GridView1.EditIndex].FindControl("DropDownListSite");
        Session["atSite"] = drop.SelectedItem.Text;
        DropDownList roomdrop = (DropDownList)GridView1.Rows[GridView1.EditIndex].FindControl("DropDownListRoom");
        roomdrop.Items.Clear();
        roomdrop.Items.Add(string.Empty);
    }

此外,当房间下拉列表的值为数据绑定时,更改站点列将导致异常:

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Stack Trace:
[InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.]
System.Web.UI.Page.GetDataItem() +2642054
ASP.viewassets_aspx.__DataBinding__control28(Object sender, EventArgs e) in <redacted path>\Temporary ASP.NET Files\root\81f582e0\4392b2e5\App_Web_kyt9rnda.0.cs:0
System.Web.UI.Control.OnDataBinding(EventArgs e) +99
System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +14
System.Web.UI.WebControls.ListControl.PerformSelect() +34
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
System.Web.UI.WebControls.BaseDataBoundControl.OnPreRender(EventArgs e) +22
System.Web.UI.WebControls.ListControl.OnPreRender(EventArgs e) +18
System.Web.UI.Control.PreRenderRecursiveInternal() +80
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842

我认为这意味着主页面线程崩溃了,因为房间下拉列表的选定值不再是列表中的项目。

以下是gridview的代码:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        CellPadding="3" DataSourceID="SqlDataSource1" ForeColor="#333333" 
        HorizontalAlign="Center" onrowcreated="GridView1_RowCreated" 
        AllowSorting="True" 
        onselectedindexchanged="GridView1_SelectedIndexChanged" 
        style="text-align: center" onprerender="GridView1_PreRender" 
            onrowdatabound="GridView1_RowDataBound" DataKeyNames="name" 
            EnableModelValidation="True" onrowupdated="GridView1_RowUpdated" 
            onrowediting="GridView1_RowEditing" AllowPaging="True">
        <RowStyle BackColor="#EFF3FB" />
        <Columns>
            <asp:CommandField SelectText="Pop-&gt;" ShowEditButton="True" Visible="False" >
            <ItemStyle ForeColor="Blue" />
            </asp:CommandField>
            <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" 
                ReadOnly="True" />
            <asp:BoundField DataField="model" HeaderText="model" SortExpression="model" 
                ReadOnly="True" Visible="False" />
            <asp:BoundField DataField="Age" HeaderText="Age" ReadOnly="True" 
                SortExpression="Age" Visible="False" />
            <asp:BoundField DataField="Last Check-in" HeaderText="Last Check-in" 
                SortExpression="Last Check-in" ReadOnly="True" Visible="False" />
            <asp:BoundField DataField="ip" HeaderText="ip" SortExpression="ip" 
                ReadOnly="True" Visible="False" />
            <asp:TemplateField HeaderText="site" SortExpression="site">
                <EditItemTemplate>
                    <asp:Panel ID="Panel1" runat="server">
                        <asp:DropDownList ID="DropDownListSite" runat="server" AppendDataBoundItems="True" 
                            DataSourceID="SqlDataSourceSiteEdit" DataTextField="school" 
                            DataValueField="school" SelectedValue='<%# Bind("Site") %>' 
                            AutoPostBack="True" 
                            onselectedindexchanged="DropDownListSite_SelectedIndexChanged">
                            <asp:ListItem></asp:ListItem>
                        </asp:DropDownList>
                        <br />
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" CssClass="EditValidator" 
                            ErrorMessage="Required field" ControlToValidate="DropDownListSite"></asp:RequiredFieldValidator>
                    </asp:Panel>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("site") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="room" SortExpression="room">
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownListRoom" runat="server" 
                        DataSourceID="SqlDataSourceRooms" DataTextField="roomName" 
                        DataValueField="roomName" SelectedValue='<%# Bind("room") %>'
                        onselectedindexchanged="DropDownListRoom_SelectedIndexChanged" 
                        AppendDataBoundItems="True" ondatabound="DropDownListRoom_DataBound" 
                        onprerender="DropDownListRoom_PreRender">
                        <asp:ListItem></asp:ListItem>
                    </asp:DropDownList>
                    <br />
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" 
                        ControlToValidate="DropDownListRoom" CssClass="EditValidator" 
                        ErrorMessage="Required field"></asp:RequiredFieldValidator>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label6" runat="server" Text='<%# Bind("room") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Function" SortExpression="Function">
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownList3" runat="server" 
                        SelectedValue='<%# Bind("Function") %>'>
                        <asp:ListItem></asp:ListItem>
                        <asp:ListItem>Desktop</asp:ListItem>
                        <asp:ListItem>Laptop</asp:ListItem>
                    </asp:DropDownList>
                    <br />
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                        ControlToValidate="DropDownList3" CssClass="EditValidator" 
                        ErrorMessage="Required"></asp:RequiredFieldValidator>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Function") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Local Printer" 
                SortExpression="Local Printer Type">
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownList4" runat="server" 
                        SelectedValue='<%# Bind("[Local Printer Type]") %>'>
                        <asp:ListItem></asp:ListItem>
                        <asp:ListItem>None</asp:ListItem>
                        <asp:ListItem>InkJet</asp:ListItem>
                        <asp:ListItem>Laser</asp:ListItem>
                        <asp:ListItem>Impact</asp:ListItem>
                    </asp:DropDownList>
                    <br />
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
                        ControlToValidate="DropDownList4" CssClass="EditValidator" 
                        ErrorMessage="Required"></asp:RequiredFieldValidator>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" 
                        Text='<%# Bind("[Local Printer Type]") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Location Type" SortExpression="Location Type">
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownList5" runat="server" 
                        SelectedValue='<%# Bind("[Location Type]") %>'>
                        <asp:ListItem></asp:ListItem>
                        <asp:ListItem>Classroom</asp:ListItem>
                        <asp:ListItem>Office</asp:ListItem>
                        <asp:ListItem>Media Center</asp:ListItem>
                        <asp:ListItem>Mobile Lab</asp:ListItem>
                        <asp:ListItem>Fixed Lab</asp:ListItem>
                    </asp:DropDownList>
                    <br />
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" 
                        ControlToValidate="DropDownList5" CssClass="EditValidator" 
                        ErrorMessage="Required"></asp:RequiredFieldValidator>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("[Location Type]") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Primary Use" SortExpression="Primary Use">
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownList6" runat="server" 
                        SelectedValue='<%# Bind("[Primary Use]") %>'>
                        <asp:ListItem></asp:ListItem>
                        <asp:ListItem>Admin</asp:ListItem>
                        <asp:ListItem>Student</asp:ListItem>
                        <asp:ListItem>Teacher</asp:ListItem>
                    </asp:DropDownList>
                    <br />
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" 
                        ControlToValidate="DropDownList6" CssClass="EditValidator" 
                        ErrorMessage="Required"></asp:RequiredFieldValidator>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label5" runat="server" Text='<%# Bind("[Primary Use]") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Comments" SortExpression="Comments">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Comments") %>'></asp:TextBox>
                    <br />
                    &nbsp;
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label7" runat="server" Text='<%# Bind("Comments") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Last Updated" HeaderText="Last Updated" 
                SortExpression="Last Updated" ReadOnly="True" />
            <asp:BoundField DataField="FA_Number" HeaderText="FA_Number" 
                SortExpression="FA_Number" ReadOnly="True" />
            <asp:BoundField DataField="Funding_Source" HeaderText="Funding_Source" 
                SortExpression="Funding_Source" ReadOnly="True" />
        </Columns>
    </asp:GridView>

所以我的问题是:如何根据所选站点对房间列动态的数据源进行数据处理,并且当站点列发生更改时,它将默认为列表中的空白(索引0)项目和选定值不再是列表中的项目了吗?

如果这是漫长的啰嗦,或者我遗漏了一些重要的事情,请提前道歉,请随时提出问题以便澄清。

提前致谢:)

0 个答案:

没有答案