Tech Junkie Blog - Real World Tutorials, Happy Coding!: ASP.NET DropDownList Part 2: Setting Default Value on First Page Load

Wednesday, July 16, 2014

ASP.NET DropDownList Part 2: Setting Default Value on First Page Load

In part one DropDownList Part 1: Bind a DataTable to A DropDownList we learned how to bind a DataTable to a DropDownList.  In this tutorial we will learn to set the default value of the DropDownList control on the page's first load that is before there is a post back to the page.  This behavior is commonly used when you want to default the drop down list to a particular selection.  Such as the "United States" in a list of countries drop down list.

To set a default value in the DropDownList control follow the steps below.

1. Add the SetDefaultSelectItem method to the code behind file.
protected void SetDefaultSelectItem(string txtSelect)
{
   DropDownList1.Items.FindByText(txtSelect).Selected = true;
}

2. Add a condition to only set the default value on the first page load
protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostBack)
    {
        BindCategoriesList();
        SetDefaultSelectItem("Seafood");
    }
}
3. If you run the application you will now see that "Seafood" is the default selection on the DropDownList control, when the page first load
Default DropDownList selection




Related Blogs:



  1. Bind a DataTable To A DropDownList
  2. (DropDownList) Setting Default Value on First Page Load
  3. Use The DropDownList Control To Populate GridView
  4. Bind DropDownList To A List Of Objects

4 comments:


  1. Sosyal medya hesaplarınızın hızla büyümesini istiyorsanız güvenilir ve etkili hizmetler sunan en güvenilir smm panel ile çalışmanız önemlidir. Bu panel sayesinde organik ve hızlı takipçi kazanımı sağlayabilirsiniz. Kaliteli hizmetleri ve uygun fiyatlarıyla öne çıkan bu platform sizlerin sosyal medya başarısına ulaşmasında büyük katkı sağlar. Güvenilirlik ve performans açısından tercih edilebilecek en iyi seçeneklerden biridir.

    ReplyDelete

  2. İşte aradığınız detaylı bilgiye ulaşmak için whatsapp görüntülü show bağlantısını ziyaret edebilirsiniz. Bu hizmet sayesinde sevdiklerinizle yüz yüze iletişim kurabilir, görüntülü görüşmelerinizi daha eğlenceli hale getirebilirsiniz. Ayrıca, çeşitli özellikler ve avantajlar hakkında detaylı bilgi edinmek için ilgili siteyi incelemek faydalı olacaktır. Sonuç olarak, bu platform size kaliteli ve güvenilir bir görüntülü iletişim deneyimi sunmayı amaçlamaktadır.

    ReplyDelete

Search This Blog