有以下几种方法:
1.第一种,比较简单的,还没有发现有什么副作用:
DataTable dt2 = DalPiaoBen.SelectOrgan(QuJu.SelectedValue.ToString()).Tables[0]; this.ddlOrgan.DataSource = dt2;
this.ddlOrgan.DataTextField = "BrchName";
this.ddlOrgan.DataValueField = "BrchID";
this.ddlOrgan.DataBind();
ddlOrgan.Items.Insert(0, new ListItem("-----全部-----", "All"));
//ddlOrgan.SelectedIndex=ddlOrgan.Items.Count-1;
2.第二种方法,
ddlOrgan.Items.Insert(0,"All"));
问题显然,不如第一种方法
3.第三种方法,没有发现什么优点,但是可以实现
DataTable dt2 = DalPiaoBen.SelectOrgan().Tables[0]; //机构号 向县局配发应注意
DataRow myRow2 = dt2.NewRow();
myRow2["BrchName"] = "-----全部-----";
myRow2["BrchID"] = "0";
dt2.Rows.InsertAt(myRow2, 0);
this.ddlOrgan.DataSource = dt2;
this.ddlOrgan.DataTextField = "BrchName";
this.ddlOrgan.DataValueField = "BrchID";
this.ddlOrgan.DataBind();
gvKuCun.DataSource = null;
gvKuCun.DataBind();
4.第四种就比较复杂了,也可能有用,算是凑个数吧;
dropSendDept.Items.Add(New ListItem( "全部 ", "all "))
while Reader.Read()
dropSendDept.Items.Add(New ListItem(Reader(0), Reader(1)))
Loop
或者ejddl.Items.Add( "全部 ")
foreach (DataRow dr in yourtable)
{
string mystr=dr[ "你的值端 "].Tostring();
ejddl.Items.Add(mystr);
}
5、再凑一种方法
通过语句SelectCommand="select '-1' as Agencyid,'全部' as AgencyName union select Agencyid,AgencyName from Agency order by Agencyid"
或者: <td>
<asp:DropDownList ID="dplAgencyA" runat="server"
DataSourceID="SqlDataSourceAgencyA" DataTextField="typeName"
DataValueField="typeid"></asp:DropDownList><asp:SqlDataSource ID="SqlDataSourceAgencyA" runat="server"
ConnectionString="<%$ ConnectionStrings:FaPiaoConnectionString %>"
SelectCommand="select '0' as typeid,'全部' as typeName union select typeid,typeName from fapiaotype"></asp:SqlDataSource>
</td> 够酷吧
DropDownList快速绑定,且有添加一个全部选项
page:A.aspx
手术类别:<asp:DropDownList ID="Adv_TypeName" runat="server"
DataTextField="ClassName" DataValueField="PlClassID"
AppendDataBoundItems="True" Width="100px">
<asp:ListItem Selected="True" Value="0">请先选择类别</asp:ListItem>
</asp:DropDownList>
Page:a.aspx.cs
string lvSQL = "select [ID] ,[ClassName] from Tb_Class where ParentID=0";
DataTable dt = DbHelper.ExecuteTable(CommandType.Text, lvSQL, null);
Adv_TypeName.DataSource = dt;
Adv_TypeName.DataBind();
以上绑定由于有了属性:
AppendDataBoundItems,绑定数据时,全以添加的形式绑定,不会清除已经拥有的选项