how to get empty row in DropDownList
i have this citys in my database:
city1
city2
city3
city4
i开发者_开发问答 what to see this in my DropDownList:
[empty]
city1
city2
city3
city4
this is my bind code:
SQL = "select distinct City from MyTbl order by City";
dsClass = new DataSet();
adp = new SqlDataAdapter(SQL, Conn);
adp.Fill(dsClass, "MyTbl");
adp.Dispose();
DropDownList3.DataSource = dsClass.Tables[0];
DropDownList3.DataTextField = dsClass.Tables[0].Columns[0].ColumnName.ToString();
DropDownList3.DataValueField = dsClass.Tables[0].Columns[0].ColumnName.ToString();
DropDownList3.DataBind();
how to do it (asp.net C#) ?
thanks in advance
After Binding DropDownList through below mentioned code you can add new item in DropDownList
DropDownList3.Items.Insert(0, new ListItem(String.Empty, String.Empty));
You can replace String.Empty
to your desire value.
DropDownList1.Items.Insert(0, String.Empty);
which is the same as
DropDownList1.Items.Insert(0, new ListItem(String.Empty, String.Empty));
or
DropDownList1.Items.Insert(0, new ListItem(String.Empty));
or
DropDownList1.Items.Insert(0, new ListItem());
<asp:DropDownList ID='DropDownList1' runat='server' DataSourceID='ObjectDataSource1'
DataTextField='ProductName' DataValueField='ProductID' AppendDataBoundItems='true'
>
<asp:ListItem Text="" Enabled="true" Selected="True" Value='-1'>
</asp:ListItem>
</asp:DropDownList>
by setting AppendDataBoundItems = true in the DropDownList markup, DropDownList would append the items from datasource after the asp:ListItem (specified in the markup).
This could be achieved from code-behind but this is lot simpler.
精彩评论