Working with PreviousPage method of the page
In this article we will discuss one of the way of accessing the controls and the variable of the previous page in the next page.We will see it with the help of PreviousPage property. We are going to see it with the help of an example.
For this in one of the page we will take some controls and user will enter some values
in the controls. Now when the user will click on a button we will redirect him
to the other page which will display the values entered by the user in the previous page.
In the designer page we have taken controls whose cod eis like this:
<table class="style1">
<tr>
<td class="style4">
<asp:Label ID="lblwritter" runat="server" Text="Select Writter"></asp:Label>
</td>
<td class="style3">
<asp:DropDownList ID="ddl_writer" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddl_writer_SelectedIndexChanged">
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="style4">
<asp:Label ID="lblbooks" runat="server" Text="Select Books"></asp:Label>
</td>
<td class="style3">
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
</asp:CheckBoxList>
</td>
</tr>
<tr>
<td class="style4">
<asp:Label ID="lbldisplay" runat="server" Text="Display Selected Books"></asp:Label>
</td>
<td class="style3">
<asp:RadioButton ID="RadioButton1" runat="server" GroupName="PageType" Text="Show on same Page" />
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="PageType" Text="Show on Another Page" />
</td>
<td>
</td>
</tr>
<tr>
<td class="style4">
</td>
<td class="style3">
<asp:Button ID="Button1" runat="server" Text="Show Selected Books" Width="172px"
OnClick="Button1_Click" />
</td>
<td>
</td>
</tr>
<tr>
<td class="style2" colspan="3">
<asp:Label ID="Label5" runat="server"></asp:Label>
</td>
</tr>
</table>
Now in code behind we will populate the drodownlist and checkboxlist.
public string bookname
{
get
{
string books = "";
foreach (ListItem li in CheckBoxList1.Items)
{
if (li.Selected)
{
books += li.Text + ", ";
}
}
return books;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
ddl_writer.Items.Add("--SELECT--");
ddl_writer.Items.Add("Yeshwant Kanetkar");
ddl_writer.Items.Add("Herbert Schildt");
ddl_writer.Items.Add("Adam Nathan");
}
}
protected void ddl_writer_SelectedIndexChanged(object sender, EventArgs e)
{
CheckBoxList1.Items.Clear();
if(ddl_writer.SelectedIndex.Equals(1))
{
CheckBoxList1.Items.Add("Let Us C");
CheckBoxList1.Items.Add("Let Us C++");
CheckBoxList1.Items.Add("UNIX Shell Programming");
}
else if (ddl_writer.SelectedIndex.Equals(2))
{
CheckBoxList1.Items.Add("Java 2 The Complete Reference");
CheckBoxList1.Items.Add("C# 3.0 The Complete Reference");
CheckBoxList1.Items.Add("Crystal Reports 10.0 The Complete Reference");
}
else if (ddl_writer.SelectedIndex.Equals(3))
{
CheckBoxList1.Items.Add("WPF Unleashed");
CheckBoxList1.Items.Add("WCF Programming");
}
}
On button click event we will display the selected books in the same page or the next page
according to the radiobutton selected by the user.
protected void Button1_Click(object sender, EventArgs e)
{
if (RadioButton1.Checked == true)
{
Label5.Text = this.bookname;
}
else if (RadioButton2.Checked == true)
{
Button1.PostBackUrl = "~/SelectedBooks.aspx";
}
}
If the user selects to see the books in the next page we will have below code to access the controls
in the next page and display the book list.
protected void Page_Load(object sender, EventArgs e)
{
if (this.PreviousPage == null)
{
Response.Write("No Previous Page Found
");
}
else
{
//code to read the property declared in previous page.
Response.Write("" + this.PreviousPage.bookname);
//code to read the controls declared in the previous page.
CheckBoxList chk = (CheckBoxList)this.PreviousPage.FindControl("CheckBoxList1");
foreach (ListItem li in chk.Items)
{
if (li.Selected)
{
Response.Write(li.Text + "
");
}
}
}
}