How to store date value into viewstate in vb.net | c#.net with explanation and example in asp.net.
How to check whether viewstate is null or have some values.
It is very simple to store value and retrieve it on another place.
we can store datatable , simple variable in viewstate to fetch same data later
on whenever we need to use that respective data.
so coming on point i am goinG to write code how to store date value or data time into viewstate and display it.
ViewState("fromdate") = controlName1.SelectedDate
ViewState("Todate") = controlName2.SelectedDate
NOW Here i am assigning data value into viewstate named fromdate and todate.
in c#
ViewState["fromdate"]= controlName1.SelectedDate
ViewState["Todate"]= controlName2.SelectedDate
now i have store from date value and to date value into viewstate named as
fromdate and todate both are in doublw quotes.now i want to retrieve that same value into another function.
what i will do , i will just take control name and assign viewstate value there.
Dim lbl As New Label
lbl.Text = CType(ViewState("frm"), Date)
so , label will display data value.
now in c#
lbl = New Label()
lbl.Text = CType(ViewState["frm"], Date);
just change that bracket style.
so if you want to use same variable after postback use viewstate .
less burdon on server, because viewstate is client side state management.once page unloaded viewstate destroy.
after if you want to check whether viewstate has value or not you can write code like this
Dim lbl As New Label
If ViewState("frm") = CType("12:00:00 AM", Date) Then
lbl.Text = "something"
Else
lbl.Text = CType(ViewState("frm"), Date)
Response.Write(lbl.Text)
End If
Try it and implement it.
0 comments: