How to get the referring page on Page_Load

When a page loads, in order to get the name of the page that sent you there, all you need to use is:

Request.UrlReferrer.ToString

You can create a global variable to hold it:

Dim sReferrer as string

Then, in the Page_Load event, assign it:

If Not Page.IsPostback then sReferrer=Request.UrlReferrer.ToString End If

Or, you can put it in ViewState at that time:

If Not Page.IsPostback then ViewState("Referrer")=Request.UrlReferrer.ToString End If

From there, on out, within that page, you can use either the variable, or ViewState, within that page as a link, or whatever you need it for.

Where To Store Database Connection (ASP.Net 2.0)

In ASP.Net 1.x, we used the AppSettings section of the web.config file to store connectionstrings to use in our web pages.

However, now, in ASP.Net 2.0, there is a connectionStrings section in the web.config file:

<connectionStrings> <add name="MyDB" connectionString="server=YourServer;uid=YourUID;pwd=YourPWD;database=YourDB" providerName="System.Data.SqlClient" /> </connectionStrings>

To use it inside your page, you would do something like this:

strConn=ConfigurationManager.ConnectionStrings("YourConnString").ConnectionString MyConn = New SQLConnection(strConn)

or, inside a SQLDataSource Control, use the Connectionstrings property:

ConnectionString="<%$ ConnectionStrings:YourConnStringGoesHere %>">

Using Sessions in Code Behind

In your ASPX pages, you can easily use the Session object, by just using:

Session("Whatever")="Something"

However to use it in your separated code pages (codebehind, partial class, function library, etc), it’s necessary to use it this way:

System.Web.HttpContext.Current.Session("Whatever")="Something"

Accessing Controls on Master Pages from Content Pages

Let’s say you have a label called ‘Label4′ on your Master Page. You might want to change the text of that label, to something different, from each content page you have in your site.

To do that, it’s very easy:

Dim lbl As Label lbl = CType(Master.FindControl("Label4"), Label) lbl.Text = "whatever"

(of course, this is all assuming the label is OUTSIDE the ContentPlaceHolder)

Also, make sure you have this at the top of your Content page:

<%@ MasterType TypeName="TheNameOfYourMasterPage" %>

Nest Master Pages easily

Let’s say your scenario is that you want a Master page for your business’s website. However, inside that ‘shell’, you also want a certain design for each department of the business.

All you need to do, is create the second ‘Department’ Master page. Then, inside the Master Directive, include a reference back to the Business Master:

<%@ Master Language="VB" MasterPageFile="~/Business.master" .....

Also, be sure to use different ContentPlaceholder ids, and remember the one caveat – if you’re using Nested Masters inside VS.Net 2005, you’ll need to only use the html view, since the design view does not support nesting master pages.

If you are working with SharePoint check out   SharePoint Master Pages

Difference between Server.Transfer and Response.Redirect

Server.Transfer processes the page from one page directly to the next page without making a round-trip back to the client’s browser. This way is faster, with a little less overhead on the server. However, it does NOT update the clients url history list or current url. Response.Redirect, as expected, is used to redirect the user’s browser to another page or site. It DOES perform a trip back to the client where the client’s browser is actually redirected to the new page. The browser history list IS updated to reflect the new address.

Confirmation Messagebox when deleting an item from the datagrid

Many Users click the delete button within the datagrid with no attention then the result the item will be deleted from the datagrid,

so we can add confirmation messagebox to appear to tell the user (are you sure you want to delete this item?

first of all we have to attach the javascript code for each delete button within the datagrid,we can do that in the itemdatabound method of the datagrid:

Dim deleteButton As LinkButton = e.Item.Cells(0).Controls(0)
deleteButton.Attributes("onclick") = "javascript:return " & _
                               "confirm('Are you sure you want to delete this item?')"

where the first cell in the datagrid is a linkbutton.

so when the user clicks the delete button then a confiramtion message appears so if the user clicks No then nothing happens else postback will happen then it should be a server event such as a ondeletecommand that fires when the user clicks the delete button as

Sub dgPendingCompleteForms_Delete(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Handles dgPendingCompleteForms.DeleteCommand

'put your code here when the user presses the yes button on the confirmation meessagebox
End Sub

For ASP.net v2.0 – also look at adding an OnClientClick attribute, and assign the Javascript confirmation here (http://aspnet101.com/aspnet101/aspnet/codesample.aspx?code=gvConfirmDelete)

I hope that my tip is helpful
Best regards