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 / February 2008

Tip: Looking for answers? Try searching our database.

DropDownList & Viewstate

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
jwnews1231@gmail.com - 31 Jan 2008 22:56 GMT
Hi,

I've been working on minimizing the use of the ViewState in my asp.net
applications because of its overhead.  The problem I am having is that
I keep losing SelectedIndex in the DropDownLists.  I don't lose the
SelectedIndex when the ViewState is enabled, only when it is off.

I have a dropdownlist named countryCbo inside of a UserControl and
countryCbo's EnableViewState property is false.  In the OnInit()
method of the UserCountry it loads the country list from the database
and DataBinding it.

public void LoadCountryList() {
   // SQL Stuff ...

   this.countryCbo.DataTextField = "Name";
   this.countryCbo.DataValueField = "Code";
   this.countryCbo.DataSource = countries;
   this.countryCbo.DataBind();
}

// Doing this will load the countries but I lose the SelectedIndex on
a postback.
protected override void OnInit(EventArgs e) {
   LoadCountryList();
   base.OnInit(e);
}

// Doing this will load the countries but when a postback occurs I
lose all the countries and the list is empty.
protected override void OnInit(EventArgs e) {
   if (!Page.IsPostBack) {
      LoadCountryList();
   }
   base.OnInit(e);
}

I have read somewhere that the SelectedIndex resets back to -1 if a
databind occurs.  Would that be the case?
Could someone tell me how they have gotten their dropdownlists to work
without the ViewState being enabled?

Thank you!

JW
Angel - 01 Feb 2008 05:02 GMT
// Enable postback for the dropdown

// in page load event

int ndx = page.request("DropDownList1");

// Now do you databinding

// then restore the selected index

DropDownList1.SelectedIndex = ndx;

Signature

aaa

> Hi,
>
[quoted text clipped - 41 lines]
>
> JW
jwnews1231@gmail.com - 01 Feb 2008 14:02 GMT
Thanks alot, that fixed it.  I didn't think to check the Request's
Form variable for the UniqueID of the control.

JW

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.