SELECT dynamically populated probem

A

AC

Hello, All.

I have SELECT on my WebForm, marked as [Run as Server Control].
<SELECT id="Select1" name="Select1" runat="server">
<OPTION value="AAA" selected>AAA</OPTION>
</SELECT>

In Page_Load new item inserted into Select1:

private void Page_Load(object sender, System.EventArgs e)
{
Select1.Items.Add("BBB");
Select1.Items[SortMode.Items.Count - 1].Value = "BBB";
...
}
And finally, here is
private void Select1_ServerChange(object sender, System.EventArgs e)
{
System.Diagnostics.Debug.WriteLine(Select1.Value);
}

Now, if I select item "AAA" and submit form - I see "AAA".
But when I select "BBB" - I still see "AAA".

Why this occur? How can I solve this problem?


Thank you for your time,
Alex.
 
C

Craig Deelsnyder

Hello, All.

I have SELECT on my WebForm, marked as [Run as Server Control].
<SELECT id="Select1" name="Select1" runat="server">
<OPTION value="AAA" selected>AAA</OPTION>
</SELECT>

In Page_Load new item inserted into Select1:

private void Page_Load(object sender, System.EventArgs e)
{
Select1.Items.Add("BBB");
Select1.Items[SortMode.Items.Count - 1].Value = "BBB";
...
}
And finally, here is
private void Select1_ServerChange(object sender, System.EventArgs e)
{
System.Diagnostics.Debug.WriteLine(Select1.Value);
}

Now, if I select item "AAA" and submit form - I see "AAA".
But when I select "BBB" - I still see "AAA".

Why this occur? How can I solve this problem?


Thank you for your time,
Alex.

Add a check for IsPostBack; when you alter the items collection, it loses
its selectedindex. So wrap you Page_Load body with

if (!IsPostBack)
{
//...do your stuff
}
 
A

AC

Hi, Craig

Thank for you tip! I have moved my code into pages OnInit method - and now
all is ok.

Alex
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top