Cross Page PostBack In Asp.Net
In web development, many times we come across following situation
We need to redirect to new page, but we also need some data which belongs to previous page, we might need to display the data on new page. So what things we normally do are,
eighter keep the data in session, or in hidden variable, or put data in application variable, or again run the code to get same data and bind it to similar kind of control.
I think this is something which we can avoid by simply applying logic of Cross Page Postback, where we can directly identify the controls whose value we need to display on new page.
E.g. step1--> We have Grid View on say PAGE1.aspx and we redirect to PAGE2.aspx.
step2--> On PAGE2.aspx we need same Grid View to Display. For this we will probably use traditional ways to maintain the grid.
Now what if I say we can directly access grid which is already filled with data, and simply put the same grid on PAGE2.aspx. This sounds very simple. And it is very simple. For better understanding just check out following code which I have written on PAGE2.aspx
It looks very easy to use the code. You can DOWNLOAD the demo code, explained below
In above code check out the If condition where we have check whether it is a CrossPagePostBack or not.
We have following things in our web application.
- Page1.aspx (with cs code)
- Page2.aspx (with cs code)
Lets build Page1.aspx first, we have following HTML in Page1.aspx file.
check out the PostBackUrl of button. which is set to Page2.aspx, we just need to do this in our code so that Page1 will be accessible in Page2.aspx
which looks like following page
Now we will write code in Page1.aspx.cs file which is as below, you can generally copy paste the code in your files.
This is what we have to do for Page1.aspx, we are done with our code. In Page1.aspx we simple have two textboxes and one Grid View control which we bind to some sample table. In Page2.aspx we Just make Grid View Object and set the reference of Page1's Grid View to new gird view thats it. If you observe the code written for Page2.aspx, we don't retrieve the data from any where and bind it to Grid View, we simple set the reference. This makes code execution also easy, and in similar way we can always pick some random controls which we need to display on some other page. We can also have many links referring to different pages depending on the requirement.
0 comments:
Post a Comment