我有一个跟踪计算机资产的 ASP.NET 网站,特别是一页让用户更新数据。这个页面有一个 GridView 的 Sqldatasource 对象,以及 GridView 的编辑模式下的每个下拉列表。此页面运行良好,直到我将一个字段从打开的文本更改为下拉列表,其中下拉列表中的项目取决于另一列的选定项目。根据记录,“站点”和“房间”有一列,房间的下拉列表将根据所选站点而变化。我还需要在下拉列表顶部添加一个空白项以支持新/空记录。为了支持空记录,我将房间列的下拉列表更改为 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->" 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 />
</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)项列表中的项目?
如果这是冗长的,或者我遗漏了一些重要的东西,我提前道歉,请随时提出问题以进行澄清。
提前致谢 :)