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

Give a brief introduction on side-by-side execution. Can two applications, one using private assembly and the other using the shared assembly be stated as side-by-side executables?

522


Explain the difference between the while and for loop. Provide a .net syntax for both loops?

505


What is shared and repeatable inheritance?

553


What is the advantage of .net?

540


What is .net3.5?

601






With these events, why wouldn't microsoft combine invalidate and paint, so that you wouldn't have to tell it to repaint, and then to force it to repaint?

514


Explain about .Net products?

611


What is reflection and what is it for?

573


Please explain what do the terms “boxing” and “unboxing” mean?

588


What's a windows process in .net?

540


Please explain when should you use .net web forms over asp.net mvc?

548


Should I implement finalize on my class? Should I implement idisposable?

549


Explain dataset.acceptchanges and dataadapter.update methods in .net?

559


How does u handle this COM components developed in other programming languages in .NET?

1673


How to implement getcommon method in class a? Are you seeing any problem in the implementation?

626