Accessing server controls from different pages - asp.net

I have a GridView on PageA, I wish to be able to call the DataBind() method of the GridView from PageB?
Basically PageB is a form which will be contained in a pop up control, when the user fills in the fields of the form on PageB and submits it I will close the pop up control, then I want to refresh the GridView on the parent page (PageA), how would I go about doing that?

Write a javascript function on Page A like RefreshGrid, you can write various ways to refresh grid from this javascript function e.g.
1) put your grid inside an update panel and create a trigger button control on that update panel , call this trigger control's click event in javascript , this will partial post back the page and refresh the update panel. There are other ways too..
2) From popup on popup close button, write following line
window.opener.RefreshGrid(); window.close();
Thats it.
If you want to pass any parameter you can pass through as argument to RefreshGrid function.
Still there are many ways , this is the one I use.

Related

Button in CustomControl added in OnClick doesn't postback until second click

I have the following scenario:
UserControlA contains a <asp:Button id="bSomeid" onClick="AddItem" /> with some code to an item to a shopping basket in AddItem.
UserControlB contains some LinkButton's that dynamically add a selection of UserControlA to the page in the OnClick event.
This is all done in an UpdatePanel. It is a little more complicated but I have pruned the information to what I believe is causing the problem, I will add more information if necessary.
The problem I have is that it takes 2 clicks for the AddItem event to trigger after I have added the items to the page after clicking the LinkButton.
I understand why this is happening - it is to late in the page cycle to register events for the next post back in the onclick - but can anyone think of a way around this? Can I force an event to be triggered on the next postback? I have tried to think of a way to run my code in page_load but I requuire access to the sender in the onClick.
Using .NET 4.0.
EDIT
I managed to find a way to get the link button sending the request in the Page_Load (using Request.Form["__EVENTTARGET"];) so I moved my code to the Page_load event. It still requires 2 clicks so I am assuming it isn't something to do with the onClick being registered to late.
Are there any other general things to check that could cause a button to require 2 clicks to post an event properly?
If your suspicion about being late in page life cycle is true then you can try using ScriptManager.RegisterAsyncPostBackControl method to register dynamically added controls in the link button click - considering that your button is within user control, you need to add public method into UserControlA that would actually register the button bSomeid1 and link button click from UserControlB would actually call the A control's method.
EDIT :
Another cause for button click not happening can be that button being dynamic control is not added in the page hierarchy when post-back happens (or it gets added very late in the page life cycle when the post back data is already processed). A really full-proof solution should add dynamic controls back to the page hierarchy in page_load it-self (and strictly maintaining same controls ids within hierarchy). If that's not possible then you can sniff the request (Request.Form) to detect the post-back.
In your case, you should ascertain if the button is indeed causing the post-back on each click. If yes, what is the POST data (Request.Form) for the first request - what is the __EVENTTARGET value on the first click (and post-back)? That should start your trouble-shooting.
On the other hand, a simple work-around could be to use html anchor element (you can still use link button) and have a javascript handler in the click event that would set some hidden variable and then submit the form (you can simulate the click on hidden button to trigger ASP.NET client side submit pipeline) . Now the hidden variable value can be used on the post-back to determine which link button has been clicked.
"Are there any other general things to check that could cause a button to require 2 clicks to post an event properly?"
Does it require two clicks on the control, or does it take accept a single click elsewhere on the screen, and then fire first time with a single click on the control?
I have my own (similar) issue with the Updatepanel where the first (expected) trigger does not fire and it seems that a single click elsewhere, and then the subsequent triggers fires first time (which totals 2 clicks)
[edit] Since you are working on this ATM, it may help me as well. Do you have a textbox with a trigger event on it? I do, and if I leave this blank (so that it does not fire) then there is no need for a second click.

AJAX Add a dynamic form and AJAX Post data

This is what I'm trying to do with AJAX:
[DropDownList]
I have a DropDownList (not inside an UpdatePanel), populated by different Products to "Add" to the database.
[UpdatePanel #1]
Below, I have an Conditional UpdatePanel that listens for "SelectedIndexChanged" on the DropDownList, when that event is triggered it adds TextBoxes to a div "productForm" inside the UpdatePanel. It creates the Form according to the Product to add.
[Button]
Below the UpdatePanel I have a button that "should" submit the form above.
[UpdatePanel #2]
I have an update panel that listens for the event on Button "Click" event. I also have a div in the ContentTemplate that should post out data that was submitted from the "Add Product Form" in the first UpdatePanel.
The thing is, when I submit (and the Controls are still visible in the first UpdatePanel. It can't read the data from the TextBoxes becaus they aren't there. Also, if I try to add all this to the same UpdatePanel, the Controls disappear whenever I click the Submit button.
Any ideas how to make something similar work?
Without the code I can suspect that when you dynamically populate first div on dropdown selected index changed event, since its within update panel, viewstate is never made aware of new controls and on submit can't post them back to server.
I've had some bad experiences with update panel and never use it other than very simple scenarios. Try getting familiar with jQuery ajax. I would do what you want to do using jQuery and web methods.

Prevent Postback in array of linkbutton is created at dynamically

i have created Array of Linkbutton
and when user click on link button it will create an array of Radio Buttons
but it requires Postback all time so page load takes more time...
what is solution of it??
Use asp.net Ajax update panel.
Put link buttons and panel/view (or what ever control you are using ) update panel.
I am not sure if I am following a 100% but if you don't want to do a post back then your going to need to write some javascript(or use a framework like jquery) to generate radio buttons when the user clicks the link button(plus you probably don't want to use a link button then as it will try to always cause a postback or you will need to stop the post back from happening).

jQuery/AJAX redirect?

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.

Passing data to server code from a client-side control using ASP.NET AJAX

I have a ListView on a page that displays a list of widgets. When a user clicks on one of the items in the list, I want to display a ModalPopup that contains controls allowing the user to operate on the item they selected.
I could easily accomplish this by placing a Panel and a ModalPopupExtender in the ListView's ItemTemplate, but this mean one set of hidden controls for each and every widget, which would massively bloat the page size. (There are going to be some rather heavyweight controls in there.) Instead I want to reuse a single ModalPopup for each of the widgets in the list.
I've done some searching but I haven't found anything that applies directly to my situation before. From what I've been able to figure out, however, I have to do something like this:
Place a Panel and a ModalPopupExtender on the page inside an UpdatePanel.
Build a custom WidgetManipulator user control that has a WidgetID property. Put this in the Panel, along with a couple OK/Cancel buttons.
In Javascript on the page, attach a click handler to each widget in the ListView that triggers a postback on the UpdatePanel.
On the UpdatePanel_Load event on the server, display the ModalPopup and then set the WidgetID propety on the WidgetManipulator to the ID of the clicked widget.
On the OKButton_Click event or CancelButton_Click event on the server, hide the ModalPopup. If OKButton was clicked, call WidgetManipulator.SaveChanges() first.
The part I haven't figured out is: How the heck do I know what widget was clicked on, and how do I pass that back to the server when I refresh the UpdatePanel? Is this even the right approach at all?
If you can use jQuery instead you could do something along the lines of these two posts:
Modal Delete Confirmation Version
Two Using jQuery SimpleModal Plugin
Demo
Inserting Content Using
jQuery SimpleModal Plugin Demo
When I need to pass data from client to server in ASP.NET AJAX, I generally use an asp:HiddenField with runat="server". Both can see it freely, but beware potential postback asynchronicity.
Sounds like you need to notify the server the widget was clicked - You may use a Timer to postback; or I'd go with option 5.

Resources