Modify and Redirect URL Values Programmatically

"Pass non-databound values and other calculated values via URL parameters. You can do this by overriding the button click handler, ModifyRedirectUrl, or Redirect methods."
- Anh Trinh, Software Engineer, Iron Speed, Inc.

November 15, 2006
Iron Speed Designer V4.0

Modify and Redirect URL Values Programmatically
Primary keys, foreign keys and other field values are easily passed from page to page via URL parameters established with settings in the Bindings tab of the Properties dialog. Sometimes, however, you may need to pass non-databound values and other calculated values via URL parameters. This can be done by overriding the button click handler method or by overriding the ModifyRedirectUrl or Redirect methods.
Modifying the whole URL string
The example below was created using the Northwind database. After creating a new Order record on the AddOrdersPage.aspx page, the user is redirected to the ShowOrderPage.aspx page instead of the normal (default) ShowOrderTablePage.aspx page. To do this, we override the SaveButton_Click() button click handler method in the AddOrdersPage class, located in:

...\<App Folder>\Orders\AddOrdersPage.aspx.cs or .vb

C#:

public void SaveButton_Click(object sender, EventArgs args)
{
      String url = "../Orders/ShowOrdersPage.aspx?Orders={PK}";
      bool shouldRedirect = true;
      try
      {
            DbUtils.StartTransaction();
            this.OrdersRecordControl.SaveData();
            url = this.ModifyRedirectUrl(url, "");
            this.CommitTransaction(sender);
      }
      catch (Exception ex)
      {
            shouldRedirect = false;
            BaseClasses.Utils.MiscUtils.RegisterJScriptAlert(this, "BUTTON_CLICK_MESSAGE",
            ex.Message);
            this.RollBackTransaction(sender);
      }
      finally
      {
            DbUtils.EndTransaction();
      }
      if (shouldRedirect)
      {
            this.ShouldSaveControlsToSession = true;
            this.Page.Response.Redirect(url);
      }
}

Visual Basic .NET:

Public Sub SaveButton_Click(ByVal sender As Object, ByVal args As EventArgs)
    Dim url As String = "../Orders/ShowOrdersPage.aspx?Orders={PK}"
    Dim shouldRedirect As Boolean = true
    Try
        DbUtils.StartTransaction
        Me.OrdersRecordControl.SaveData
        url = Me.ModifyRedirectUrl(url, "")
        Me.CommitTransaction(sender)
    Catch ex As Exception
        shouldRedirect = false
        BaseClasses.Utils.MiscUtils.RegisterJScriptAlert(Me, "BUTTON_CLICK_MESSAGE",
        ex.Message)
        Me.RollBackTransaction(sender)
    Finally
        DbUtils.EndTransaction
    End Try
    If shouldRedirect Then
        Me.ShouldSaveControlsToSession = true
        Me.Page.Response.Redirect(url)
    End If
End Sub

Modifying URL parameters using the ModifyRedirectUrl() method
In this example, the primary key of a new record is passed from the AddOrdersPage.aspx page to the MyPage.aspx page by specifying a URL parameter in the Bindings tab of the Properties dialog for the “Save” button on the AddOrdersPage.aspx page. A URL parameter is passed which is a concatenation of two fields, ShipCity and ShipCountry. Modifying URL parameters is accomplished by overriding the ModifyRedirectUrl() method.

Add this code customization in the AddOrdersPage class, located in:

...\<App Folder>\Orders\AddOrdersPage.aspx.cs or .vb

C#:

public override string ModifyRedirectUrl(string redirectUrl, string redirectArgument)
{
    string myStr = this.ShipCountry.Text + this.ShipCity.Text;
    redirectUrl = redirectUrl + "?myparam=" + myStr;
    return base.ModifyRedirectUrl(redirectUrl, redirectArgument);
}

Visual Basic .NET:

Public Overrides Function ModifyRedirectUrl(ByVal redirectUrl As String, ByVal redirectArgument As String) As String
    Dim myStr As String = (Me.ShipCountry.Text + Me.ShipCity.Text)
    redirectUrl = (redirectUrl + ("?myparam=" + myStr))
    return MyBase.ModifyRedirectUrl(redirectUrl, redirectArgument)
End Function

Pass Calculated Values in URL’s
In some cases it may be desirable to pass calculated values from one page to another via a page’s URL. For example, you might wish to pass the grand total of a table column on one page as an input value to another page:

http://MyApp/Page.aspx?Total= FreightGrandTotal.Text()

Or you might wish to pass the value of a dynamically retrieved session variable to a page:

http://MyApp/Page.aspx?Value=Session("Test")

You can do this with a code customization that overrides the ModifyRedirectUrl() method in the Page class.

In the example below, the grand total of the UnitPrice column in the Products table is passed as a URL parameter when the “New” button is clicked on the ShowProductsTablePage.aspx page. The grand total is passed via URL to the AddProductsPage.aspx page.

Step 1: In the Design tab in Iron Speed Designer, click the Configure… button on the Products table panel. This displays the Table Panel Wizard.

Step 2: In the Table Panel Wizard, select the Totals tab and select UnitPrice for the grand total column. Your generated application will automatically calculate the grand total.

Step 3: Override the ModifyRedirectUrl method in the ShowProductsTablePage class of the ShowProductsTablePage.aspx page.

.NET Framework 1.1 and 2.0:

...\<App Folder>\Products\ShowProductsTablePage.aspx.cs or .vb

C#:

public override string ModifyRedirectUrl(string redirectUrl, string redirectArgument)
{
    String modUrl = redirectUrl;
    If (redirectUrl.Equals("../Products/AddProductsPage.aspx"))
        modUrl += "?Total=" + UnitPriceGrandTotal.Text;
    return base.ModifyRedirectUrl(modUrl, redirectArgument);
}

Visual Basic .NET:

Public Overrides Function ModifyRedirectUrl(ByVal redirectUrl As String, ByVal redirectArgument As String) As String
    If (redirectUrl.Equals("../Products/AddProductsPage.aspx")) Then
        redirectUrl = redirectUrl + "?Total=" + UnitPriceGrandTotal.Text
    End If
    Return MyBase.ModifyRedirectUrl(redirectUrl, redirectArgument)
End Function

About the Author
Anh Trinh
Software Engineer, Iron Speed, Inc.

Anh is a software engineer at Iron Speed, Inc. He enjoys developing applications with Iron Speed Designer and Microsoft .NET technology.



  Privacy Statement