VB.NET persistent cookie exception - asp.net

I am attempting to add the text that a user types into the textbox 'textMain' to a cookie. I am trying to do this so there is no need to click a 'Save' button. Therefore a user could type a sentence and then close the browser and it would be saved. Here is the VB code; done on the load handler and the unload handler.
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
If Request.Cookies("content") IsNot Nothing Then
textMain.Text = Request.Cookies("content").Value.ToString
End If
End If
End Sub
Protected Sub Page_Unload(sender As Object, e As EventArgs) Handles Me.Unload
'' If Request.Cookies("content") Is Nothing Then
Dim contentCookie As New HttpCookie("content")
contentCookie.Value = textMain.Text
contentCookie.Expires = DateTime.MaxValue
Response.Cookies.Add(contentCookie)
'' End If
End Sub
The exception comes on the Respinse.Cookies.Add(contentCookie) line.
Thanks!

Related

how to access textbox value from another webpage in asp.net

i want to display text box value to another text box from one webpage to another in a button click.
i know the windows code But don't know the web application.
public qus as form = new question()
qus.txtname=txtname.text
On first page add button click event
Sub Btn_Click(sender As Object, _
e As EventArgs)
Response.Redirect("SecondPage.aspx?id=" + txtname.text, false)
End Sub
on second page set your text on page load.
Private Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack
SecondPageTextBox.Text = Request.QueryString("textValue").ToString()
End If
End Sub
Use the query string to pass the text value to other page in the button click handler, like this:
Protected Sub Button1_Click(sender As Object, e As EventArgs)
Response.Redirect("OtherPage.aspx?textValue='Value from other page'")
End Sub
Now in the Page_Load of the other page, pull the query string value out and assign it to the text box, like this:
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Request.QueryString("textValue") IsNot Nothing Then
YourTextBox.Text = Request.QueryString("textValue").ToString()
End If
End Sub
You have to write code on first page
Protected Sub Button1_Click(sender As Object, e As EventArgs)
Response.Redirect("webpage2.aspx?textValue='value'")
End Sub
On second page
Private Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack
SecondPageTextBox.Text = Request.QueryString("textValue").ToString()
End If
End Sub

Web Controls Not Talking to Each other in ASP.NET using VB.NET

I have an asp.net page that loads two controls, Control A and Control B. Control A has some generic form submit and clear buttons that trigger click events in its' own code behind which use reflection to call the update function in Control B which has a few input fields. I have debugged this and everything seems to be in order, however; when the update function in control B is called the input fields are not returning a value when using inputname.text or me.inputname.text. Does anyone have any ideas why this is not working? Any guidance would be appreciated.
This is the code in Control A's codebehind which calls the update method in Control B's code behind
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
Try
Dim lctlControl = Session("SelectedQstnCtl")
Dim methodObj = lctlControl.GetType().GetMethod("UpdateGenInfo", BindingFlags.NonPublic Or BindingFlags.Instance)
' Execute UpdateGenInfo method to update the data
methodObj.Invoke(lctlControl, Nothing)
Catch ex As Exception
'TODO: check for concurrency error here
End Try
End Sub
This is the update function in Control B that is being called. The session values are being passed, but the form fields are not.
Protected Sub UpdateGenInfo()
Dim lclUtil As New clUtility
Dim genInfo As New clGenInfo
Try
Dim dt As Integer
'Update Data for 1-2
dt = genInfo.UpdateGenInfo_E1_01_02(Session("ConnStrEP"), Me.varLastUpdate, Session("AppNo"), Session("RevNo"), _
Me.txtPrName.Text, Me.txtPrAddr1.Text, Me.txtPrAddr2.Text, _
Me.txtPrCity.Text, Me.txtPrState.Text, Me.txtPrZip.Text)
Catch ex As Exception
'Display error
lclUtil.DisplayMsg(Me.lblErrMsg, String.Format("Error Location: Sub LoadGenInfo (ctlE1_01_02) {0}", ex.Message))
End Try
End Sub
The most likely cause is that the control instance stored in the session is not the control instance on the current page. For example, if you're storing the control instance in the session when the page is first loaded, and retrieving it on post-back, it will be a different instance.
If you can't give Control A a direct reference to Control B, then change your code to store the reference in the Page.Items collection:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Page.Items("SelectedQstnCtl") = TheSelectedQstnCtl
End Sub
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
Dim lctlControl = DirectCast(Page.Items("SelectedQstnCtl"), YourControlClass)
lctlControl.UpdateGenInfo()
End Sub
I see you are using reflection which might be an overkill for that task.Try referencing the method in the control directly.Make then method UpdateGenInfo public and then reference it like this.
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
Try
Dim lctlControl = CType(Session("SelectedQstnCtl"),YourControlClass)
lctlControl.UpdateGenInfo()
Catch ex As Exception
End Sub
Public Function UpdateGenInfo()
'your code here
Catch ex As Exception
End Try
End Function
This way you can easily trace where your values are getting lost.Let me know how it goes.
Try yet another simple approach working demo here
In control a
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim testb1 = CType(Me.NamingContainer.FindControl("testb1"), testb)
testb1.UpdateGenInfo()
End Sub
In control b
Public Function UpdateGenInfo()
Try
Dim a = Me.TextBox1.Text
Catch ex As Exception
End Try
End Function
Aspx Parent Page
<uc1:testa ID="testa1" runat="server" />
<uc2:testb ID="testb1" runat="server" />
The controls in testb are in an update panel.Try this and let me know if this works.

Write a form in VB.NET that will be the same state when you return to it

I'm trying to make a form that when it is return to, it will have the same state for the controls
I have 3 .net pages, testa,testb,testc
testa, sets up the session and has a button to go to testb. testb has a check box and a button to go to testc. testc has a button to return to page testb.
I want it so when we leave testc, and then come back the check mark will be in the same state, but it is not. it stays as uncheck when I go from page testc back to page testb
This is what i did,
On the page load event, I set the check box equal to the "button" Session variable. On the button event I save the button checked state in the Session variable and call page testc
Code
Page A
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
Server.Transfer("testb.aspx")
End Sub
Page b
Partial Class testb
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim t As Boolean
t = Session.Item("button")
CheckBox1.Checked = t
End Sub
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
Dim t As Boolean
t = CheckBox1.Checked
Session.Item("button") = t
Server.Transfer("testc.aspx")
End Sub
End Class
Page c
Partial Class testc
Inherits System.Web.UI.Page
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
Server.Transfer("testb.aspx")
End Sub
End Class
Data in the Session is stored as an Object. What you'll need to do is get the value from the Session and then convert it to a Boolean if it's not null (Nothing in VB). We've used this technique before:
Dim rawValue As Object = Session.Item("button")
Dim realValue as Boolean
If rawValue IsNot Nothing Then
realValue = Boolean.Parse(rawValue.ToString())
End If
It sounds like you need to check the IsPostBack property in the Page_Load method to decide if the values should be loaded or not. When it's a postback, don't load from the Session.
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim t As Boolean = Session.Item("button")
CheckBox1.Checked = t
End If
End Sub

View state after postback VB?

I have a label's value that is posted back from a previous page and I want to use this label to do a simple calculation in the current page, but when I do the calculation the page (refreshed) the value of this label and automatically deleted the value (Since there would be no value in postback when it refreshed).
Here is the code behind file:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label2.Text = Request.Form("Hidden1")
End Sub
and here where I want to use the label
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Stotal As String
Stotal = Val(Label2.Text) * 10
Label3.Text = Stotal
End Sub
How can I save the value in the page via view state or any other method? Thanks in advance
Unless you've disabled ViewState on your page, the problem isn't that your label's ViewState isn't being saved, it's that you're overwriting it in your Page_Load method on the postback since the form variable Hidden1 is no longer being posted to your page from the previous page. Try:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack
Label2.Text = Request.Form("Hidden1")
End If
End Sub

My Session Value is Not Changing

Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack = True Then
Session("x") = "ABC"
End If
End Sub
Protected Sub btnABC_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnABC.Click
Session("x") = "ABC"
End Sub
Protected Sub btnCBA_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCBA.Click
Session("x") = "CBA"
End Sub
Protected Sub btnShow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnShow.Click
TextBox1.Text = Session("x")
End Sub
End Class
Three buttons-ABC,CBA and Show.
if you click on ABC and then Click on Show button The textbox shows "ABC"
but when I Clicking on CBA button And then Click on Show button The textbox shows again "ABC". IsPostback property will true on each time the page is posted to the server.
So the session reset the value.
how to overcome this issue ????
If you set the value in page_load(), this assignment occurs every time you load the page. Maybe you want to set this value only at the first call of the page:
If IsPostback = False Then
Session("x") = "Something"
End If
The second load of the page will not overwrite the value you set in button1_click.
When you press the show button it causes a postback to the server. The Page_Load method fires first, and you assign "ABC" into Session("x"). Then you're putting Session("x") into into the textbox.
What you'd probably want is this instead:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Session("x") = "ABC"
End If
End Sub
Besides what other people wrote above, I also recommend you to assign Session values during Page_InitComplete event. Because mostly developers work in Page_Load stage and some times assigning Session values as well may throw errors because of it. It is like which one is happening before or after. You can make mistakes and so on.

Resources