Make postback when the OkButton in a ModalPopup is clicked in ASP.NET AJAX

余生长醉 提交于 2019-12-22 12:18:10

问题


I follow the example in Atlas: Creating a Confirmation Using the ModalPopup Extender to make a postback when the OkButton in a ModalPopup is clicked (it uses the ModalPopupExtender that comes in ASP.NET Ajax Control Toolkit), but as I can see, the Sys.WebForms.PostBackAction() is no longer present in ASP.NET AJAX (the example is for Atlas). When I run it the "Microsoft JScript runtime error: Object expected" error message raises, in the line in javascript where I create the PostBackAction. How can I make this works in ASP.NET AJAX, or maybe, there's another way to do it? Thanks


回答1:


Have you thought about using the ConfirmButtonExtender? You get the modal "bling" through the extender, and you'll have the PostBack processing you need.

You wire the button click event as normal for processing, but the Confirm extender will "intercept" the processing and allow things to continue if the user confirms the action.




回答2:


Server side events do work with the ModalPopupExtender. What you need to do is to create a button with a display style set to none. Then set the TargetControlID property of the ModalPopupExtender to the hidden button ID.

<asp:Button ID="btnShowModal" runat="server" Text="Show" OnClick="btnShowModal_Click" />
<asp:Button ID="btnHidden" runat="server" Style="display: none" />
<ajaxControlToolkit:ModalPopupExtender ID="modalExtender" runat="server" 
    TargetControlID="btnHidden" PopupControlID="divId" />

In the OnClick handler show the modal:

modalExtender.Show();

In the handler for the control that will close the modal (usually a button) hide the modal:

modalExtender.Hide();

I'm able to successfully use this method for posting back to the server in order to populate controls in my modal with info from a database during the postback. Works like a champ.




回答3:


If I remember the ModalPopupExtender correctly, it has an OKControlID that you set as your button ID.

Could you not just assign your button on "onclick" clientside handler that calls a postback?

For example, something like:

 myModalPopupExtender.OkButtonID = myOKButton.ID;
 string script = ClientScriptManager.GetPostBackEventReference(myOKButton, myOKButton.ID);

 myOKButton.Attributes.Add("onclick", script);

EDIT: It looks like another potential solution here.




回答4:


You can place an in-visible button in the panel and set it as OkButton. Add your actual button in the panel and add its OnClick event handler. See code below:

<cc1:ModalPopupExtender 
TargetControlID="btnSubmit"
DropShadow="true"
OkControlID="btnOK"
CancelControlID="btnDecline"
PopupControlID="pnlTermsAndConditions"
BackgroundCssClass="popup"
ID="ModalPopupExtender1" 
runat="server">
</cc1:ModalPopupExtender>
<asp:Panel ID="pnlTermsAndConditions" runat="server">
<table width="500" cellpadding="6" cellspacing="0" border="0" bgcolor="white">
<tr>
    <td><b>Tables and Content</b></td>
</tr>
<tr>
    <td><asp:TextBox ID="TermsAndCondition" runat="server" TextMode="MultiLine" Rows="10" Columns="60">
    This is my terms and condoition
    </asp:TextBox></td>
</tr>
<tr>
    <td align="center">
    <asp:Button ID="btnAccept" runat="server" CssClass="btn" Text=" Accept " OnClick="btnAccept_Click" />&nbsp;&nbsp;
    <asp:Button ID="btnDecline" runat="server" CssClass="btn" Text=" Decline " />
    <asp:Button ID="btnOK" style="display:none;visible:false;" runat="server" CssClass="btn" Text=" OK " />
    </td>
</tr>
</table>
</asp:Panel>



回答5:


This is the solution I have been looking for. Great post. Incidentally, it is not neccessary to create the mock OK button. Just don't include the optional OKbutton prameter in the Modal Popup Extension definition.




回答6:


Maybe just adding a server side OnClick event to the button? This should post back the whole form. Or you could get the main form from html ($find('myFormName') or something) and do a .submit() on it.



来源:https://stackoverflow.com/questions/809222/make-postback-when-the-okbutton-in-a-modalpopup-is-clicked-in-asp-net-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!