Wednesday, March 28, 2012

Postback? how?

in Ajax, how is it possible to do a postback to the server? So for example, i have a modalpopupdialog, and when i click the ok button in it, i want it to postback to the server to process the request or whatever.

how is this done? Currently I have a scriptmanager on the aspx page, and the User control for the modaldialog - now what?

I've noticed something.

If I don't add this custom user control containing ajax in a webpage, then anything on that webpage, the links and button clicks work fine. However if I add this user control with ajax, the button post backs do not fire at all. This is wierd and no idea why. I think it maybe to do with the scriptmanager or the modalpopupextender but not sure

what would the reason be for the click events to the server not being able to fire when we add this user control?


I'm not sure why postbacks don't work with the modal popup, possibley because it was intended to be used with javascript to customise the page?

In any case, you can use the OnOkScript and OnCancelScript properties of the modalpopupextender to fire a postback. You just need to add this line of javascript as the script that is fired when one of those buttons is pressed:

__doPostBack(TargetControl, Argument);


Replace TargetControl with the control id that triggered your postback (if you want a pass the server a different control id you can too) and replace Argument with any argument you want to pass to the server.

Then, in the Page_Load of your page add these lines:

string controlid = Request.Params.Get("__EVENTTARGET");
string argument = Request.Params.Get("__EVENTARGUMENT");

This will allow you to read the arguments you have passed the server, from here you can trigger a function or event handler.



I actually did that before also but made no difference.

Basically.....

If I have a form and place a simple button on it, double click it to make the click event and do something like Response.Write("I was clicked");, it will work fine.

BUT as soon as I add the modalpopupextender, or even infact, add the assembly reference in the aspx file for the ajaxtoolkit, then when i click that button again - nothing happens. no post back is happening. nothing is firing back at all - the page stays the exact same.


any ideas? I'm stuck. I just created a new ajax enabled project on my main home computer, and the same thing happens - when you insert the modalpopupextender, set all the properties etc... it doesnt fire anything (it shows the control of course) but when you take out that control and press say, the button (which is what the OkControlID property is set to in the ajax control), it works and fires the click event.
looks like there seems to be a problem when using base master templates!! How is it possible to use the Base master templates and making ajax do the postback (of the modalpopupextender)

Works great for me – the code…

<asp:LinkButtonID="btnUpload"BackColor="ButtonFace"Font-Size="11px"Font-Underline="false" ForeColor="Black" CssClass="CenterText"BorderStyle="Outset"BorderWidth="1px"

Text="Upload New Photo"runat="server"Width="100px"Visible='<%#IsAdmin%>'>

</asp:LinkButton>

<asp:PanelID="pnlUpload"runat="server" CssClass="modalPopup"style="display:none">

<asp:LabelID="Label5"Font-Size="11px"Font-Bold="true"runat="server"Text="Upload Photo"/>

<asp:LabelID="ErrorLabel"runat="server"Text=""/>

<table>

<tr>

<tdclass="formlabel"align="right">File:</td>

<td><asp:FileUploadID="FileUpload1"runat="server</td>

</tr>

<tr>

<tdclass="formlabel"align="right">Image title:</td>

<td><asp:TextBoxID="imgTitle"runat="server"CssClass="txtfield"/> </td>

</tr>

</table>

<br/>

<tablestyle="margin-left:auto; margin-right:auto;">

<tr>

<tdstyle="width:100px;"align="left">

<asp:ButtonID="btnUploadImg"runat="server"Text="Upload"OnClick="UploadFile_Click"/>

</td>

<tdstyle="width:100px;"align="right">

<asp:ButtonID="btnClose"runat="server"Text="Close"/>

</td>

</tr>

</table>

<br/>

</asp:Panel>

<ajaxToolkit:ModalPopupExtenderID="modalPopup"runat="server"

TargetControlID="btnUpload"

PopupControlID="pnlUpload"

BackgroundCssClass="modalBackground"

DropShadow="true"

CancelControlID="btnClose"/>

Code behind in vb…

ProtectedSub UploadFile_Click(ByVal senderAsObject,ByVal eAs System.EventArgs)

If FileUpload1.HasFile =TrueAnd IsAdminThen

Dim imageidAsInteger

Dim albumidAsInteger

Dim oAsObject = Request.QueryString("AlbumID")

IfNot oIsNothingThen

albumid =CInt(o)

EndIf

imageid = ImageUtils.uploadImage(imgTitle.Text, albumid, FileUpload1.FileContent)

DataList2.DataBind()

DataList2.SelectedIndex = DataList2.Items.Count - 1

FormView2.DataBind()

FormView2.PageIndex = FormView2.PageCount - 1

ErrorLabel.Text =""

Else

ErrorLabel.Text ="<p>Please select a file to upload<p>"

EndIf

EndSub


yeh it doesnt work. I'm wonder if its to do with the fact that base/master pages are being used, as well as several user controls being placed on an aspx page?
Was anybody ever able to resolve this I have the exact same problem, only controls within a gridveiw fire their events...

I don't think it has anything to do with base pages or master pages. The project I'm working on uses a custom base page, inherits from a master page, and runs more than one user control. The modal popup seems to work just fine. One thing worth considering is building an AJAX Control Toolkit website. You'll find the option if you ran the .vsi installer. It can be informative to look at the reference structure and the web.config after creating a temporary site from that template. Good luck!

Keith


Would you mind posting the markup of your ModalPopup that has working controls?
I finally found the cause of this problem.Smile After going through each control I discovered that when I removed the RequiredFieldValidators from the page, the click events fired. So I put them back and then set causesvalidation = false for the button and again the click event fired. This must be a bug with the extender. So basically you have to disable validation on any control in the modal pupup window.

interesting. ill see if we have this (cant right now) however currently the way it stands, it still does not work. I'm able to do it on a simple aspx page (which does not use a master page) but when using a master page, it does not work.

how do we get it to work with validation, should there be validation in our solution/project?


nope - we dont have any requiredFieldValidators on the pages we are using the ajax control on. - still stuck

No comments:

Post a Comment