Thursday, 11 December 2014

Add Select text in Dropdownlist in Asp.Net OR Adding Default Select Option in the DropDownList Using C#.Net in Asp.Net

12/11/2014 - By Pranav Singh 0



This article will show you how you can bind the value collection to dropdown list and add the “Select” value to the the dropdown in asp.net using c#.net. In this article I have use sql server to retrieve the data from the database.

Here is the sql table which we will bind with dropdownlist

 
So for this article first we will create a new asp.net application and add the below code into the .asp page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Add Select text in Dropdownlist in Asp.Net OR Adding Default Select Option in the DropDownList Using C#.Net in Asp.Net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>   
        Country Name :<asp:DropDownList ID="DropDownList1" runat="server" Width="150px">
        </asp:DropDownList>   
    </div>
    </form>
</body>
</html>

Now add the below code into the .cs page for binding the dropdown list and assigning the default value.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace WebApplication1
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDdl();
            }
        }
        private void BindDdl()
        {
            SqlConnection con = new SqlConnection("Data Source=SQLEXPRESS;Initial Catalog=Demo;Integrated Security=True");
            string queryValidate = "Select * from CountryName";
            DataTable objdt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(queryValidate, con);
            da.Fill(objdt);
            if (objdt.Rows.Count > 0)
            {
                DropDownList1.DataSource = objdt;
                DropDownList1.DataTextField = "CountryName";
                DropDownList1.DataValueField = "Id";
                DropDownList1.DataBind();
                DropDownList1.Items.Insert(0, "Select Country");
            }
        }
    }
}

In above code I have shown how you can bind the dropdown list control using c#.net in asp.net. Just check the below code.

                DropDownList1.Items.Insert(0, "Select Country");

This piece of code is used for assigning the default value to the dropdown list control. Now we have done run the code and check the output.



About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top