Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / July 2005

Tip: Looking for answers? Try searching our database.

SELECT dynamically populated probem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
AC - 20 Jul 2005 02:19 GMT
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.
Craig Deelsnyder - 20 Jul 2005 03:24 GMT
> Hello, All.
>
[quoted text clipped - 24 lines]
> 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
}

Signature

Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET

AC - 20 Jul 2005 09:21 GMT
Hi, Craig

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

Alex

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.