2013-05-08 84 views
2

我在UpdatePanel_2下拉列表中,单击在UpdatePanel_1时Button_1它被填充,如何保持“选择”的项目[0]下拉列表

填充的DropDownList时,它会删除我的“选择”项不知道为什么,

我的下拉列表标记是,

<asp:DropDownList id="drop1" runat="server" > 
       <asp:ListItem Text=" Select " /> 
      </asp:DropDownList> 

这是怎么了填充它,

using (SqlDataSource sqlds = new SqlDataSource(ConnectionString(), SelectCommand())) 
      { 
       drop1.DataSource = sqlds; 
       drop1.DataTextField = "UserName"; 
       drop1.DataBind(); 
      } 

回答

3

你需要添加AppendDataBoundItems="true"DropDownList

,但如果你再填充DropDownList又一次,你可以做如下

<asp:DropDownList id="drop1" runat="server" ondatabound="drop1_DataBound" > 
</asp:DropDownList> 

然后在后面的代码:

protected void drop1_DataBound(object sender, EventArgs e) 
{ 
    drop1.Items.Insert(0, new ListItem(" Select ", "")); 
} 

甚至以下将工作

<asp:DropDownList id="drop1" runat="server" > 
</asp:DropDownList> 

然后在你的代码背后:

using (SqlDataSource sqlds = new SqlDataSource(ConnectionString(), SelectCommand())) 
{ 
    drop1.DataSource = sqlds; 
    drop1.DataTextField = "UserName"; 
    drop1.DataBind();// insert after DataBind 
    drop1.Items.Insert(0, new ListItem(" Select ", "")); 
} 
相关问题