Home > ASP.Net > Bind DropdownList with Multiple Text Value

Bind DropdownList with Multiple Text Value


Hi

try this example :

let we have table in my database has the following fields (Id , Name , IsActive)

<asp: DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
        Width="209px">
    </asp: DropDownList>

in code behind:-

public void Bind()
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Bag_ConnStr"].ConnectionString);
        SqlCommand comm = new SqlCommand();
        comm.CommandText = "Select * from Education_Year";
        comm.Connection = conn;
        comm.CommandType = CommandType.Text;
        DataSet ds = new DataSet();
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = comm;
        adapter.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                ListItem ls = new ListItem();
                ls.Text = row["Name"] + " " + row["IsActive"];
                ls.Value = row["Id"].ToString();
                DropDownList1.Items.Add(ls);

            }
            DropDownList1.DataBind();
        }

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Write(DropDownList1.SelectedValue);
    }

Good Luck

Advertisements
Categories: ASP.Net
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: