views:

203

answers:

3

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:

  1. 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)
  2. User control contains the custom control
  3. The .aspx page contains the user control
  4. 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)
  5. 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)
  6. Method MoveItem is called
  7. 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
+1  A: 

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.

Scott Gottreu
Yea, I have no need for post here. Just send some variables back to the same page because in the code-behind of that .aspx page, I'm checking for querystring variables. So if using html() how would my code-behind grab those variables that are passed? And I would still need to do a page redirect because otherwise my page_load will not be hit on both the .aspx and .ascx after my method fires in the codebehind. I need to rebind in the page load otherwise the lists don't show the new state after the item was added/removed from the lists
CoffeeAddict
The method I"m currently checking (check for that querystring flag) and calling is a code-behind (server-side) method.
CoffeeAddict
Scott, can you remove your last comment and include that as part of your answer. Never thought about doing it this way but I guess it makes sense to remove the item using jQuery after the method call.
CoffeeAddict
Here's another thing. I don't want this jQuery code to be tightly coupled to the .ascx because the .ascx should know nothing about the .aspx using it and its code-behind methods. So then putting it in my .aspx will require me to just get a reference to that .ascx (which I already have) and then once I have the .ascx via ref ID I can get at the custom server control to grab the ID using html(), etc. Anyway, just a bit more juggling through ASP.NET object to get what I want into my jQuery script in my .aspx. Nothing crazy and probably common but point is that's what I'll have to do then
CoffeeAddict
(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.) Hmmm, if this is true than that defeats this route and is much more efficient than this: http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/
CoffeeAddict
+1  A: 

Remember that the first A of AJAX is asynchronous. You'll want to avoid having your button reload the page, of course. Something like:

  1. Figure out the data you need to send to your "add to shopping cart" handler page -- part number (SKU, etc.), colors, sizes, quantity, etc.
  2. 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.
  3. 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.
  4. In the response handlers, update the shopping cart section of the page with the number of items or indicate success or failure.
NVRAM
(Replace the custom control hyperlink/button href with an onclick handler instead) That defeats the entire point of using jQuery selectors and events. I will never use onclick again like that which also goes against the entire concept of keeping javascript seperate from content "unobtrusive".
CoffeeAddict
again why use $.ajax() to send stuff to a page handler when I guess you can just use $.get() to call the server-side method on the .aspx
CoffeeAddict
FYI, **$.get()** is a wrapper around **$.ajax()** and as the documentation states, one reason you'd use **$.ajax()** is when you want an error handler that you didn't specify a globally.
NVRAM
To your first comment, I think you are misunderstanding me (re-read first sentence in the post). "onclick" is the attribute on an HTML element which is used to handle a mouse click (and sometimes keypress). It can be assigned via jQuery **$('.xyz').click(someFunction)**, in HTML or in JavaScript.
NVRAM
+1  A: 

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"?

David Robbins
thanks but we are using repeaters..they are clean easy to use and efficient.
CoffeeAddict
And sorry I hate MS AJAX. Will never go that route again after jQuery. MS AJAX is substandard, slow, clunky, and reliant on one technology.
CoffeeAddict
I see that article you showed about calling methods but that's using .ajax. Why not just use the $.get() that Scott Gottreu mentioned to call a server-side method that's not via web service and resides in code-behind. Seems much cleaner, easier, etc.
CoffeeAddict
Sorry if I wasn't clear, but with DataTables you be able to KEEP the repeater. The article I posted uses the jQuery ajax functionality, so it is NOT MS Ajax. If you look at the blog I linked it has an article that does what you suggest.
David Robbins
thanks! the links were helpful.
CoffeeAddict
Cool - Dave Ward's blog is a great site for jQuery and Asp.Net integration. It really cleared things up for me.
David Robbins