This is a migrated thread and some comments may be shown as answers.

Default Value in Dropdown list problem

3 Answers 100 Views
Grid
This is a migrated thread and some comments may be shown as answers.
sanjeev
Top achievements
Rank 1
sanjeev asked on 16 Feb 2009, 03:24 PM
i have dropdown in ascx.cs page...i want "Select" to be bind in 0th position..it is not binding at first time...i also gave in !ispostback,no use....after insert it is binding....please give suggestion..

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 17 Feb 2009, 06:26 AM
Hello Sanjeev,

You can set the AppendDataBoundItems property for the DropDownList to true and then try out the following code to insert an item to the dropdownlist.
ascx:
<asp:DropDownList ID="DropDownList1" AutoPostBack="true" AppendDataBoundItems="true"  DataSourceID="SqlDataSource1" DataTextField="ContactTitle" DataValueField="ContactTitle" runat="server"
</asp:DropDownList> 

ascx.cs:
 protected void Page_Load(object sender, EventArgs e) 
    { 
        DropDownList1.Items.Insert(0, "Select");           
        
    } 

Thanks
Princy.
0
sanjeev
Top achievements
Rank 1
answered on 17 Feb 2009, 08:46 AM
the soltion u have given is working. and "select" is binding but other items below are binding twice..how can i rectify this..can u please suggest....
0
Shinu
Top achievements
Rank 2
answered on 18 Feb 2009, 08:21 AM
Hi Sanjeev,

I guess the 'Select' item is getting added repeatedly after each PostBack. If so give a try with the code snippet below and see if it helps.

CS:
  protected void Page_Load(object sender, EventArgs e) 
    { 
        if (!IsPostBack) 
        { 
            DropDownList1.Items.Insert(0, "Select");         
        } 
     
    } 


Thanks
Shinu
Tags
Grid
Asked by
sanjeev
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
sanjeev
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or