C#下拉列表绑定默认值 [英] c# dropdown binding default value

查看:75
本文介绍了C#下拉列表绑定默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当dropdownlist具有从数据库数据集中获取的datatext字段和datavalue字段时,如何在零索引位置添加默认值选择一个".

how can i add default value "select one" at zero index position when dropdownlist is having datatext field and datavalue field which is obtained from database througt dataset.

推荐答案

您可以使用查询进行添加:您可以添加page的pageload事件.
但要添加!ispostback事件.

喜欢.....
you can add by using the query : you can add on pageload event of page.
but add in !ispostback event.

like.....
protected void Page_Load(object sender, EventArgs e)
 {
    

     if (!IsPostBack)
     {
         //  you must set the connection first.
        // e.g c.setcon(); // here this is method for setting a connection
         DataSet ds = new DataSet();
        SqlDataAdapter myda = new SqlDataAdapter("Select field1  FROM Table", connection object);
        myda.Fill(ds);
        DropDown1.DataSource = ds;
        DropDown1.DataValueField = "field1";
        DropDown1.DataBind();
        DropDown1.Items.Insert(0, new ListItem("Select One", "0"));
         
     }
    
 }




希望这能对您有所帮助,否则请贴出来.




hope this will help you, if not then dear please post it.


>一种方法是在用于填充下拉列表的查询中使用UNION 子句.

例如

>One way to do this is to use UNION clause on the query you use to populate your dropdown with.

For e.g.

select 0, "select one"
UNION
select colA, colB from tableA


这篇关于C#下拉列表绑定默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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