Passing Value from Page to Another Page ASP.net Using VB - asp.net

I simply need to copy the textbox input data to another page. i have a datagridview from Home.aspx when i select data from it. it will display the row data to the textbox. now i want to copy/pass the data to another page. how can i do that? i tried following codes but it does not work.
This is the 2ndPage Code on load_page
If Not Page.PreviousPage Is Nothing Then
Dim SourceTextBox As TextBox
SourceTextBox = CType(PreviousPage.FindControl("TextBox1"), _
TextBox)
If Not SourceTextBox Is Nothing Then
Label1.Text = SourceTextBox.Text
End If
End If

Ok, a truckload of issues here.
First up:
page.previouspage is ONLY available if you used
Server.Transfer("~/MyCoolWebPage.aspx")
And as general rule you don't a "server side" transfer/jump/hop to a page this way. Trying to keep this post short, but a server.Transfer() means that no real browser side jump to the page occurs - only a server side transfer. That means that the previous URL does not even change! And thus as a general rule, a jump to a page occures from a button click on a page, and that button click can be a client side (JavaScript) click, or a standard button click that executes the Response.Redirect(). However, in both cases, it really is a client side browser request. You as a general rule thus don't want to build or write a web page that "assumes" that a Server.Transfer() was how you landed on that page.
Because of the above? Well me.Page.PreviousPage is ONLY available if the page you are landing on was the RESULT of a 100% server side re-direction to a new page. And that simply not going to be the case, and thus for the vast majority of your web pages, then page.Previouspage will be null, and of ZERO use to you. (it's simply not available).
So, now we back to traditional means and approaches to "transfer" data to the next page.
Remember, that page is state-less. That means EVEN on the same page, all values, and variables are lost between each page load. Even a simple button click means the page will re-load, but that re-load will see all your values lost. Now most controls such as a text box, or even the gridview can and will keep what is called their state for those "round trips" when you click say on a button.
Now, in your case, no doubt you are clicking on a button, and jumping/loading to a new web page. Thus, all previous controls, data and everything you had in existance on that previous page is all gone, and lost.
So, how to pass values give this "state-less' nature of web pages?
Well, a very common way is to use parameters in the URL. However, I find them quite ugly, and users can also mess with those values. However, for really large web sites, we see the parmaters in the URL often used, and that's because the web site does not want to spent the energy, the time and even the memory and resources to keep track of say 1 million users hitting that web site.
But, there is another very common means to "save" values in what we call a round trip. That handy dandy "temp" storage system is called Session(). Now Session() should be treated as a precious and limited resource, but it is quite much how you can (and should) pass values around. The beauty of these values is they "persist" and keep their values between post-backs. And this includes jumping to another page.
So, to pass that data to the next page?
Simply stuff that data row into the session, and then in the target page, you can then get/grab/use/pull any values from that one row of data.
So right before your response.Redirect("to my 2nd page page"), you can do this:
Session("MyTextBox") = me.TextBox1.Text
Now above is fine for one text box value. But, you want to pass the whole row.
So, you can go:
Session("MyListDataRow") = MyRowOfData
Now, you don't explain how or when or where you selected the one row of data. But you can certainly shove the one row of data into session, and then show/display it on the next page.
One more SUPER DUPER WHOPPER:
If you do pull out the selected row into a datarow (as opposed to datatable, or dataset), then keep in mind that .net serializers don't support the datarow (why, I have no idea!!!). So if you using "in-memory" sessions(), then you are ok. But if you ever decide to adopt SQL based session() management, then that code will start to fail. So, either shove in a ListViewDataItem, or a datatable to pass that row of data, and not a datarow.
I would thus just not pass a "datarow" in Session() and avoid the above horrifying revelation that .net left out that data rows don't support serialization! (holy moly!!!!).
So unless you are using a server.transfer() for this, then page.previous is not valid nor available. And if you using a master page, then you have to first dig and get the master page refernce, and then the ContentPlaceHolder if you ARE using a server transfer and you thus do have use of page.PagePrevious.
Edit:
Having said all of the above? Well, if you have a click event on a listview (or datagrid) row, then a server.transfer actually is "one" of those good use cases in which you may VERY well consider a server.transfer(), since the you have full use of the listview/datagrid data in that 2nd page though use of page.PreviousPage. Just be aware that you have to pull down one more level to get at the content if you using master pages.

Related

How can I achieve two different sessions for the same page?

Is it possible to have unique sessions for the same page in different tabs? Lets say I have in one tab this url https://www.test.com/test.aspx?id=123 and another tab with https://www.test.com/test.aspx?id=124. Each one has data loaded for their respective id. If I do something in the first tab that saves a value in a Session and then do the same with the second tab, if I switch to the first tab and make a PostBack (like calling an event that saves the information), the Session value from the second tab will be saved on the first. How can I avoid that?
This is a big pain - and one that I wish a built in option/setting was available.
However, what often works VERY well?
You can use session to pass value(s) to a page, but ONCE you get on that page and the first page load (Is PostBack = False), then transfer/save the few passed values into ViewState. ViewState is quite much as easy and flexible as Session(), and it is a persisting set of values that is per page, and per tab. And it qiute much is the same if you used a bunch of hidden field controls for this anyway.
So, don't use the session() for persisting of values on a page, but use session() to pass values.
So now, you can have multiple pages open - and you only grabbed/used the session() to get values on first page load - after that, you code using ViewState. Now you want to be careful, since viewstate is browser side based - and you don't want to overload it. (put much data in Viewstate - since whatever you have in ViewState WILL MAKE round trips and lives in the browser - every post back will send this data from client side to server.
ViewState is also how asp.net manages all your controls. So if you have a bunch of text boxes - and do a post-back? you note that these controls values persist automatic for you. This works for you by asp.net using Viewstate. And Even if you have two of the same pages loaded - you note that text boxes etc. still persist for each of the pages.
Session() = for global types of values for that user - applies to all pages.
ViewState() = for per page values.
So, if you use session() to pass values to that page - say a new tab, then on first page load - transfer the few values to ViewState. Those values now are local to the one page, and are not effected by other tabs/pages open - even when pages are on the same page.

Asp.net controls losses the value

I am using two tabs in my page like the attached image.
.
Here, after selecting the value in all dropdownlist controls then, i click the generate button. so, the tab is moved from parameter to report. Then again i click the parameter tab. At this time all my dropdown controls shows null value.
but I want all the dropdown values which i selected before.
how can i get the dropdown control values?
It's important to remember that web pages like this are stateless. One request won't remember anything about another request.
So in your situation, you're putting in data, then it sounds like you're clicking a link (or button) that redirects you to another page, then you try and go back to the first one, and it has to fire off yet another request, with no knowledge of the first one's state.
Given that, it seems like you have two primary options (and dozens of others that I won't bother mentioning):
Move the tabs to be on the same page. (My recommendation)
There are lots of controls out there, or you could easily write one yourself, that don't separate tab pages via multiple requests. If you did that, switching tabs wouldn't lose the data.
Add a "save" button, and disable the other tab.
This way, the user actively has to save the data before being able to leave the page via another tab. The biggest issue here comes in the form of usability.
Which one of those you choose is dependent on a number of factors, not the least of which being code maintainability and the size of requests.

Cross-page posting with target _blank leaves source page unfunctional

I'm trying to describe it in as few steps as possible:
I have Page1.aspx with lot of controls, and Preview and Save button among those. I also have Page2.aspx that is the redirection target of a Preview Button click.
Since I need all the controls selections from Page1 to draw a preview on Page2 the redirection is done with setting Preview's PostBackUrl.
I also must have preview shown on a new tab or window so I used onClientClick="aspnetForm.target='_blank'" for Preview button definition.
Save button-click callback, after storing data to a database does redirection to some Page0.aspx (initial list of reports - the subject of the code)
Preview button works fine - a preview renders in a new tab, but when I go to the old tab and click on Save, I see from debugger, that firstly Page2.aspx(?) and secondly Page1.aspx are loaded. Then all the data is stored in the db, but though Page0 redirection is executed Page1.aspx stays loaded in the browser.
I have no idea what processes are behind this. Could one who knows give me an insight? Or if you consider my approach impossible to implement give some idea how to do the same?
If it's of importance, everything on the Page1 is located in an update panel.
Thank you very much for replying
In ASP.NET there are basically zero (0) circumstances in which you will ever send form data from one page to another. Although what exactly you are trying to accomplish is vague, you can consider some of the following:
Isolate unique operations/systems to a single page. If you have something like a User Profile, don't have three different aspx pages; just use a single page for the user or admin to manage that data / functions. Postback events are your friend.
Understand the difference between ViewState and traditional form data. I'm guessing that if you're trying to post form data from one page to another, you probably don't understand the point of ViewState. Using a single page to maintain temporary data that the user is currently working with is a great use for ViewState. If you want the data to appear on another page then you need to consider the data from the previous page as final and thus should be saved to a database or some other medium.
These are just some general guidelines because there is no exact answer to your problem without saying something generic like "You're doing it wrong." I would recommend starting by never again trying to post form data from one aspx page to another.

Maintaing changes on the previous page

I have a page in asp.net by clicking on the grid in the page it takes me to the detail page,on this page i have a back button (not browser back) by clicking on the button it takes me again to the search page.
i want to see the page with changes which i have made before coming to the detail page,also i want to disable back button of browser.
i dont want to use session variable or viewstate variable.
is there any property for this scenario.
yours sincerely
Talha khan
You can't disable the browser back button. End of story. Sorry.
What changes are you making to the grid, where is it getting it's data from? You could use a cookie to store the previous pages' state if you don't want to use session or viewstate.
If the the amount of data you need to track is relatively small (in your case this would probably be a search query) you can use query string to pass the state to the detail page (which can be later passed back to the search page).
You can also use browser cookies to persist the state but I wouldn't do that (at least because they're not designed for this kind of interaction).
-- Pavel
There are several ways to solve this.
The first that comes to mind is to change the page to AJAX. AJAX would not force a page jump forward, hence there will be nowhere to go back to.
The second way is to have a check on the previouis page, for example set "Session("if_search_go_here") = Request.Url.PathAndQuery" and unset it at the right place. If this is set then the search page will just forward you to the detils page again.
probably more. :)

Is there any way to modify query strings without breaking an ASP.Net postback?

From reading here and around the net, I'm close to assuming the answer is "no", but...
Let's say I have an ASP.Net page that sometimes has a query string parameter. If the page has the query string parameter, I want to strip it off before, during, or after postback. The page already has a lot of client-side script (pure JavaScript and jQuery).
As an example, say I load:
http://myPage.aspx?QS=ABC
The QS parameter is necessary to control what appears on the page when it first loads and is set by the page that "calls" it. myPage.aspx has form elements that need to be filled in and has a submit button that does a postback. When the page completes the postback, I need to returned URL to be:
http://myPage.aspx
in order to avoid the client-side code that is called when the query string is present. In other words, after a submit I don't want the client side actions associated with the query string parameter to fire. I know I could append the form contents to the URL as query string parameters themselves and just redirect to the new URL and avoid the submit/postback, but that will require a lot more type checking on the codebehind to avoid bad data and casual spoofing. I supposed I could also set a hidden field in the codebehind and look at it along with the query string to cancel the client-side behavior if I am coming back from the postback, but that still leaves the query string intact basically forever and I want to get rid of it after the initial page load.
Any ideas or best practices?
PS - Is there anything I can do with the Form.Action property that won't break the postback behavior?
I would use an HTTPModule to intercept and rewrite the URL and query as it came in. I'm not 100%, but I don't believe this has any impact with viewstate.
It sounds (and looks) complex, but it's actually fairly trivial and open to a ton of refinement and extension (as per .NET in general).
iam not sure this is what you are looking for, but if understand correctly you could do this:
-on page load check for the QS value, if its not there use a hidden input field.
-first time page loads with QS, do your normal processing and store QS value in a hidden input field.
-if no QS then use the hidden input value
-after postback, you could redirect to the same page, at that point you could user Request.Form[] to retrieve the hidden input field, still load the data properly but get rid of the QS.
it made sense in my head, i am not sure it make sense now, but i'll let you decide.
It is probably bad practice, but in those cases (and ones I just need to 'reset' the page), I simply do a Response.Redirect to the same page.

Resources