开发者

User enters code into TextBox, code gets added to URL (using session)

开发者 https://www.devze.com 2023-04-01 23:21 出处:网络
I\'m using ASP.net 4.0 VB :) I am using a session variable to add a user entered code into the url of each page. I can get the code to show up at the end of the page\'s URL that my textbox is on, but

I'm using ASP.net 4.0 VB :)

I am using a session variable to add a user entered code into the url of each page. I can get the code to show up at the end of the page's URL that my textbox is on, but what do I add 开发者_开发技巧to every page to make sure that the session stays at the end of every URL that person visits during their session? This is the code from the page that the user enters their user code.

  Protected Sub IBTextBoxButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles IBTextBoxButton.Click
  Session("IB") = IBTextBox.Text
  Dim IB As String = Session("IB")
End Sub


 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 ProductID.Value = Request.QueryString("id")
 If Session("IB") Is Nothing Then
     tab4.Visible = "False"
 Else
     tab4.Visible = "True"

 End If
End Sub

This is what I have in the page load of one of the other pages. What else do I add to make sure that variable is added to the URL of that page?

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim IB As String
    IB = Session("IB")
End Sub


  string url = Request.Url.ToString();
  string newUrl = url + url.Contains("?") ? "&" : "?" + "ib=" + Server.UrlEncode(IBTextBox.Text);
  Response.Redirect(newUrl);
  return;


The approach I might use would be to create a base page class that all of your pages can inherit. The base page would then inherit the System.Web.UI.Page.

Within your base page class, create a property for IB and also handle the page load event.

In that event, check if the QueryString has the IB parameter in it. If it does, set the property to the value in the parameter.

Private _IB As String
Public Property IB() As String
    Get
        Return _IB
    End Get
    Set(ByVal value As String)
        _IB = value
    End Set
End Property

Public Function GetIB(ByVal url As String) As String
    If Not(_IB = String.Empty) Then
        If (url.Contains("?")) Then
            Return "&IB=" & _IB
        Else
            Return url & "?IB=" & _IB
        End If
    Else
        Return url
    End If
End Function

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not (String.IsNullOrEmpty(Request.QueryString("IB"))) Then
        _IB = Request.QueryString("IB")
    End If
End Sub

Finally in your markup you would need to place something like the following at the end of all of your links:

<a href="<% =GetIB("nextpage.aspx") %>">next page</a>


I threw this code into the Master Page to make sure that every page knows whether or not the Session is there. Thanks for all the help everyone!

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) 
Handles Me.Load
  If Session("IB") Is Nothing Then

    IBText.Visible = True
    IBTextBox.Visible = True
    IBTextBoxButton.Visible = True
  Else

    IBText.Visible = False
    IBTextBox.Visible = False
    IBTextBoxButton.Visible = False
    lblIB.Visible = True

    lblIB.Text = "Welcome, " + Session("First_Name") + " " + Session("Last_Name")
  End If
End Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消