[英]Populating a dropdown list from a database with selected items then the rest in alphabetical order
[英]Populating a dropdown from the database with a selected value (hidden column)
我有一个控件正在不同的页面上使用,但是在一个特定的页面上,我出现了一个下拉菜单,允许用户更改数据库中的某个字段。 下拉菜单有两个项目(“出勤日”和“非出勤日”),我希望在访问控件时加载数据库中的任何选项。
这是我的网格视图的顶部:
<asp:GridView runat="server" DataSourceID="sdsLookups" ID="gvValues"
AutoGenerateColumns="False" Width="760px" OnDataBound ="GridView_DataBound">
这是“网格视图”中的实际下拉列表。 在正确的页面上,从数据库中加载了正确的值:
<asp:TemplateField HeaderText="Is Attendance?">
<ItemTemplate>
<asp:DropDownList id="ddlAttendanceStatus" AutoPostBack="True" runat ="server" SelectedValue='<%# Eval("rules") %>' >
<asp:ListItem>Attendance Day</asp:ListItem>
<asp:ListItem>Not Attendance Day</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
这是我当前用于隐藏列的代码。 我还试图检查该项目是否为空,但是我还没有开始工作:
protected void GridView_DataBound(object sender, EventArgs e)
{
if (this.LookupType == "Day Status" ? true : false)
{
gvValues.Columns[12].Visible = true;
GridViewRow row = (sender as Control).Parent.Parent as GridViewRow;
DropDownList rules = (row.FindControl("ddlAttendanceStatus") as DropDownList);
ListItem item = rules.Items.FindByText("Attendance Day");
if (item != null)
rules.Items.FindByText("Attendance Day").Selected = true;
}
}
当前,该列仅显示在正确的页面上,但是当我转到使用此控件的其他页面时,出现错误消息:
ArgumentOutOfRangeException: 'ddlAttendanceStatus' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value
我知道为什么会收到此错误,但是我不确定如何解决此问题。 如果我不在正确的页面上,是否可以完全忽略此字段? 还是我应该走另一条路?
如果需要发布更多信息,请告诉我。
先感谢您。
问题是aspx页面上的这段代码: SelectedValue='<%# Eval("rules") %>'
。 在其他页面中,如果不存在rules
或rules
的值不是“ Attendance Day
或“ Not Attendance Day
,则将收到该错误。 您可以通过在OnRowDataBound
事件中设置SelectedValue
来解决此问题。
首先将aspx上的GridView更改为
<asp:GridView runat="server" DataSourceID="sdsLookups" ID="gvValues"
AutoGenerateColumns="False" Width="760px" OnDataBound="GridView_DataBound"
OnRowDataBound="gvValues_RowDataBound">
然后在后面的代码中
protected void gvValues_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the row is a datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
//cast the row back to a datarowview
DataRowView row = e.Row.DataItem as DataRowView;
//find the dropdownlist with findcontrol
DropDownList rules = e.Row.FindControl("ddlAttendanceStatus") as DropDownList;
if (row["myColumn"].ToString() == "Day Status")
{
//set the correct selectedvalue
rules.SelectedValue = "Not Attendance Day";
//or
rules.SelectedValue = "1";
}
}
}
最后,作为一种选择,我建议您将键/值用作ListItems。 这使得处理长字符串更加容易。
<asp:DropDownList ID="ddlAttendanceStatus" AutoPostBack="True" runat="server">
<asp:ListItem Text="Attendance Day" Value="0"></asp:ListItem>
<asp:ListItem Text="Not Attendance Day" Value="1"></asp:ListItem>
</asp:DropDownList>
更新
首先从GridView本身删除DataSourceID
,然后将其移动到后面的代码中。
if (!Page.IsPostBack)
{
GridView1.DataSource = yourSource;
GridView1.DataBind();
}
并且您在DropDownList上将AutoPostBack
设置为True,但似乎不处理PostBack。 如果没有任何反应,请尝试将其删除。 有关在Grid中编辑和更新数据的信息,请看一下本教程 。 它涵盖了所有基础知识。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.