Cross Page PostBack Using ListBox
Solution:-
As there is no button on the page, ListBox AutoPostback should be true.
But ListBox PostBacks to same Page. Here requirement is that it should post to other page. So we need to change the property of ListBox.
We can not access Controls of Previous Page from Next Page. So we will define a Public property on First Page & access that on Second Page.
Code Snippet:-
Code on FirstPage.aspx.cs:-
public partial class ListBoxCrossPagePostBack : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
PostBackOptions options = new PostBackOptions(ListBox1);
options.ActionUrl = "SecondPage.aspx";
string s = Page.ClientScript.GetPostBackEventReference(options);
ListBox1.Attributes["onchange"] = s;
}
public ListBox TheListBox
{ get
{
return ListBox1;
}
}
}
Code On SecondPage.aspx.cs
if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
{
Response.Write("ListBox Selected Value:" + PreviousPage.TheListBox.SelectedValue);
}
We also need to register First Page as a Previous Page On Second Page.Add this line in SecondPage.aspx.cs
<%@ PreviousPageType VirtualPath="~/ListBoxCrossPagePostBack.aspx" %>