日期:2014-05-20  浏览次数:20990 次

关于asp:DropDownList控件的问题
lstCategory是绑定到数据库表Categories,HTML显示如下代码:
<select   name= "lstCategory "   id= "lstCategory "   style= "width:80px;Z-INDEX:   102;   LEFT:   16px;   POSITION:  

absolute;   TOP:   376px ">
<option   selected= "selected "   value= "1 "> Beverages </option>
<option   value= "2 "> Condiments </option>
<option   value= "3 "> Confections </option>
<option   value= "4 "> Dairy   Products </option>
<option   value= "5 "> Grains/Cereals </option>
<option   value= "6 "> Meat/Poultry </option>
<option   value= "7 "> Produce </option>
<option   value= "8 "> Seafood </option>

</select>
我能否在lstCategory中增加一项自定义的(前提事数据库的表不需要增加数据行):

<option   value= "0 "> All </option>


ASP.NET页:

CodeWizard.aspx
---------------------------
<asp:DropDownList   id= "lstCategory "   style= "Z-INDEX:   102;   LEFT:   16px;   POSITION:   absolute;   TOP:   376px "
runat= "server "   Width= "80px "> </asp:DropDownList>

CodeWizard.aspx.cs
----------------------------
private   void   Page_Load(object   sender,   System.EventArgs   e)
{
//   在此处放置用户代码以初始化页面
if   (!Page.IsPostBack)  
{
DataGrid1.DataSource   =   GetProductsDataSet(0);
DataGrid1.DataBind();
}

if   (!Page.IsPostBack)  
{
Response.Write   (Page.IsPostBack);
lstCategory.DataSource=GetCategories();
lstCategory.DataTextField= "CategoryName ";
lstCategory.DataValueField= "CategoryID ";
lstCategory.DataBind();
}

}

------解决方案--------------------
lstCategory.DataBind();
lstCategory.Items.InsertAt(0,new ItemList( "xx ", "xx "));
------解决方案--------------------
if (!Page.IsPostBack)
{
Response.Write (Page.IsPostBack);
lstCategory.DataSource=GetCategories();
lstCategory.DataTextField= "CategoryName ";
lstCategory.DataValueField= "CategoryID ";
lstCategory.DataBind();
ListItem li = new ListItem();
li.Value = "0 ";
li.Text = "all ";
lstCategory.Items.Insert(0,li);
}

------解决方案--------------------
可以考虑动态添加item。
------解决方案--------------------
if (!Page.IsPostBack)
{
Response.Write (Page.IsPostBack);
lstCategory.DataSource=GetCategories();
lstCategory.DataTextField= "CategoryName ";
lstCategory.DataValueField= "CategoryID ";
lstCategory.DataBind();
lstCategory.Items.InsertAt(0,new ItemList( "All ", "0 "));

}
------解决方案--------------------
...都说光了。
------解决方案--------------------
先绑定好,之后加上
this.DropDownList1.Items.Insert(0,new ListItem( " ", " "));