473,804 Members | 2,252 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DropDownList & Viewstate

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(EventArg s 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(EventArg s e) {
if (!Page.IsPostBa ck) {
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
Jan 31 '08 #1
2 6366
// Enable postback for the dropdown

// in page load event

int ndx = page.request("D ropDownList1");

// Now do you databinding

// then restore the selected index

DropDownList1.S electedIndex = ndx;
--
aaa
"jw********@gma il.com" wrote:
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(EventArg s 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(EventArg s e) {
if (!Page.IsPostBa ck) {
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
Feb 1 '08 #2
Thanks alot, that fixed it. I didn't think to check the Request's
Form variable for the UniqueID of the control.

JW

Feb 1 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
280
by: James Holloway | last post by:
I have written a collection that generates user information, ie username, password. When used in a Win environment the class works perfectly well, it is only when i attempt to use the class in a web environment i start getting problems. On the load the collection is populated from the database and subsequently a drop down list is loaded from a further button click. I am using 'If Not IsPostBack' and therefore on the button click all...
0
1300
by: Nir | last post by:
Hello all , I have a very strange problem with DropDownList . I am using this Code for select items in Drop Down : Dim Item As New ListItem() Item = cmb.Items.FindByValue(Data) cmb.SelectedIndex = cmb.Items.IndexOf(Item) Item = Nothing
6
7028
by: Robin Bonin | last post by:
In my user contol I am creating a set of dropdownlists. Each list is created based on input from the other lists. The problem I am having is setting the selected index on the lists. If someone changes box1, I want to set the selected index in box2 = 0. When I do this, I dont get an error, but when the page loads, it still has the selected value and not 0. It seems that it is getting the selected value from the viewstate
5
2079
by: Bob N | last post by:
I have an ASP.NET web page -- several auto-post back DropDownLists that perform a query against a relatively large database (3 to 4 seconds delay) that repopulate a couple of datagrids, comboboxes and a chart (using dotnetCharting control) on the same page. This works just fine on its own. However, when I integrated the page into the rest of the site, it is then placed inside a frameset. Again, the page seems to work fine EXCEPT under the...
4
8074
by: dm_dal | last post by:
Is there a know issue surrounding the CheckBoxList control and it's viewstate? When my control is created, it's ListItems are checked as needed, but on a postback, they loose their Selected status. David
1
1369
by: Neven Klofutar | last post by:
Hi, I have a problem, and it seems I have an error in my logic ... First I add a Table control on my webform (EnableViewState is True by default). Then I retrive some information vie QueryString (in the !isPostBack part of the Page_Load event) and use those information (in code behind, in !isPostBack part of Page_LOad event) to add some rows and cells to Table I added moment ago.
0
1259
by: Mutley | last post by:
I am finding an issue with the current DropDownList and ListBox ASP.NET Web Controls. I had a system set up for Arabic and a string that I put into a drop down contained a number. The letters were being displayed in Arabic but the numbers were appearing as 1,2,3, etc. In other parts of my app, the Arabic equivalent to these numerals would appear as expected. I found an MSDN link to the following web page that mentioned there was an issue...
2
4837
by: BhumikaP | last post by:
Hiiiiiiii, can u guide me on how to Populate data in GridView on the basis of selection made in a Dropdown list. Like 1st the user will select an item from dropdown & the GridView should show on that data which is releted to the value selected by user in Dropdown & all other rows should not be displayed Plz rpl
3
2372
by: RPhlb | last post by:
This is my first post, so excuse me if I don't get this right the first time. I have an issue where when I use DropDownList I only get the first, or "SelectedValue" back when I update a GridView Edit. I have been trying to hunt down a viable soltion to this, seems that many people out there have similar issues and the ...SelectedItem.Value or SelectedValue is not returning what is actually being selected by the end user. So the simple question...
0
9594
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10599
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10346
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10347
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10090
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9173
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6863
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5531
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.