DropDown1.SelectedItem.Value.......

Discussion in 'ASP .Net' started by Lasse Edsvik, Oct 4, 2004.

  1. Lasse Edsvik

    Lasse Edsvik Guest

    Hello

    I have a dropdown with 3 different values, and it prints out first value no
    matter how i change it......

    private void Button1_Click(object sender, System.EventArgs e)

    {

    if(Page.IsValid)

    {


    Response.Write(DropDown1.SelectedItem.Value);


    }

    }



    what's wrong?

    /Lasse
     
    Lasse Edsvik, Oct 4, 2004
    #1
    1. Advertisements

  2. Lasse Edsvik

    Guest Guest

    Hi Edsvik,
    Just check if you are rebinding the data on PostBack.

    Sample code for ref

    private void Page_Load(object sender, System.EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    DropDownList1.Items.Add("1");
    DropDownList1.Items.Add("2");
    DropDownList1.Items.Add("3");

    }
    }
    private void Button1_Click(object sender, System.EventArgs e)
    {
    if(Page.IsValid)
    {
    Response.Write(DropDownList1.SelectedItem.Value );
    }
    }
    HTH
    srini
     
    Guest, Oct 4, 2004
    #2
    1. Advertisements

  3. Lasse Edsvik

    Lasse Edsvik Guest

    srini,

    not sure what you mean....... if i check the source its there:

    <select name="Textures" id="Textures">
    <option value="gold.jpg">gold.jpg</option>
    <option value="goldwave.jpg">goldwave.jpg</option>
    <option value="rocks.jpg">rocks.jpg</option>
    </select>


    and in the Page_Load i call a function that generates that droplist.......

    but it doesnt work...... always prints first value (gold.jpg) in droplist
    when i click the button......
     
    Lasse Edsvik, Oct 4, 2004
    #3
  4. Lasse Edsvik

    Guest Guest

    Dear Lasse,

    Where are you populating your Dropdownlist. I think you are doing it at the
    page_load event.

    If so, you need to put the code between

    if(!IsPostBack)
    {
    code to populate your dropdownlist
    }

    thats bcos, each time you click the button, the page is posted back and
    loads the page. hence, it will refill the dropdownlist.

    if you give it within the above code, then the dropdown will be filled only
    when the page is first time loaded and not each time it is posted back.

    hope it helps.
     
    Guest, Oct 4, 2004
    #4
  5. Lasse Edsvik

    Guest Guest

    call that function on page load like
    if (!Page.IsPostBack)
    {
    //Call function here
    }
    HTH
    srini
     
    Guest, Oct 4, 2004
    #5
  6. Lasse Edsvik

    Lasse Edsvik Guest

    thx for helping


     
    Lasse Edsvik, Oct 4, 2004
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.