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

  • Thread starter Thread starter Lasse Edsvik
  • Start date Start date
L

Lasse Edsvik

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
 
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
 
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......
 
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.
 
call that function on page load like
if (!Page.IsPostBack)
{
//Call function here
}
HTH
srini
 
Back
Top