The code of the UserControl with the ModalPopupExtender -> PopupInputPanelOkCancel.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="PopupInputPanelOkCancel.ascx.cs" Inherits="ABC.PopupPanels.PopupInputPanelOkCancel" %>
<%@ Register TagPrefix="atk" Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit, Version=4.5.7.607, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e" %>
<span style="display:none">
<asp:Button runat="server" ID="dummyButton" />
</span>
<atk:ModalPopupExtender ID="popupExtender" runat="server" PopupControlID="pnlPopup" TargetControlID="dummyButton" CancelControlID="cmdAbbrechen" BackgroundCssClass="modalBackground" />
<asp:Panel ID="pnlPopup" DefaultButton="cmdOk" runat="server">
<div id="Div1" style="border-style: none; padding: 5px;" runat="server">
<table style="border: 0">
<tr>
<td style="background-color: #CCCCCC; height: 15px; text-align: right" />
</tr>
<tr>
<td style="background-color: #FFE580;">
<div style="padding: 10px;">
<asp:Label ID="lblLabel" runat="server" Text="Name"/>
<asp:TextBox ID="txtInput" Width="300" runat="server"/>
</div>
</td>
</tr>
<tr>
<td style="background-color: #FFE580; height: 20px; text-align: center">
<asp:Button ID="cmdOk" OnClick="CmdOkClick" CssClass="button" runat="server" />
<asp:Button ID="cmdAbbrechen" CssClass="button" runat="server" />
</td>
</tr>
</table>
</div>
</asp:Panel>
In the code behind, I have a property to set the BehaviorId of the extender and one to get the field on which we should set the focus after the panel pops up -> PopupInputPanelOkCancel.ascx.cs
public string BehaviorId
{
get { return popupExtender.BehaviorID; }
set { popupExtender.BehaviorID = value; }
}
public string FocusId
{
get { return txtInput.ClientID; }
}
In the aspx page, I add the control like this:
<uc1:PopupInputPanelOkCancel ID="PopupInputPanelOkCancel1" BehaviorId="addPopupBehaviorId" runat="server"/>
Make sure that you add the scriptmanager on top of the page (if not added somewhere else in a parent control.
<asp:ScriptManager ID="asm" runat="server" />
To call the show method, I added the following code to a button:
<asp:Button ID="cmdSorteAdd" CssClass="button" runat="server" OnClientClick="showModalPopupExtender('addPopupBehaviorId');return false;" />
Additionally, I added the following javascript code at the end of the aspx page
<script type="text/javascript">
function pageLoad() {
// $find is not jQuery. It's from MS and returns an AJAX control.
var modal = $find('<%=PopupInputPanelOkCancel1.BehaviorId%>');
if (modal != null) {
modal.add_shown(modalPopupExtenderShown);
}
}
function showModalPopupExtender(modalBehaviorId) {
// $find is not jQuery. It's from MS and returns an AJAX control.
var modal = $find(modalBehaviorId);
if (modal != null) {
modal.show();
}
}
function modalPopupExtenderShown() {
//jQuery selector
$('#<%=PopupInputPanelOkCancel1.FocusId%>').focus();
}
</script
What I don't like yet is that I need to set the value of BehaviorId (addPopupBehaviorId) manually in the
OnClientClick="showModalPopupExtender('addPopupBehaviorId');return false;"
It would be perfect if one would need to add it only in the
<uc1:PopupInputPanelOkCancel ID="PopupInputPanelOkCancel1" BehaviorId="addPopupBehaviorId" runat="server"/>
But it does work :) My problem I am still on is that when the function modalPopupExtenderShown is called, the focus() does fire a postback in my case. I don't understand why there is a postback. Do you have any ideas? -> Edit: I used the wrong function. I used Microsofts $find('id').focus() instead of jQuery's $('#id').focus() to set the focus. Now, everything works like a charm.
Other than that, my solution works fine and I hope that someone finds it useful.