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.
Related
I am opening a second webform on a button click of first webform. On second webform, I perform certain action and store it in a session to use it later. Now after performing certain action on second webform and closing it, I want a post back on first webform. (Note: both webform are independent of each other. No parent child relationship between them). However, I tried calling Page_load of first form from second webform, but failed to do as it's not accepting the arguments of page_load. Neither null arguments are accepted by page_load.
Please help !
Page_Load is a page lifecycle event which is supposed to get called when the page is loading.
I don't think you will be able to call it from other page's page_load event.
Alternative is to create a helper class and keep the reusable code there in the separate helper class.
Then both of your web forms can call the helper class method from their page loads.
Hope this helps.
A good option is to use Server.Execute(string) which is going to run the other page events including Page_Load and Page_Init, and return the control to the calling page.
Server.Execute("mysite.com/page2.aspx");
You need to use a Redirect to go back to the first page - the page will then cycle and go through the Page_Load and do what you need to do.
If you need to tell the other page something then add a URL parameter
eg: Redirect("~/page1.aspx?reload=1")
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")
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.
I have a user control that contains a repeater, which in turn is nested in a default.aspx. When a user clicks a specific link on the page, the ajax post function is called which posts data to a method on default.aspx. All good so far! This method then calls a method within the user control, passing in the data, which in turn is used to requery, and rebind the repeater.
For some reason, the page bails when I try to do this last part?
If this seems converluted, it's because it doesn't seem possible in jquery to directly access user control methods.
Many thanks.
Rebinding a repeater control, which is an ASP.NET server control, is done on the server side, not the client side. If you want some grid behavior with ajax I suggest you look at a javascript based grid like for example jqGrid.