How do you pass value of a text box from page1.aspx to
page2.aspx without storing it as a session value?

Answer Posted / rishiraj r

We can also use cross post back property ofASP.NET 2.0

eg:

1. Specify the ListBox as

<asp:ListBox ID="ListBox1" runat="server" >
<asp:ListItem Value="1" Text="1st Option" />
<asp:ListItem Value="2" Text="2nd Option" />
<asp:ListItem Value="3" Text="3rd Option" />
</asp:ListBox>

2. Add this code in the default.aspx(.cs)

public ListBox TheListBox
{
get
{
return ListBox1;
}
}
protected void Page_Load(object sender, EventArgs e)
{
//Generate the cross-page postback script
PostBackOptions options = new PostBackOptions
(ListBox1);
//This will trigger correct script generation
options.ActionUrl = "secondPage.aspx";

//Add it to onchange attribute if the ListBox
string s =
Page.ClientScript.GetPostBackEventReference(options);
ListBox1.Attributes["onchange"]=s;

}

3. Then on secondPage.aspx

3.1 In aspx

<%@ PreviousPageType VirtualPath="~/Default.aspx" %>

3.2 in Page_Load of the secondPage.aspx(.cs)

protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null &&
PreviousPage.IsCrossPagePostBack)
{
Response.Write("You selected " +
PreviousPage.TheListBox.SelectedValue );
}
}

Is This Answer Correct ?    3 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What’s different between process and application in .net?

579


How is .net able to support multiple languages?

601


What makes .net core cross platform?

512


What is the difference between vb.net and vc#.net?

534


Explain what is immutability, what is it for and how is it codified?

515






What is the advantage of packaging over xcopy in .net?

584


What do you mean by web service?

643


How to create multiple inheritance in c#, with example?

543


What is file extension of Webservices in .Net?

568


When should I use .net core?

546


can any one tel me the complete Testing Procedure of any one simple PROJECT i mean either web/windows based application?

1461


What are the collections you've used?

569


What is an anonymous method and how is it different from a lambda expression?

505


Do you know what is garbage collector?

563


what is the keyword used for self reference?

1922