Add Browser Redirection to ASP.NET File

Perhaps a page was created with a spelling error, or a reorganisation of the website means that a particular page has been renamed.

We wish to redirect our visitors to the page to a different address.

In the example below its assumed that the website page has moved to www.example.com/fruit-and-veg/.

We wish to tell the browser to redirect to the new page name by adding a short snippet of code to the page.

<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.example.com/fruit-and-veg/");
}
</script>

The code example above can be added to the page. This is for a dynamic asp.net page. It won’t work for a static HTML page.

In the above I’ve included the notification that the change is permanent.

Browsers will cache the redirect, after all you’ve told the browser that you no longer wish the old page to be visited. You may find forcing browsers to adopt a change back difficult to achieve, with the browser reluctant to discover the change. A further permanent redirect back again may be used.

I’ve illustrated the example using a permanent redirect. But, as noted, it’s possible that the browsers will equally permanently cache your rule. If it’s wrong then amending it may well be ignored. Experiment first with a temporary redirect, just to be safe.

If its your intention to redirect more than the single page, perhaps a whole menu section, by adding the redirect into a common header file then you may wish to reconsider your approach. Making use of the file web.config.

LinkButton Handles OnClick Equivelent in C#

To handle a Linkbutton onclick in VB.NET the LinkButton is declared and a handler sub declared:

<asp:linkbutton id="cmdAdd" runat="server" ResourceKey="Add" BorderStyle="none" CausesValidation="true" />

Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
:::
:::
End Sub

C# doesn’t support the VB.net handles on click, the function is simply declared.

The LinkButton declaration is given below. Unlike vb.net there is no WithEvents part of the declaration.

protected global::System.Web.UI.WebControls.LinButton cmdAdd

Explanations of the above:

  • protected – Used to restrict access to the code to the same class.
  • global – Used to clarify that the object is taken from the global namespace as opposed to the current one.
  • :: – This operator, positioned between two indentifiers, is used to look up an identifier.
  • cmdAdd – name of the LinkButton

Looking in the .ascx presentation file the link button is declared as:

<asp:linkbutton id=”cmdAdd” runat=”server” ResourceKey=”cmdAdd” CausesValidation=”true” />

Begin the OnLoad, and include reference for the LinkButton

protected override void OnLoad(EventArgs e) {
base.OnLoad(e)
try {
cmdAdd.click += new System.EventHandler(cmdAdd_Click);

Then later in the code there is the cmdAdd_Click function:

private void cmdAdd_Click(System.Object sender, System.EventArgs e) {try {
// actions here for the function

References

MSDN: global

MSDN: The :: Operator

MSDN: How to: Use the Global Namespace Alias

C# Equivalent of InStr

In VB there is a test using InStr as to whether a string contains another string. C# uses a different function for the equivalent test.

An example of the VB InStr function is given below

If InStr(DishName,"apple") > 0 Then
  Return True
End If
  Return False
}

In the above example the name of the dish is checked to see if it contains the word apple.

c# uses the IndexOf function to perform a comparable operation.

The equivalent of the test from the example above is

if (DishName.indexOf("apple")  != -1) {
  return true;
}
  return false;
}
Posted in C# Tagged

Multiview Set Active View

The MultiView control is used together with a number of View controls to change the content presented to the website visitor.

Setting the active view shows only that block of code as visible on the web page. An example of its use is a page showing a table of items. Selecting an individual table row changes the page to show the details pertaining to the selected entry.

Panels can be used to achieve this, but it soon becomes cumbersome. Each panel requires its visibility to be set accordingly. The more steps built into the page the more panels which need to be handled, for example:

pnlList.Visible = False
pnlReview.Visible = True
pnlSummary.Visible = False

MultiViews simplify the disclosing of parts of the presentation according to the current step in the sequence. In place of setting the visibility for each of the panels a single call to set the required view hides all of the others and shows the requested one.

.ascx File

Below is an example MultiView  with three views, as described in the panels above.

The code is a part of the HTML from the .ascx file

<asp:MultiView ID="mltApplications" runat="server">
<asp:View ID="vewList" runat="server" >
:::
</asp:View>
<asp:View ID="vewReview" runat="server" >
:::
</asp:View>
<asp:View ID="vewSummary" runat="server" >
:::
</asp:View>
</asp:MultiView>

Code File

The following code examples illustrate selecting one of the views above, VewReview. The first line shows the declaration required for the MultiView and then secondly the call to set it. The corresponding definitions for VB.NET and C#, in the code file are:

VB.NET

Protected WithEvents mltApplications As System.Web.UI.WebControls.MultiView
:::
:::
mltApplications.SetActiveView("FindControl("vewReview"))

C#

protected global::System.Web.UI.WebControles.MultiView mltApplications;
:::
:::
View vewReview = findControl("vewReview") as View;
mltApplications.SetActiveView(vewReview);

Converting Text to Proper Case

For a title or persons name VB.NET and C# can convert the text to initial capital letters.

For example, to convert the lower case name john smith to John Smith.

VB.NET

To implement the conversion of the name to initial word capital letters, ie Proper Case, is a simple function in VB.NET

StrConv(UserName, VbStrConv.ProperCase)

C#

C#  borrows from the Visual basic to implement the change, not having a conversion of its own.

Microsoft.VisualBasic.Strings.StrConv(UserName, Microsoft.VisualBasic.VbStrConv.Lowercase, 0);StrConv(UserName, vbProperCase)

There is an option in CSS to capitalise the first letter of a paragraph, or to change the case, either upper or lower, for all words in a paragraph, or other tag.

The use of JavaScript or JQuery would also be an option.