473,594 Members | 2,651 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How does this example retain selectedItem on postback?

RSH
Hi,

I am experimenting with the Viewstate and based on a few articles I have
read, I put together a test. it is a simple test where I am dynamically
creating a DropDownList that contains 25000 list items. When I don't create
the control dynamically, I get a viewstate that is roughly 1 mb in size!
However, when i use the code below the viewstate is 0 bytes even on
postback. Impressive size difference. Now where I get perplexed is even
though the viewstate is set to off in the creation of the dynamic control,
the Dropdownlist retains the correct SelectedItem, and furthermore i can
even reference the control to bring back it's Selecteditem property and it
returns correctly...but with Trace=true set on the page i can see that the
viewstate = 0 bytes on this control. My question is if indeed the viewstate
is disabled for this control (everything points to this) then how is the
Control retaining the correct SelectedItem upon Postback???

Thanks,
Ron
Protected Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Init

DropDownlist1 = New DropDownList

Databindlist(Dr opDownlist1)

DropDownlist1.E nableViewState = False

PlaceHolder1.En ableViewState = False

PlaceHolder1.Co ntrols.Add(Drop Downlist1)

Button1 = New Button

Button1.Text = "Postback"

Button1.EnableV iewState = False

Placeholder2.Co ntrols.Add(Butt on1)

Placeholder2.En ableViewState = False

End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

Label1.Text = DropDownlist1.S electedItem.Tex t <-- some how this line sets
the correct SelectedItem whether it is a postback or not

End If

End Sub

Public Sub Databindlist(By Val ddl As DropDownList)

Dim li As ListItem

Dim i As Integer

For i = 0 To 25000

li = New ListItem

li.Text = "test " & i

li.Value = "test " & i

ddl.Items.Add(l i)

Next

End Sub
Jan 9 '07 #1
1 2998
the viewstate is used to regenerate the list on postback, not the value
(thats sent by the browser). if you turn viewstate off, then in oninit
you just need to regerate the list options. then when the load postback
data event is fired (just before the page onload event but after onint)
the option value will be found.

note: this works with static or dynamic controls. i always turn
viewstate off.
-- bruce (sqlwork.com)

RSH wrote:
Hi,

I am experimenting with the Viewstate and based on a few articles I have
read, I put together a test. it is a simple test where I am dynamically
creating a DropDownList that contains 25000 list items. When I don't create
the control dynamically, I get a viewstate that is roughly 1 mb in size!
However, when i use the code below the viewstate is 0 bytes even on
postback. Impressive size difference. Now where I get perplexed is even
though the viewstate is set to off in the creation of the dynamic control,
the Dropdownlist retains the correct SelectedItem, and furthermore i can
even reference the control to bring back it's Selecteditem property and it
returns correctly...but with Trace=true set on the page i can see that the
viewstate = 0 bytes on this control. My question is if indeed the viewstate
is disabled for this control (everything points to this) then how is the
Control retaining the correct SelectedItem upon Postback???

Thanks,
Ron
Protected Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Init

DropDownlist1 = New DropDownList

Databindlist(Dr opDownlist1)

DropDownlist1.E nableViewState = False

PlaceHolder1.En ableViewState = False

PlaceHolder1.Co ntrols.Add(Drop Downlist1)

Button1 = New Button

Button1.Text = "Postback"

Button1.EnableV iewState = False

Placeholder2.Co ntrols.Add(Butt on1)

Placeholder2.En ableViewState = False

End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

Label1.Text = DropDownlist1.S electedItem.Tex t <-- some how this line sets
the correct SelectedItem whether it is a postback or not

End If

End Sub

Public Sub Databindlist(By Val ddl As DropDownList)

Dim li As ListItem

Dim i As Integer

For i = 0 To 25000

li = New ListItem

li.Text = "test " & i

li.Value = "test " & i

ddl.Items.Add(l i)

Next

End Sub

Jan 10 '07 #2

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

Similar topics

0
2294
by: Jax | last post by:
I have a listbox. When the user selects an item i want to to take the text of the item and manipulate it. It wasn't working when autopostback was false, now it's true the event runs but i get an error when tries to access the selecteditem property of the listbox. I've gone through debugger and the tasks are in the items collections but i cant access those from the object instance. Am i using the wrong property, do i not understand the...
2
4880
by: theo | last post by:
Hi... I wish to extract the text content of an Xml file and assign it to DropDownList controls at runtime.I can get the Xml file text content into the DropDownList controls (Ex...if 5 Xml text tags then 5 dropdownlist controls each containing the 5 Xml text tags). Problem,when I save the user DropDownList selected items by means of a button click the web form looses the PlaceHolder viewstate and generates the following error..."Object...
3
1600
by: Karl | last post by:
Hi there, I am using asp.net with c#. I created a web user control "MyControl". When I click a button, page creates an instance of "MyControl" and loads it at run time. It is good. And the "MyControl" has a button in it which can do some magic work. But when I click this button in MyControl, page reloads and "MyControl" disappears. I know "Mycontrol" is created at run time and when page reloads, the creating event is not triggered. It is...
6
5658
by: KathyB | last post by:
Hi, On Page Load (if not postback), the user selects a choice from dropdownlist1. On SelectedItemChanged for dropdownlist1, dropdownlist2 is populated and the user selects an item. I cannot find a combination where I can RETAIN both values during postback...if I put if not posback on the change event of
7
11179
by: Ohad Asor | last post by:
Hello all, I have an ASP.NET page I've written using VS.NET2003, which have a ListBox in it. When I press a button in the form, I try to get the selected item in the list by calling ListBox.SelectedItem. And it returns null, even when I select an item. Even when I try to read the value when I get the SelectedIndexChanged event, it still doesn't give me the correct answer.
2
4653
by: John Blair | last post by:
Hi, I have a dropdownlist in a grid header template - i have autopostback on but when i check the selectedindex in the page_load event (i have a routine which identifies the control that caused the postback and it determines the dropdownlist correctly) it is always 0 - i am NOT rebinding to the dropdownlist in the page_load event i.e. only once if !postback. I also have viewstate set to true for the control and the grid it is in! The...
2
2105
by: Vinay | last post by:
Hi All: I have an aspx page that has 2 dropdownlists. I want to populate the second dropdownlist based on the selecteditem in the first dropdown. I know I can do this by doing a post back and using the selectedindex and loading the second dropdownlist. Is there anyway I can do this without doing a postback? Any ideas/pointers will be much appreciated.
3
1881
by: Frustrated Developer via DotNetMonster.com | last post by:
I have developed a form that would allow the user to load and search a database several ways, by data range, by specific number or all database entries. I can successfully load any of the ways once. But when I click a button labeled "Clear Form" and then attempt to load data again I get the following error message: An unhandled exception of type 'System.ArgumentException' occurred in system. windows.forms.dll Additional information:...
0
7941
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7874
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
8246
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
8368
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
8000
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
6652
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
5404
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
3854
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
2383
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.