473,396 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

viewstate possible bug?

I am trying to change a dropdown's value through code on a postback but the
framework seems to override my changes with the value posted. This only
happens when adding controls though code. Here is a code example of the
problem:

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

Dim intValue As Integer = 1
intValue = Request.Item("list")

Dim a As New DropDownList
With a
.ID = "list"
.AutoPostBack = True
.EnableViewState = False
.Items.Add(New ListItem("One", 1))
.Items.Add(New ListItem("Two", 2))
.Items.Add(New ListItem("Three", 3))
End With

'change value which should override the posted value
a.SelectedValue = 3

CType(Me.FindControl("Form1"), Object).controls.Add(a)
End Sub
Here is a work around which prevents the framework from matching the control
on post back by changin the control's ID on every post thus preventing the
value override. It works but is ugly and wish someone can show me the
correct to achieve this:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim intValue As Integer = 1
If Not Viewstate("guid") Is Nothing Then
intValue = Request.Item("list" & Viewstate("guid").ToString)
End If

Dim a As New DropDownList

Dim g As Guid
g = Guid.NewGuid
Viewstate("guid") = g

With a
.ID = "list" & g.ToString
.AutoPostBack = True
.EnableViewState = False
.Items.Add(New ListItem("One", 1))
.Items.Add(New ListItem("Two", 2))
.Items.Add(New ListItem("Three", 3))
End With

'change value which overrides the posted value
a.SelectedValue = 3

CType(Me.FindControl("Form1"), Object).controls.Add(a)
End Sub

Thanks
Perry
Nov 18 '05 #1
2 1081
The following MSDN article explains the Control Execution Lifecycle, which
should help you figure out the sequence issue you're having.

http://msdn.microsoft.com/library/de...nLifecycle.asp

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Perecli Manole" <Pe*****@dslextreme.com> wrote in message
news:#t**************@tk2msftngp13.phx.gbl...
I am trying to change a dropdown's value through code on a postback but the framework seems to override my changes with the value posted. This only
happens when adding controls though code. Here is a code example of the
problem:

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

Dim intValue As Integer = 1
intValue = Request.Item("list")

Dim a As New DropDownList
With a
.ID = "list"
.AutoPostBack = True
.EnableViewState = False
.Items.Add(New ListItem("One", 1))
.Items.Add(New ListItem("Two", 2))
.Items.Add(New ListItem("Three", 3))
End With

'change value which should override the posted value
a.SelectedValue = 3

CType(Me.FindControl("Form1"), Object).controls.Add(a)
End Sub
Here is a work around which prevents the framework from matching the control on post back by changin the control's ID on every post thus preventing the
value override. It works but is ugly and wish someone can show me the
correct to achieve this:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim intValue As Integer = 1
If Not Viewstate("guid") Is Nothing Then
intValue = Request.Item("list" & Viewstate("guid").ToString)
End If

Dim a As New DropDownList

Dim g As Guid
g = Guid.NewGuid
Viewstate("guid") = g

With a
.ID = "list" & g.ToString
.AutoPostBack = True
.EnableViewState = False
.Items.Add(New ListItem("One", 1))
.Items.Add(New ListItem("Two", 2))
.Items.Add(New ListItem("Three", 3))
End With

'change value which overrides the posted value
a.SelectedValue = 3

CType(Me.FindControl("Form1"), Object).controls.Add(a)
End Sub

Thanks
Perry

Nov 18 '05 #2
I have read this article before and does not explain what is occuring here.

The document under the OnLoad states: At this point, server controls in the
tree are created and initialized, the state is restored, and form controls
reflect client-side data.

Well, as you can see from my example, I overwrote the OnLoad event. What do
I need to do to prevent posted values from overitting the values I set in
the OnLoad event.

Perry
"Kevin Spencer" <ks******@takempis.com> wrote in message
news:u%****************@TK2MSFTNGP11.phx.gbl...
The following MSDN article explains the Control Execution Lifecycle, which
should help you figure out the sequence issue you're having.

http://msdn.microsoft.com/library/de...nLifecycle.asp
--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Perecli Manole" <Pe*****@dslextreme.com> wrote in message
news:#t**************@tk2msftngp13.phx.gbl...
I am trying to change a dropdown's value through code on a postback but

the
framework seems to override my changes with the value posted. This only
happens when adding controls though code. Here is a code example of the
problem:

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

Dim intValue As Integer = 1
intValue = Request.Item("list")

Dim a As New DropDownList
With a
.ID = "list"
.AutoPostBack = True
.EnableViewState = False
.Items.Add(New ListItem("One", 1))
.Items.Add(New ListItem("Two", 2))
.Items.Add(New ListItem("Three", 3))
End With

'change value which should override the posted value
a.SelectedValue = 3

CType(Me.FindControl("Form1"), Object).controls.Add(a)
End Sub
Here is a work around which prevents the framework from matching the

control
on post back by changin the control's ID on every post thus preventing the value override. It works but is ugly and wish someone can show me the
correct to achieve this:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim intValue As Integer = 1
If Not Viewstate("guid") Is Nothing Then
intValue = Request.Item("list" & Viewstate("guid").ToString)
End If

Dim a As New DropDownList

Dim g As Guid
g = Guid.NewGuid
Viewstate("guid") = g

With a
.ID = "list" & g.ToString
.AutoPostBack = True
.EnableViewState = False
.Items.Add(New ListItem("One", 1))
.Items.Add(New ListItem("Two", 2))
.Items.Add(New ListItem("Three", 3))
End With

'change value which overrides the posted value
a.SelectedValue = 3

CType(Me.FindControl("Form1"), Object).controls.Add(a)
End Sub

Thanks
Perry


Nov 18 '05 #3

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

Similar topics

2
by: James Glover | last post by:
I need help... I am trying to set the Viewstate of a var on an onclick event and let the page post back to get the new value. When i click the button, the first time though (on the post back) it...
2
by: Ben Rush | last post by:
Hello World, Okay, I have spent the day browsing the newsgroups and reading up on article after article concerning ViewState corruption and so forth, and I have a couple questions. We...
6
by: clsmith66 | last post by:
Is it possible to store the same information about a control that would be saved in the ViewState in a Session state? I have a page with three treeview controls and if I enable the view state for...
3
by: RCS | last post by:
I have an app that I have different "sections" that I want to switch back and forth from, all while having the server maintain viewstate for each page. In other words, when I am on Page1.aspx and...
9
by: Mark Broadbent | last post by:
Been a while since I've touched asp.net but one thing that always seems to fustrate me is the loss of state on variable declarations. Is there anyway (i.e. assigning an attribute etc) to instruct...
2
by: epigram | last post by:
I'm responding to a button click event on an asp.net web form. I then need to retrieve the value from a TextBox control and I want to compare it against the control's previous value to see if it...
28
by: Vishwanathan Raman | last post by:
Hi I am aware of the technical differences.But would like to get it clarified.Given an oppurtunity is ViewState storage more efficient than Session based.I do not have a scenario where I need to...
10
by: Robert | last post by:
I have an app that was originally 1.1, now migrated to 2.0 and have run into some sporadic viewstate errors...usually saying the viewstate is invalid, eventvalidation failed or mac error. My web...
2
by: Jarod | last post by:
Hey I change backColor of linkButton in my Page_Load: lbDoSth.BackColor = Color.Red; But I have multiView on this page. On one of the views I have detailsView. When I add a new row a set...
9
by: =?Utf-8?B?TUNN?= | last post by:
I'm sure the answer to my question varies depending on the situation, but I am looking for a general "best practice". If I have an asp.net application and I load certain data from a database,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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,...
0
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...
0
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
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,...

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.