将“请选择”添加到从数据库中检索值的下拉列表中 [英] Add “Please Select” to Drop-down list that retrieves values from Data base
问题描述
我有一个下拉列表并使用SQL数据源来检索SQL服务器中的产品名称。现在,我想在下拉列表中添加请选择产品选项。
据我所知,我通过使用
I have a drop-down list and uses the SQL data source for it where I am retrieving the product names from SQL server. Now, I want to add "Please Select a product" Option to the drop-down list.
As far as my knowledge, I add options to drop-down list by using
<asp:ListItem Selected ="true" Value = "1">1</asp:ListItem>
因为,我没有添加值但是从DB中检索值,如何另外实现此选项并将其作为第一个位置添加到我的下拉列表中?
我尝试了下面的代码,但没有每当我选择其他值时,每次我都得到额外的请选择选项。
Since, I am not adding the values but retrieving the values from DB, how to achieve this Option additionally and add to it as first position to my drop-down list?
I tried the below code, but not able to do get at first position.Also, each time I am getting extra "please select" option whenever I am selecting other values.
protected void NameDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
NameDropDownList.Items.Insert(0, new ListItem("Please Select a product", "Please Select a product");
SqlCommand cmd = new SqlCommand("SELECT ProductID, Price, Description, Rating FROM Product_Info Where Name = '" + NameDropDownList.Text + "'", conn);
SqlDataReader myReader;
conn.Open();
myReader = cmd.ExecuteReader();
while (myReader.Read()) {
//Logic
}
conn.Close();
myReader.Close();
这是我绑定数据的代码:
Here is my code behind where I bind the data:
<tr>
<td class="style2">Name</td>
<td>
<asp:DropDownList ID="NameDropDownList" runat="server" Height="16px"
Width="130px" AutoPostBack="True" DataSourceID="NameSqlDataSource"
DataTextField="Name" DataValueField="Name"
onselectedindexchanged="NameDropDownList_SelectedIndexChanged">
</asp:DropDownList>
<asp:SqlDataSource ID="NameSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ProductsConnectionString %>"
SelectCommand="SELECT [Name] FROM [Product_Info]"></asp:SqlDataSource>
</td>
</tr>
我也将自动发布回真。提前谢谢
I also enables the Auto post back to true.Thanks in advance
推荐答案
ConnectionStrings:ProductsConnectionString%>
SelectCommand = SELECT [Name] FROM [Product_Info] > < / asp:SqlDataSource >
< / td >
< / tr >
ConnectionStrings:ProductsConnectionString %>" SelectCommand="SELECT [Name] FROM [Product_Info]"></asp:SqlDataSource> </td> </tr>
我也将自动发布回真。提前谢谢
I also enables the Auto post back to true.Thanks in advance
尝试
NameDropDownList.Items.Add("Please Select a product");
如果您首先清除列表,那么请选择产品将是列表中的第一项,然后填充列表的其余部分。
就个人而言,我不会在列表中提供帮助信息,而是将其放在工具提示或单独的标签上。
这假设你没有绑定列表..
如果数据绑定使用
If you clear the list first then the "Please Select a Product" will be the first item in the list and then populate the rest of the list.
Personally I would not put help information in the list but put it in on a tooltip or separate label.
This assumes you do not have a bound list..
If data bound use
NameDropDownList.Items.Insert(0, new ListItem("Please Select a product", "Please Select a product");
DropDownList1.Items.Insert(0,请选择选项);
DropDownList1.Items.Insert(0,"Please Select Options");
这篇关于将“请选择”添加到从数据库中检索值的下拉列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!