[英]Set drop down list selected value to value in database
What I'm trying to do is set a drop down list to be whatever its value is in the database when editing an entry. 我想要做的是将一个下拉列表设置为编辑条目时数据库中的值。 I am able to save the value of the drop down list to the database just fine, but I can't seem to set the control in the browser to show this. 我能够将下拉列表的值保存到数据库中,但是我似乎无法在浏览器中设置控件来显示此信息。 IE The selected value of "4" is saved to the database for entry ABC, but when I return to this entry to edit it again, this is not the selected value--it's the first item in the drop down list. IE选定的值“ 4”已保存到数据库中,用于条目ABC,但是当我返回此条目以再次对其进行编辑时,这不是选定的值-这是下拉列表中的第一项。
This is the code I have that is meant to set the selected value of the drop down upon editing an entry, but it does not work: 这是我拥有的用于在编辑条目时设置下拉菜单的选定值的代码,但是它不起作用:
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["AbleCommerce"].ToString()))
{
cn.Open();
SqlCommand cmd = new SqlCommand(selectQuery, cn);
cmd.Parameters.Add(new SqlParameter("@CustomerID", custIDHidden.Value));
using (IDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
TaxExemptDDL.SelectedValue = reader["TaxExemptFile"].ToString();
}
}
cn.Close();
}
The control itself (the value is what gets stored in the database, not the text): 控件本身(值是存储在数据库中的内容,而不是文本):
<asp:DropDownList ID="TaxExemptDDL" style="width:165px;" runat="server">
<asp:ListItem Text="True" Value="1" />
<asp:ListItem Text="False" Value="0" />
</asp:DropDownList>
Am I going about this the wrong way? 我会以错误的方式处理吗? It seems like it would be simple to do but I appear to have it wrong somehow... 看起来这很简单,但是我似乎以某种方式错了...
You can't set the SelectedValue like that. 您不能像这样设置SelectedValue。
ListItem itemToSelect = TaxExemptDDL.Items.FindByValue(reader["TaxExemptFile"].ToString());
if (itemToSelect != null)
{
TaxExemptDDL.SelectedIndex = TaxExemptDDL.Items.IndexOf(itemToSelect);
}
Why doesn't someone make it easier to post code here? 为什么没有人使在此处发布代码变得更容易? Like other sites where you can put stuff between [Code] tags and it formats the code. 与其他站点一样,您可以在[Code]标签之间放置内容,并格式化代码。 It is hard work posting code here. 在这里张贴代码是艰苦的工作。 And edits don't always update either. 而且编辑也不总是更新。
**u can add items to the dropdownlist by manually from design or from values from database. ** u可以通过设计或数据库中的值手动将项目添加到下拉列表中。 if u are in a page for editing and u want to set the dropdownlist selected value as in the database value, then take the selected value from database to string and do like this. 如果u在要编辑的页面中,并且您想要将下拉列表选择的值设置为数据库值,则将选择的值从数据库中提取为字符串,然后执行此操作。 in my example i want to show the course list in dropdownlist & my selected course in database is "php". 在我的示例中,我想在下拉列表中显示课程列表,而我在数据库中选择的课程是“ php”。 Bt my dropdownlist is in the order of 我的下拉列表的顺序为
android 安卓
asp.net asp.net
java 爪哇
php 的PHP
in PageLoad** 在PageLoad **中
string course="php"; 字符串课程=“ php”;
ListItem itm = new ListItem("course", "course"); ListItem itm = new ListItem(“ course”,“ course”);
DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(itm); DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(itm);
now when page loads dropdownlist selected item will be "php". 现在,当页面加载下拉列表时,所选项目将为“ php”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.