0

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.

2
  • 4
    12 Questions and no answers accepted ? Commented Apr 26, 2011 at 6:44
  • Select the Checkmark/Tick against the answer you found useful Commented Apr 26, 2011 at 11:11

2 Answers 2

2

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...
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

so what was the issue finally ? which solution helped you ?
That's right every time your page will postback it'll populate the ItemList of DropDownList and set the SelectedIdex, SelectedValue, & SelectedItem to the default ones, so put the population on !PostBack aka HTTP GET
Problem was i am not using the page.ispostback, so that was the error. So now it is working fine. Thank u very much
then you accepted the wrong answer, As I mention the page.Ispostback issue. You must have to do correction in your acceptance.
-1
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.