AJAX Add a dynamic form and AJAX Post data - asp.net

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.

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.

Accessing server controls from different pages

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.

What is the difference between AsyncPostBackTrigger & PostBackTrigger?

What is the difference between AsyncPostBackTrigger & PostBackTrigger?
Controls inside an UpdatePanel by default cause a partial page update, controls outside cause a postback, using these triggers it is possible to change this behaviour as required.
From http://seminaarit.codezone.fi/video/devdays-2007/track1/2/2-ASP-dotNET_AJAX_Extensions.ppt: (dead link)
AsyncPostBackTrigger
Converts postbacks into async callbacks * Typically used to
trigger updates when controls outside an UpdatePanel post back * If
ChildrenAsTriggers="false", can be used to specify which controls
inside UpdatePanel should call back rather than post back
PostBackTrigger
Lets controls inside UpdatePanel post back. * Typically used to
allow certain controls to post back when ChildrenAsTriggers="true"
Here's a blog post which explains the difference:
In the template in an
update panel, there are the options of
an AsyncPostBackTrigger or a
PostBackTrigger.
By default, controls outside of an
update panel will trigger a normal
synchronous post back. The
AsyncPostBackTrigger “wires” up these
controls to trigger an asynchronous
post back. Conversely, controls
declared inside an update panel will
trigger an asynchronous call by
default. The PostBackTrigger short
circuits this, and forces the control
to do a synchronous post back.
1. AsyncPostBackTrigger
it is the one which enforces Asynchonous post back of the Page.., i.e. the AJAX way. The data will be transacted without full post back. When you are using functionalities like login, you may use this.
Ex. You are having two dropDowns Viz., Countries and States. the states should be loaded when a country is selected and it should be changed on Countries change.
You may use AsyncPostBackTrigger in this scenario, which will populate the states ddl without full post back.
2. PostBackTrigger
It is the one which does not follow the AJAX functionalities, but the full post back as usually(as without using UpdatePanel). Situtions are there where you would not like to enforce Partial Post back (as explained in Point 1. above).
Like you are having FileUpload Control withing the UpdatePanel and when you do it by AsyncPostBack, you will not get any values to the server. It requires Full PostBack. in such a case you should use this trigger.
Suppose Button1 is inside your Update panel and Button2 is outside the update panel.
Now let's undertsand that the controls that are outside the update panel are doing a Asyncpostback and that are inside creates a Syncpostback.
So as both buttons are on a form Button1 in inside Update panel and bUtton2 is outside it.
So by the way by giving the Button2's ID and its Event name to the Asyncpostback Trigger as given in the example we suppose that now it will create a syncpostback with the updatepanel as like the Button1.

GridView.PageIndexChanging fires but there's no SelectedIndex afected!

I have tow diferent implementations for the same problem.
A gridview that is binded with some data and it has a select column that has a button. When the button click is fired I know in debug that the : sender.SelectedDataKey and sender.SelectedIndex have values that I use later.
But now I whant to use the ajax accordion control. I have an Accordion, and one Pane inside. Inside that pane I have a CollapsiblePanel (that uses the CollapsiblePanelExtender). And in that CollapsiblePanel I have my Gridview.
So, only when I click on the collapsiblePanel I want to get data from DB and bind it to the GridView. But it seems that using this methot the sender.SelectedDataKey and sender.SelectedIndex are Nothing (VB) when the PageIndexChanging fires!
This does not make any sense!
The GridView is the some on both implementations and EnableViewState=true
Thank you.
With the Accordion Panel being an Ajax control, it might be adding the Update Panel in there even though you did not add one to the page. Since the GridView is inside of an Ajax control, all the events that are being triggered by the GridView are going to be captured by Ajax. You might try making the GridView button clicks an Ajax callback as well.
I have run into this problem before where the controls inside of an Ajax Update Panel or an Ajax control; such as the Accordion Panel, fire the server side event handler, but because Ajax is involved, the values were stuck back on the client side.
be sure that your controls are in the same UpdatePanel, or update the various container UpdatePanels.

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