ASP.NET 4.0 Webform website
master page:
left side panel (navigation menu purpose): use UpdatePanel control (AJAX), so when selecting new menu, the whole page won't reload. Only content part will be updated.
My question is -- I want to be able to use browser back button to go back to previous menu (with form state saved). However, when looking into the viewstate, it's always the same. Is there any way to do this? Thanks.
I guess one work around would be, when a user selects a menu item from the menu, put that selection in a session variable and in the page load. Then when the user hits the browser back button, you can set the menu in the page load, since you have values in your session variable.
What pst is referring to is "cross-page posting".
On your source page, you will need to set the asp button's postbackurl to your target page url.
In the target page c# onload method, set page.previouspage to your source page. Check if the previouspage's post was a crosspagepost.
If true, get zource page's viewstate.
How do you get access to the source page's viewstate from the target page? Save the source page's viewstate in the source page before going to the target page. Save the viewstate in an asp hidden variable on the source page, then get it in the target page using previouspage.findcontrol("variable name").
After getting the source page's viewstate, save it in an asp hidden variable on the target page.
When you want to return to the source page, you will do everything above in reverse, in a way.
The "back to previous page" link button will use the source page url for the postbackurl value.
In the source page's onload c# method, set the page.previouspage url to the target page's url. Check if the previouspage's post was a cross-page post.
If true, in the source page (after returning from target page) use previouspage.findcontrol("savedSourcePageViewstate") to get the saved viewstate of the source page.
I am not exactly sure yet how to then change the source page's viewstate to the saved viewstate. you might actually have to do it in the target page's backttopreviouspage button's click handler.
I am working at developing this process right now in a project i'm developing at work.
When i get it working, i'll create a full walkthrough for this, with code examples, and post the link to the walkthrough here.
However, i already haveworking the cross-page posting process of getting data from the source page in the target page.
It is secure, and is the best method that i have come across for getting data from one page to anotherin an asp.net webforms application.
Related
I have a group of controls on the Master Page. I use as a collection of links(HyperLink controls) to all the pages. When I navigate thru the pages I was setting the specific link to hl.Visible = False for the page currently open. I have noticed that this messes up the ViewState. I hate having a link to a page that is already present, is there a way to do this without messing up the ViewState? There are no dynamic controls on the page.
What techniques would you use for this?
More generally this error happens when the control hierarchy of the page changes in a way that prevents the framework to load the view state. The view state mechanism assumes that the control hierarchy is the same on load as it was when it was saved.
This might look as a random error because there are cases when changing the controls in the page does not prevent the view state from loading. Have a look in your page and look for controls that are dynamically created/deleted. Ensure that any controls are added to the page before the view state is loaded (that would be before page Load event).
Unfortunately there is no way to see which exactly is the guilty control that is not added correctly to the control hierarchy. A common way to see this error is to add some dynamic controls on an event (for example on a drop down selected index changed) - this way their state is saved to view state - but not add them again on postback - this way the view state is invalid because the controls do not exists any more when the view state is loaded.
Source
I need to have datagrid(3 columns) to which the values will be added using 3 textboxes and a button(Add) on the same page. If click on Add button the value should append to datagrid without page refresh/reload?
Please guide me with your approaches to this problem? I ran out of search.
Have you considered using Ajax on the client-side? I presume that clicking the "Add" button is also supposed to update something on the back-end, which means you must do a round-trip of some kind. Either via a postback, but since you don't want this, then the only other option is Ajax whereby you'll make a separate asynchronous post to the back-end that will not cause a page refresh, and on the client script, inject the correct elements to "update" the grid on-screen. You could get that working as a prototype and then use knockout to simplfy the process.
1) I have used iframe in my default aspx page.
2) Created a new aspx page "datagrid.aspx" and had my datagrid as the only control in that page and set the src attribute of iframe to "datagrid.aspx"
3) Datatable to be rendered in datagrid.aspx is stored in the session variable in page load of default.aspx.
4) In the pageload of datagrid.aspx datatable is retrieved and binded to the datagrid.
5) When an item to be added/updated/removed are sent through querystring parameters from default.aspx using onclick event (JS) of a html button by setting the iframe source attribute.
Hope this answer helps.
If its not clear, Please comment.
I have 2 asp.net web forms. The first has a ScriptManager, History, UpdatePanel and GridView; the later is ScriptManager, UpdatePanel and TextBoxes. The premise here is a list that links to a detail form where an item can be edited.
From within the GridView (inside the UpdatePanel), if I use a HyperLink control with the url set to the edit page (with the necessary parameters), change something, save it and then click the back button I see the original list with no updates. Pressing F5 to refresh shows the changes.
If instead I use a LinkButton inside the GridView, and handle that LinkButton in code-behind to perform a Response.Redirect to the same edit page (with the same parameters), make the same changes, save and then click the back button, the list on the original page refreshes automatically to show my changes.
Note that the code in the detail page where the editing/saving takes place does not change - only the way it is first displayed is changed.
My question is this: what is it about the Response.Redirect that causes the page to be refreshed when the back button is clicked, and it it possible to replicate this for the direct HyperLink approach? I would prefer to use the HyperLink method as I see no reason for the postback, but I want the GridView to refresh when the user browses back to it.
Thanks.
A LinkButton causes a postback, the response to which is a HTTP 302 redirect command triggered on the server side by your Response.Redirect. Your web browser therefore does not cache the old version of the page.
The Hyperlink control simply renders a regular <a> tag which takes you to the detail page on the client side. The browser has no reason to believe the page may have changed, so it presents the cached version when you hit the back button.
If you want to tell the browser specifically not to cache the page if the back button is used,
use the cache-control HTTP header. W3C Link,
In any case, you should provide a link on the detail page (or automatic redirect on accepting changes) which takes the user back to the GridView/summary page, so they don't have to resort to using the back button.
Edit:
Sorry, the previously provided header example was not for Asp.net, but basically you'll want to do something like this:
Response.AppendHeader("Cache-Control", "no-cache")
I have a hyperlink on a page that sends certain variables e.g. Default.aspx?var1=x&var2=y
I then create an ImageButton dynamically thereafter. My ImageButton has its own event.
It seems that the ImageButton inherits the values sent from the hyperlink. If I right click on the image button and view is properties via the browser it points to the following as well Default.aspx?var1=x&var2=y , I then tried to turn view state off which didnt work.
I also tried specifying the PostBackUrl so that the url variables are not sent. But then its event doesnt trigger.
Is there any way to not h
The postback URL for a form in ASP.NET will ALWAYS be the same URL that the form was originally loaded using. So if you load a page from a hyperlink with a querystring, then use a postback control on that page (like an ImageButton), it will always post back to the same URL it was originally loaded with, including the querystring.
There are different ways to change the postback URL that depend on the version of ASP.NET you're using, but if 2.0 or below it will have to be javascript. Search on this issue specifically (changing postback url or form action in asp.net) to find the answers to your problem.
Other less elegant solutions include redirecting a page to itself with no querystring after the initial load with a querystring.
You can also just ignore querystrings on postback, which is almost always the desired behaviour, so for the most part this is a cosmetic problem. E.g. in your code just do...
if (!Page.IsPostback) {
// check for querystring data and do stuff if there, otherwise ignore it
}
That's default behavior of ASP.NET. Since ImageButton will just cause the execution of event handler , the hyperlink attached to it will be its same page.
Why is this a concern ?
Ok, this is a bit different scenario. I guess I would have to think about doing it this way sort of with MVC anyway if we were actually using MVC...but we're not at the moment.
So I've got and .aspx page. In that .aspx page is a user control (.ascx). And in that user control is a custom control (.cs).
The custom control has a repeater in it. So I'm showing a list of items on that .aspx through the .ascx's custom control. For each item in the repeater is a button. It's just a hyperlink, just a regular on my page
When you click that button, it redirects to whatever page you're on. Since the custom control never knows what your parent .aspx page is, I'm doing a redirect to the self .aspx by doing a Response.Redirect(Request.Path). So that way it always redirects to whatever .aspx is using that user control and custom control.
So after it redirects to self, I check the querystring in the page_load of whatever .aspx is using it. If the value is true, then I handle it however the .aspx wants to. In this case when it's true, I call a method in the code behind of my .aspx that handles the action for the button. For example lets say that button was "Add to Shopping Cart", the .aspx handles that action and calls a AddToCart method in the .aspx.cs.
I'm not using an ASP.NET control for the actual hyperlink and button because I just don't need it and in my particular case I'm using a user control and a custom server control. For this instance, I had some issue where I didn't wnat to use an ASP.NET control...I forget why but the point is, no this is what it is.
So with that, I'm trying to figure out how I can apply some AJAX here call to call that method instead. I still need to somehow redirect again back to the same page like I'm doing...I'm doing the redirect in that method after all the logic at the end. I am redirecting again back to the same page, because I need my Page_Load methods in my .aspx and also in an .ascx to still fire off after that method is completed.
So I am not sure where to start on this. Let me go through this once again:
Custom control has a repeater in it and in the repeater, each item has a standard HTML hyperlink (non ASP.NET control) which wraps a standard image tag (image is a button)
User control contains the custom control
The .aspx page contains the user control
User clicks the button and hyperlink redirects them to the parent .aspx page that is using this custom control...so it calls Response.Redirect(Request.Path)
In the code-behind of this .aspx, in my page_load I check a querystring flag to see if I performed that action..meaning user clicked that button. For example one of the querystring params is "AddItem" and another querystring param is "itemID". If movedItem is true, then I fire off a method called MoveItem(int itemID)
Method MoveItem is called
Method MoveItem redirects again back to this same .aspx using Response.Redirect(Request.Path).. this is so that the page load is hit again as well as my .ascx page load is hit. Because in both those page loads, I rebind a repeater so I can show the latest state of the lists. I call a method in my .aspx page_load which rebinds a grid and then page_load in my .ascx also calls another method which rebinds some other list
You can use $.get() to pass the variables to a server-side method that performs any server-side functions you need. You don't need to run page_load or have a code behind.
If you only need to update the HTML in the client's browser then you can use jQuery to add/remove them from the lists in the HTML. You can use the html() function in jQuery to append the item to the list.
You should look at DataTables.net as you can build a client side, editable grid that will perform Ajax updates as well. You would be able to keep your repeater control, but eliminate all the back and forth to the server and the deciphering of clicks and coordinating between Page_Load and etc. Here is a good post from Dave Ward(Encosia) that covers jQuery, Page_Methods and repeater controls.
If you want to preserve your work so far, why not try Ajax Update Panels around the region that you do not want to visible "refresh"?
Remember that the first A of AJAX is asynchronous. You'll want to avoid having your button reload the page, of course. Something like:
Figure out the data you need to send to your "add to shopping cart" handler page -- part number (SKU, etc.), colors, sizes, quantity, etc.
Ensure this data is on every applicable page, in an identical fashion: perhaps one or more hidden input elements, or even from the page URL.
Replace the custom control hyperlink/button href with a [client side] onclick handler instead, which will get the data from step 2 and send it via $.ajax() (or $.post or $.get), specifying the response handlers. You probably want to disable the button or give other visual feedback to avoid duplicate clicks.
In the response handlers, update the shopping cart section of the page with the number of items or indicate success or failure.