galvin Verghese
galvin Verghese

Reputation: 605

Asp.Net Dropdownlist

I Bind the Dropdownlist at runtime and data is populated to the database. Its ok Fine. But if i want to select the particular value and display in a message box. It only shows the default value.

Here My code is:

protected void Button1_Click(object sender, EventArgs e)
{
    Response.Write("You have selected " + DropDownList1.SelectedItem.Value);
  }

So how can i display the selected value in the message box. Here i am very new. Please help me.

Upvotes: 0

Views: 403

Answers (2)

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

The problem is in your Page_load event, where you are assigning your Datasource. When you click the button, Page_load will be called again and it will rebind to your dropdown again.

It should be:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        //Set your dropdown datasource here...
    }
}

Upvotes: 2

Muhammad Umair
Muhammad Umair

Reputation: 30

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Response.Write("<script>alert('You have selected ' + '" + DropDownList1.SelectedValue + "')</script>")
End Sub

Upvotes: -1

Related Questions