How to create a link to a website in vb code behind? - asp.net

It may be simple but the search words in Google give too many irrelevant results.
Protected Sub Menu1_MenuItemClick(sender As Object, e As MenuEventArgs) Handles Menu1.MenuItemClick
If e.Item.Text = "SomeItem" Then
'The link goes here
End If
End Sub

Use Response.Redirect if you want to send the current page to a new url:
Protected Sub Menu1_MenuItemClick(sender As Object, e As MenuEventArgs) Handles Menu1.MenuItemClick
If e.Item.Text = "SomeItem" Then
Response.Redirect("http://www.stackoverflow.com")
End If
End Sub
To open a new url in a new window/tab you would have to use javascript. Normally I would recommend just putting the javascript directly onto the aspx page but in the event that the url will use data from the code behind to generate the url you can use the ClientScript.RegisterStartupScript function.
Protected Sub Menu1_MenuItemClick(sender As Object, e As MenuEventArgs) Handles Menu1.MenuItemClick
If e.Item.Text = "SomeItem" Then
Dim sURL As String = "http://www.stackoverflow.com"
ClientScript.RegisterStartupScript(Me.GetType(), "script", "window.open('" & sURL + "', 'popup_window');", True)
End If
End Sub

Related

VB.NET display label text only on default.aspx page only

Have a message that currently displays on every page of our website. I'd like to change this so that it only appears on the homepage which is default.aspx. Please let me know what I can try. the below code lives in the code behind of the master page of the site. We use vb.net but if someone can write it in C# I can convert it.
Previous code that displays label text on every page:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Using ds As DataSet = SystemDataObject.SystemDataGetMessage()
lblSystemMessage.Text = ds.Tables(0).Rows(0)("SystemMessage_sd").ToString
End Using
End Sub
Code I have written that isn't working, what am I missing?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Using ds As DataSet = SystemDataObject.SystemDataGetMessage()
Dim appPath As String = Request.PhysicalApplicationPath
If appPath = "/Default.aspx" Then
lblSystemMessage.Text = ds.Tables(0).Rows(0)("SystemMessage_sd").ToString
End If
End Using
End Sub
Ended up wrapping the control in a panel with ID of Message and using the following code to display only if on the homepage.
If Page.Title = "Home" Then
Message.Visible = True
Else
Message.Visible = False
End If

Getting previous page visited

I'm trying to get thee previous page visited in ASP.NET using VB.NET using the following code:
Partial Class _Default Inherits Page
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim previousPage As String = Page.Request.UrlReferrer.ToString
If (Request.UrlReferrer <> Nothing) Then
If previousPage.Contains("Login") Then
Dim tUsername As String = Session("Username")
lblUsername.Text = "Welcome, " + tUsername
ElseIf previousPage.Contains("Register") Then
Dim cUsername As String = Session("CUsername")
lblUsername.Text = "Welcome, " + cUsername
Else
lblUsername.Text = "Welcome, Guest"
End If
End If
End Sub
End Class
I get this error:
Object reference not set to an instance of an object.
at:
Dim previousPage As String = Page.Request.UrlReferrer.ToString
What I want to do is get the previous page visited so I can get a session variable.
Try This code.
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If (Request.UrlReferrer <> Nothing) Then
Dim previousPage As String = Page.Request.UrlReferrer.ToString
If previousPage.Contains("Login") Then
Dim tUsername As String = Session("Username")
lblUsername.Text = "Welcome, " + tUsername
ElseIf previousPage.Contains("Register") Then
Dim cUsername As String = Session("CUsername")
lblUsername.Text = "Welcome, " + cUsername
End If
Else
lblUsername.Text = "Welcome, Guest"
End If
End Sub
End Class
Unsure what you are trying to do but while its easy enough to answer your specific question, you should take a step back and review why you are doing things that way.
It seems you are trying to control flow based on some authentication. If so, consider ASP.Net Forms Authentication +/- Login Controls. You can "plug" this architecture into your existing auth mechanism (meaning you don't have to uproot your existing stuff to implement it).
(If you still want to reinvent the wheel) Consider cookies instead of trying to figure out "where the user came from" prior to landing on "this" page - both of which can vary by x - the more web pages your web site has or will have, you'll have more spaghetti.

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

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:
next page
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

How can I grab the current url and assign it to a variable using vb and asp.net?

I have a form that the user fills out and submits. I have vb code that converts that form into an email and sends it to me. I need to know what page it is coming from so I want to assign the current url to a variable that will be included in the email.
Simply put: How do I assign the URL to a variable?
You can find this in the request object. e.g. Request.Url.AbsoluteUri
Dim url As String = Request.Url.AbsoluteUri
VB.NET
'static variable
Private Shared prevPage As String = String.Empty
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If (Not IsPostBack) Then
prevPage = Request.UrlReferrer.ToString()
End If
End Sub

ASP.NET - Cross Page Posting From Custom Control

Can I get some help posting across different pages from a custom control?
I've created a custom button that raises it's own click event through the following code:
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Const EventName As String = "button_click"
Const ArgName As String = "__EVENTARGUMENT"
If Page.IsPostBack _
AndAlso Request.Params IsNot Nothing _
AndAlso Request.Params(ArgName).Trim = EventName Then
Me.OnClick(Me.this_button, New EventArgs)
Else
Me.this_button.Attributes.Add("OnClick", Page.ClientScript.GetPostBackEventReference(Me.this_button, EventName))
End If
End Sub
How would I go about modifying this to let me post to a different page?
I'd like it to act as close as possible to the System.Web.UI.WebControls.Button property PostBackUrl.
You can use Cross Page Postbacks.
You can also use the WebForm_DoPostBackWithOptions js method to postback the current page to another page.

Resources