473,474 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Create 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.EventArgs) Handles MyBase.Init

DropDownlist1 = New DropDownList

Databindlist(DropDownlist1)

DropDownlist1.EnableViewState = False

PlaceHolder1.EnableViewState = False

PlaceHolder1.Controls.Add(DropDownlist1)

Button1 = New Button

Button1.Text = "Postback"

Button1.EnableViewState = False

Placeholder2.Controls.Add(Button1)

Placeholder2.EnableViewState = False

End Sub

#End Region

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

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

End If

End Sub

Public Sub Databindlist(ByVal 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(li)

Next

End Sub
Jan 9 '07 #1
1 2989
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.EventArgs) Handles MyBase.Init

DropDownlist1 = New DropDownList

Databindlist(DropDownlist1)

DropDownlist1.EnableViewState = False

PlaceHolder1.EnableViewState = False

PlaceHolder1.Controls.Add(DropDownlist1)

Button1 = New Button

Button1.Text = "Postback"

Button1.EnableViewState = False

Placeholder2.Controls.Add(Button1)

Placeholder2.EnableViewState = False

End Sub

#End Region

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

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

End If

End Sub

Public Sub Databindlist(ByVal 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(li)

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
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...
2
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...
3
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...
6
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...
7
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...
2
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...
2
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...
3
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...
0
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...
0
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,...
1
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...
0
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...
0
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.