CodeCharge Studio All ASP PHP Perl ColdFusion Java C#.NET VB.NET

Add URL Parameters to a Page

This example shows how to reload a page if the URL lacks some parameters. In this case, the URL for the page must contain a status=1 URL parameter otherwise the page will be reloaded and the parameter added to the URL.

  1. Add the After Initialize event to the page.
  2. Within the event, add the code below:

ASP

Function Page_AfterInitialize(Sender)

  If IsEmpty(CCGetParam("status", Empty)) Then
    Response.Redirect FileName & "?" & CCAddParam(Request.ServerVariables("QUERY_STRING"), "status", 1)
  end if 

End Function

PHP

function Page_AfterInitialize(& $sender) {
global $FileName;

  if (CCGetFromGet("status","") == "") {
     header("Location: " . $FileName."?". CCAddParam(CCGetQueryString("QueryString", ""), "status", 1) );
  }

}

Perl

sub Page_AfterInitialize() {

  if (CCGetFromGet("status","") eq "") {
    print $page_header . "Status: 302 Found\nLocation: ".
    $FileName."?". CCAddParam(CCGetQueryString("status", ""), "status", 1) ."\n\n";
    exit 0;
  }

}

ColdFusion

<!---Page_AfterInitialize --->
  <CFPARAM Name="URL.status" Default="">
  <CFIF URL.status EQ "">
    <CFMODULE Template="CCGetQueryString.cfm" strCollection="QueryString" arrRemoveParameters="#ListToArray('CFID;CFTOKEN',';')#">
    <CFMODULE Template="CCAddParam.cfm" strQueryString="#CCGetQueryString#" strName="status" strValue="1">
    <CFLOCATION URL="#strFileName#?#CCAddParam#"> 
  </CFIF>

VB.Net

'Page_AfterInitialize

  If IsNothing(Request.QueryString("status")) Or Request.QueryString("status") = "" Then  
    Dim params As New LinkParameterCollection()
    params.Add("status","1")
    Response.Redirect(Request.Url.AbsolutePath + params.ToString("GET","status"))
  End If

C#

//Page_AfterInitialize

  if (Request.QueryString["status"] == null || Request.QueryString["status"].Length == 0) {
    LinkParameterCollection HrefParams = new LinkParameterCollection();
    HrefParams.Add("status","1");
    Response.Redirect(Request.Url.AbsolutePath + HrefParams.ToString("GET","status"));
  }

Java

//Page_AfterInitialize

  if ( StringUtils.isEmpty(e.getPage().getHttpGetParams().getParameter("status")) ) {
    String servletPath = e.getPage().getRequest().getRequestURI();
    String queryString = e.getPage().getRequest().getQueryString();
    queryString = StringUtils.isEmpty(queryString) ? "status=1" : queryString + "&" + "status=1";
    e.getPage().setRedirectString(servletPath + "?" + queryString);
  } 

See Also:

After Initialize event


On-line, printable versions and updates