如何在下拉列表中设置-请选择- [英] how to set --please select-- in dropdown list

查看:228
本文介绍了如何在下拉列表中设置-请选择-的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <   asp:DropDownList   ID    ddlroomtype "  runat   服务器" <   asp:ListItem      ="  已选择  真实" <  > 
<  /asp:DropDownList  >  




我使用上面的编码在dropdownlist中显示请选择"选项

页面加载事件中

我已经使用c#编码从数据库中获取数据并绑定到dropdownlist.页面加载后无法在dropdownlist中看到请选择"选项.它仅绑定了数据库中的值.

尝试一下:

private void FillDropDownList()
    {
        DataSet ds = new DataSet();
        SqlDataAdapter myda = new SqlDataAdapter("Select field  FROM tablename",con object);
        myda.Fill(ds);
        drop1.DataSource = ds;
        drop1.DataValueField = "field";
        drop1.DataBind();
        drop1.Items.Insert(0, new ListItem("--Please Select", "0"));
    }



并在页面上加载

 如果(!ispostback)
{
  FillDropDownList();
} 




谢谢.....


绑定下拉列表后,编写以下代码.

 ddlroomtype.Items.Insert( 0  new  ListItem(" )); 




谢谢


简便的解决方法.在查询中,使用带有"--please select ---"子句的UNION.

例如

mytable

 选择 id,名称
联盟
选择  0 ' -请选择---' 


<asp:DropDownList ID="ddlroomtype" runat="server">
<asp:ListItem Value="0" Selected="True">Please Select</asp:ListItem>
</asp:DropDownList>




i used above coding to show "please select" option in dropdownlist

in page load event

i have used c# coding to fetch data from database and bind to dropdownlist.after page get load unable to see "please select" option in dropdownlist., it have only binded values from database..

解决方案

try this :

private void FillDropDownList()
    {
        DataSet ds = new DataSet();
        SqlDataAdapter myda = new SqlDataAdapter("Select field  FROM tablename",con object);
        myda.Fill(ds);
        drop1.DataSource = ds;
        drop1.DataValueField = "field";
        drop1.DataBind();
        drop1.Items.Insert(0, new ListItem("--Please Select", "0"));
    }



and on Page Load

if(!ispostback)
{
  FillDropDownList();
}




Thanks.....


Write the following code after binding the dropdownlist.

ddlroomtype.Items.Insert(0, new ListItem("Please Select"));




Thanks


Easy workaround. In your query, do a UNION with a "--please select---'' clause.

For e.g.

select id, name from mytable
UNION
select 0, '--please select---'


这篇关于如何在下拉列表中设置-请选择-的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆