Automatic page refresh even we navigate to another page - asp.net

i have a problem in page navigating. please help me to solve out.
in one asp.net page forexample Page1.aspx i have dropdown when we select an item in dropdown that would display in a label. Then we navigate to another page from page1.aspx to page2.aspx by clicking linkbutton in page1.aspx. Again if i come to page1.aspx that previously selected value of dropdown should appear in label.
please help me.

HTTP is a stateless protocol. This means that it'll forget anything that you don't tell it to specifically ask it to.
When going back to Page1.aspx, your program has no idea what was selected before.
In terms of persisting user choices, you should look into storing them in Session ( or alternatively, cookies ) and checking for a pre-existing choice when returning to Page1.aspx.
If you have a saved value for the user at that point, you'll be able to set the correct value during the Page_Load event.

If it is like a wizard, I suggest you use a wizard control in the same page.
here is an example,
https://web.archive.org/web/20211020103244/https://www.4guysfromrolla.com/articles/061406-1.aspx

Related

Confirm Resubmission page on clicking back button

In my page I have a droplist which with OnSelectedIndexChanged event, which initiates a POST request to the server on selection change.
Whenever I navigate away from the page to someother page and try to click the back button in the browser it gives me a "Confirm Resubmission Page" since it has a post data associated for the page.
I cant use redirect to clear the post data since i need the data from the droplist control and other controls to be retained even after the OnSelectedIndexChanged. Is there any solution to come around this issue??
Take a look at this question. May be it can help.
If you can't redirect to self after your post request then you can do it using ajax.

hyperlink and responce.redirect

I am new to asp.net and i need to know
whats the differences between a button with response.redirect behind it
and a hyper link ? they get you both to the page but one is server side while the other is not
is it the only difference
and when it is best to use either one of them
A hyperlink render a link to the given NavigateUrl in the browser. When the user clicks on it, the browser directly goes to the destination.
A button with redirect renders a button which does a postback using javascript. When the user clicks on it, a postback is done to the same page and the browser is instructed to go to another URL using Response.Redirect.
The main difference is that the second solution loads the same page again, while the first solution goes directly to the destination. With a button you can run some code before redirecting, or redirect to a different URL based on the information in the postback. Because it loads the original page before loading the destination page, it is a bit slower.
With a hyperlink, the visitor will see where the hyperlink leads to. With a button you can not see this. A hyperlink is thus better for search engines, because they will follow a hyperlink and will not follow a button.
If you know the URL in advance and you do not want to run extra code when the user clicks on something, use a hyperlink. Else, use a button.
A hyperlink is a link which will redirect you somewhere in the same tab or in a new tab when you click on it.
In case of response.sendredirect() the user's browser is redirected to a link sent by the server depending on your business logic.(the link may belong to some other domain).
hope that helps
Hyperlink control
This is server control use for navigation to another page specified in the NavigateURL property. Hyperlink control doesn’t expose any server side event.
Response.Redirect method
This method is used to navigate to another page from code. You can use this method to navigate from a Linkbutton or ImageButton control.

loading the same page with and without session variables

I have an aspx page. This page is used for both create and edit.
While editing, I set a session variable and load the same page.
While creating , user has to click on menu option and it get redirected to "CreatePage.aspx".
While editing, it will be redirected on click of a column in a grid . this time the response.redirect happens to the same page with Session["Id"] set to some value. Now my question is how and when do I set the session to null?
This should work for Create and Edit using the same page. Any help will be greatly appreciated.
The how is easy:
Session["Id"] = null.
About the when: Can the user cancel a edit/creation? If so, what happens then?
The change of the session variable should be done when the user clicks on either the menu option or the grid column AND when the operation has finished (by either an OK or a Cancel).
Anyways, Changing data in a grid can easily be done with the GridView control. You can see an example here. So you might be trying to "reinvent the wheel" here...

persisting links on an asp.net page - is this a job for viewstate?

I've got a master page with a section for subnavigation links on it:
<div id="sub_nav" runat="server"></div>
I programatically populate this with Hyperlinks when my main asp:Menu data is bound depending on the address of the page I'm on.
This works fine and all my correct submenu stuff shows up on each page. The problem is that when one of these pages does a postback, I lose all the links that were in my sub_nav div.
Now, I could just populate the div with links every time regardless of whether the master page load is a postback or not, but I figured there is a better way of doing this. I was thinking enabling the viewstate on the div and links inside it might persist them through postbacks, but apparently that is not how viewstate works.
What is the correct way of doing this?
Viewstate only stores the current state of a control and not the controls by themselves. If you are dynamically adding controls make sure to add them on page init method irrespective of postback
This MSDN sample should help you.
According to the excellent article TRULY Understanding ViewState, that's not really the purpose of ViewState. Furthermore, ViewState costs additional bandwidth so in general we want to avoid it if possible. It sounds like this data should be "cheap" to obtain (cacheable or whatnot), so I'd definitely populate it on every request and disable ViewState on those controls.
To understand the main purpose of ViewState consider a page with two buttons, btnA and btnB and two labels lblA and lblB.
When the user clicks btnA , the page posts back and sets lblA to "You clicked A!".
When the user clicks btnB, the page posts back and sets lblB to "You clicked B!".
With ViewState, the page remembers that lblA.Text was set to "You clicked A!" previously and restores that value. Without ViewState, if the user clicked A and then B, the page would only display "You clicked B!" because there's nothing to store the previous value of lblA.

Persisting dynamically loaded user controls in view state

I have a simple user control containing two text boxes which I am adding to placeholder on Button Click event. I am storing the number(count) of clicks in View state and running a loop using count to create all previously added user control. I am also adding IDs to each User control (appending "UC" and count). I have also checked in view source the ids are same each time they are created. I have another button which basically does an post back. I have EnableViewState enabled ="true" in all controlls all the way up to Page Level.
My problem is that User Input does not persist on postback. please advice. Should this not be happening automatically for me?
Have a look at this:
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
I've encountered minor problems with it on a web farm, but for simple deployments its use is pretty straightforward. (Comes with source code, and the web farm glitch is a pretty simple fix.)
You need to create your dynamic controls in the Page_PreInit event rather than Page_Load or in a Click event handler. That way, they'll be there before ViewState, and then your posted values are applied.
I thinks what is happening is that you are creating your controls during the click event handler which happens AFTER ViewState and PostBack have been applied. This means your controls will be created empty each time.

Resources