Sunday, November 21st, 2010

Dynamic Controls in Cross Page Postback

Requirements:

This tutorial requires basic knowledge in Asp.Net, Csharp and Visual studio.

What is Cross Page Postback?

In Asp.Net world a Cross Page Postback refers to passing form data from one web page to another. In Asp.Net this is handled differently when compared to classic Asp or any other server side languages. In this tutorial let us see how a static Asp.Net control and dynamic Asp.Net controls are handled in a Cross Page Postback.

Asp.Net controls are nothing but the form elements and dynamic Asp.Net controls here, I mean the controls that are created on the fly during runtime.

Step 1:

First we create a source page front end with static form control. We will add a text box control and button to the form. By default the PostBackUrl property of the button is assigned to the same page. Since we are dealing with cross page postback, we assign it to Target.aspx page.

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Source.aspx.cs" Inherits="WebApplication1.Source" %>


 
 


Step 2:

Now we need to get the form data in the Target.aspx page. Let us first design the front end for the Target.aspx page.

<%@ Page Title="About Us" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Target.aspx.cs" Inherits="WebApplication1.Target"  %>
<%@ PreviousPageType VirtualPath="~/Source.aspx" %>


    

Target Page


In the above snippet you can see a PreviousPageType directive with virtual path set to Source.aspx . This is how the target page knows, from where the cross page postback comes from.

Step 3:

In this step we write some c# code inside the page load event for the target page to retrieve the value from the source page. Let us see how to do it.

  protected void Page_Load(object sender, EventArgs e)
  {
     if (this.PreviousPage.IsCrossPagePostBack)
     {
      HtmlGenericControl br = new HtmlGenericControl("br");
      ContentPlaceHolder con = (ContentPlaceHolder)this.PreviousPage.Master.FindControl("MainContent");
      TextBox txt1 = (TextBox)con.FindControl("TextBox1");
      Label1.Text = txt1.Text;
      /*Dynamic controls code to follow*/
      }
   }

In the above snippet we first check the if PreviousPage has sent a Cross Page Postback, if yes then we get the placeholder control called “MainContent” in the master page. We find the textbox control in the previous page using its ID, “TextBox1″. The value of the TextBox1 can be retrieved using the Text property.

Step 4:

Now let us see how to retrieve the values for the dynamic controls in the target page. So lets add some c# code to the page load event of the Source page to create the dynamic controls on the fly.


 protected void Page_Load(object sender, EventArgs e)
 {

     ListItem lst1 = new ListItem("Item1", "CheckBox1 is Checked");

     ListItem lst2 = new ListItem("Item2", "CheckBox2 is Checked");

     CheckBoxList1.Items.Add(lst1);
     CheckBoxList1.Items.Add(lst2);

     ListItem lst3 = new ListItem("Item1", "RadioButton1 is Checked");

     ListItem lst4 = new ListItem("Item2", "RadioButton2 is Checked");

     RadioButtonList1.Items.Add(lst3);
     RadioButtonList1.Items.Add(lst4);

 }

In the snippet above a checkbox list and radio button list is created during the runtime, when the source page is loaded.

Step 5:

The target page can retrieve the values from the dynamic control as follows,

protected void Page_Load(object sender, EventArgs e)
{

   if (this.PreviousPage.IsCrossPagePostBack)
   {
      HtmlGenericControl br = new HtmlGenericControl("br");
      ContentPlaceHolder con = (ContentPlaceHolder)this.PreviousPage.Master.FindControl("MainContent");
      TextBox txt1 = (TextBox)con.FindControl("TextBox1");
      Label1.Text = txt1.Text;
      CheckBoxList ck1 = (CheckBoxList)con.FindControl("CheckBoxList1");
      foreach(ListItem item in ck1.Items)
      {
          if (item.Selected)
          {
           Label lbl = new Label();
           lbl.Text = item.Value;
           LabelHolder.Controls.Add(br);
           LabelHolder.Controls.Add(lbl);
          }
     }
     RadioButtonList rbtn1 = (RadioButtonList)con.FindControl("RadioButtonList1");
     foreach (ListItem item in rbtn1.Items)
     {
       if (item.Selected)
       {
         Label lbl = new Label();
         lbl.Text = item.Value;
         LabelHolder.Controls.Add(br);
         LabelHolder.Controls.Add(lbl);
         break;
       }        
    }
 }

In the above snippet we check for the selected items from the ckecboxbox list control and radiobutton list control. The value is retrieved from the list item and assigned to the labels that are created during the runtime.

Category: Articles
You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allowed.