postback


passing values using viewstate across postback in multiview


I have a multiview and have 2 views inside it. I am going to paste a sample code.
<asp:MultiView ID="MultiView1" ActiveViewIndex="0" runat="server">
<asp:View ID="View1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="view1" />
<asp:Label ID="Label2" runat="server" ></asp:Label>
</asp:View>
<asp:View ID="View2" runat="server">
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="view2" />
</asp:View>
</asp:MultiView>
I want the value in txtbox1 to be there in postback. Though multiviews maintain state i do response.redirect to pass querystring to view2. Since i do postback i cannot use the value in txtbox1(in view1), in view2. The value in txtbox1 becomes null during postback. I tried the following code
Public Partial Class viewstatetest
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Not Page.IsPostBack()) Then
MultiView1.ActiveViewIndex = 0
Else
TypedPassword = TextBox1.Text
TextBox1.Attributes.Add("value", TypedPassword)
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
MultiView1.ActiveViewIndex = 1
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
MultiView1.ActiveViewIndex = 0
Label1.Text = TextBox1.Text
Response.Redirect("viewstatetest.aspx")
End Sub
Public Property TypedPassword() As String
Get
If (ViewState("TypedPassword") IsNot Nothing) Then
Return CStr(ViewState("TypedPassword"))
End If
Return ""
End Get
Set(ByVal value As String)
ViewState("TypedPassword") = value
End Set
End Property
End Class
When the page loads for the first time, i type something in txtbox1 in view1 and click on the button, view2 is loaded and i have a code that gets the value of the txtbox1 and writes the value inlabel1 in view1. And when i do response.redirect the textbox1 becomes null and view also become null.
Why there is no value in viewstate?
Thanks!
Asp.Net viewstate is very different from normal get/post browser requests in other systems such as Rails, PHP, or even Asp.Net MVC.
Here is what is going on in your scenario:
User's browser does HTTP get on page for first time. This is not a postback.
User fills in TextBox1 and clicks Button1. This is a postback (HTTP post).
On the server, the information in the encrypted hidden __VIEWSTATE variable is unpacked and compared with the new values for TextBox1 and Button1 according to the post data in the request.
Asp.Net identifies state changes and fires events in your server code. This results in the change of the active view index to 1.
The browser now receives the page with View2 shown.
User hits Button2. This is a postback (HTTP post).
The server goes through the same process as before comparing viewstate to post data and fires the Button2 click event.
Your code now does something different. When you do a Response.Redirect, the server does not send back the page with new viewstate. You are sending a short header without the preserved viewstate information in the hidden form variable. The redirect forces the browser to immediately do an HTTP get operation to the url specified. THIS IS NOT A POSTBACK and the view state has been lost. It is exactly the same scenario as the first HTTP get from the user's browser in step 1.
I hope this helps. I think it is safe to admit that Microsoft has recognized the drawbacks from their viewstate model in Asp.net. It makes it quite difficult to implement sexy, modern ajax applications where the browser is maintaining most of the application state and just wants to make small requests for new data from the server. I think this is one of the primary motivations for abandoning the viewstate model in Asp.Net MVC.

Related Links

Selenium2 WebDriver (Page factory) error after a postback (Element not found in the cache)
converting time in query using to_char
How do I find my C# panel controls?
Get TextBox values entered before postback
CalendarExtender loose his value on postback
passing values using viewstate across postback in multiview
How do I do an http request upon successful Inno Setup install, for tracking purposes?
calculate sum of two numbers in asp.net without postback
Textbox inside a Repeater inside a Custom Server Control loses values
client-side values of dynamic control on postback
postback (a la ASP.NET) in other frameworks?
Cant assign values to variable in a user control on ajax postback… or something like that!
How to stop Telerik RadWindow from always reloading on the next PostBack
sending to paypal the “custom” variable on saved buttons ipn question
viewstate in reporting services
Keeping track of refinements made to page using ASPX (Breadcrumb trail)?

Categories

HOME
go
oracle12c
gridview
checkstyle
jmeter
pda
mysql-workbench
serialization
apiblueprint
opendj
google-apps
cakephp-3.4
big-o
retrofit2
recordrtc
elasticsearch-5
text-mining
java.util.logging
redis-sentinel
datastage
xcodebuild
supervisor
flash-player
atlassian
selectize.js
symbol
powershell-v5.0
microsoft-dynamics-nav
vuejs
asp.net-mvc-viewmodel
foreign-keys
nim
apache2.4
asciidoctor-pdf
ag
r-mice
inno-download-plugin
google-api-client
has-and-belongs-to-many
linked-data
firebase-analytics
android-things
constexpr
record
left-join
testbed
angularjs-material
openwhisk
video-capture
modernizr
cin
subclipse
microsoft-metro
uiimage
plan-9
vaadin-elements
user-defined-fields
arq
context-sensitive-grammar
er-diagram
bbpress
rxtx
ziparchive
parfor
pacemaker
macaulay2
simevents
boost-serialization
upx
lz77
beanstalk
findfirst
toast
ascii-art
rasterize
jbase
decimalformat
grgit
page.js
sapscript
visual-sourcesafe-2005
xmltype
gamekit
uncaught-typeerror
ssis-data-flow
encryption-asymmetric
mdichild
jqmodal
grails-domain-class
authlogic
drupal-nodes
http-patch
inappsettingskit
observium
flex++
process-explorer
css-reset
chart-director
virtual-pc
wimax
web-notifications
joomla3.1
veracity
boost-signals
thttpd
kqueue
floating-point-exceptions
drawtobitmap
rmdir
mbeans
silent
object-database
interprocess
build-environment
application-planning
flvplayback
soappy
wcf-proxy

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App