[英]How to set selected value on a databound dropdownlist from a datatable
Ok, so i have a dropdownlist that is originally populated from the database. 好的,所以我有一个最初从数据库填充的下拉列表。 Now based on a datatable, i want the selected value to be equal to the text residing in the database.
现在基于数据表,我希望所选值等于数据库中的文本。 Whatever i do, it only shows "---Select One---" which is the only item i added manually to the dropdownlist items list to show a default value if the value i'm pulling is null (or that's what i want to do)
无论我做什么,它只会显示“ --- Select One ---”,这是我手动添加到下拉列表项列表中的唯一项,如果我要提取的值为null(或者这就是我想要的),则会显示默认值去做)
protected void Page_Load(object sender, EventArgs e)
{
Master.TopLabel = "Survey Creation";
if (!IsPostBack)
{
SqlConnection Connection = DatabaseConnection.GetSurveySystemConnection();
string sqlquery = "SELECT S.[Survey_Desc], S.[Start_Date], C.[Category_Name] ,S.[End_Date], S.[Audience] FROM [Survey] S Inner Join Category C On S.Category_ID = C.ID Where S.[ID] =" + Session["Survey_ID"];
SqlCommand cmd = new SqlCommand(sqlquery, Connection);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataTable DT = new DataTable();
da.Fill(DT);
if (DT != null)
{
DescriptionMemo.Text = DT.Rows[0]["Survey_Desc"].ToString();
CategoryDropDownList.SelectedIndex = CategoryDropDownList.Items.IndexOf(CategoryDropDownList.Items.FindByText(DT.Rows[0]["Category_Name"].ToString()));
StartDateCalender.SelectedDate = DateTime.Parse(DT.Rows[0]["Start_Date"].ToString());
EndDateCalender.SelectedDate = DateTime.Parse(DT.Rows[0]["End_Date"].ToString());
string Audience = DT.Rows[0]["Audience"].ToString();
if (Audience == "Students Only")
{
AudienceRadioGroup.Items[0].Selected = true;
}
else if (Audience == "Staff Only")
{
AudienceRadioGroup.Items[1].Selected = true;
}
else
{
AudienceRadioGroup.Items[2].Selected = true;
}
}
Connection.Close();
}
}
DropDownList in aspx page. 在aspx页面中的DropDownList。
<asp:DropDownList ID="CategoryDropDownList" runat="server"
DataSourceID="SqlDataSource1" DataTextField="Category_Name"
DataValueField="Category_Name" AppendDataBoundItems="true" Height="16px"
Width="200px">
<asp:ListItem>---Select One---</asp:ListItem>
</asp:DropDownList>
The sql datasource select command. sql数据源选择命令。
SELECT [Category_Name] FROM [Category]
EDIT : This is full code, but i didnt know if it was relevant, sorry. 编辑:这是完整的代码,但我不知道它是否相关,对不起。
Change : 变更:
CategoryDropDownList.SelectedIndex = CategoryDropDownList.Items.IndexOf(CategoryDropDownList.Items.FindByText(DT.Rows[0]["Category_Name"].ToString()));
to 至
CategoryDropDownList.SelectedValue = DT.Rows[0]["Category_Name"].ToString()
Two things: 两件事情:
using-statement
using-statement
尽快关闭连接。 if(!IsPostback)
check into Page_Load
, don't databind controls on postback when EnableViewState
is set to true
(default). if(!IsPostback)
检查放入Page_Load
,当EnableViewState
设置为true
(默认值)时,不要在回发时对控件进行数据绑定。 Otherwise events won't be triggered and values(like DropDownList-SelectedIndex
) are overridden. DropDownList-SelectedIndex
)将被覆盖。 protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
string sqlquery = "SELECT S.[Survey_Desc], C.[Category_Name], FROM [Survey] S Inner Join Category C On S.Category_ID = C.ID Where S.[ID] =" + Session["Survey_ID"];
using(var con=new SqlConnection(connectionString))
using(var cmd = new SqlCommand(sqlquery, con))
using(var da = new SqlDataAdapter(cmd))
{
DataTable DT = new DataTable();
da.Fill(DT);
DescriptionMemo.Text = DT.Rows[0].Field<string>("Survey_Desc");
string categoryName = DT.Rows[0].Field<string>("Category_Name");
CategoryDropDownList.SelectedIndex = CategoryDropDownList.Items
.IndexOf(CategoryDropDownList.Items.FindByText(categoryName));
}
}
}
OnPageLoad CategoryDropDownList.Items.FindByText(categoryName).selected = true;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.