Thursday, 5 November 2015

How to bind dropdownlist at page load in asp.net ?



Design Code :


<asp:DropDownList ID="ddlName" runat="server"
          AutoPostBack="true">

    </asp:DropDownList>

Note : AutoPostback is marked true.




At .cs 

  protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlBind( );
            }
       }
 public void ddlBind( )
        {
            string[ ] UserName = new string[ ] { "Kumar", "Rohit", "Shivam" };
            ddlName.DataSource = UserName;
            ddlName.DataBind( );
        }



Note :    

We have first checked that the page is a postback or not.
 If you will not use IsPostBack then you will always get "Kumar" our first listitem at the time of selectedIndexChange

Because the dropdown is re-binded.


Related Articles :


No comments:

Post a Comment