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

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
 
G

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
 
L

Lasse Edsvik

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......
 
G

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.
 
G

Guest

call that function on page load like
if (!Page.IsPostBack)
{
//Call function here
}
HTH
srini
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top