AspdotnetCodes.com
ASP.NET 3.5 Web Hosting – Click Here
 
Articles Subscribe for our Articles Updates
Books
Resources
Downloads
Free Tech Magazines
Archives
Softwares
Newsletter
Suggest Us
Link to Us
 ModalPopup Extender with PostBack and Set Focus
Posted by Moderator1 on 6/12/2007 10:06:08 AM Category: AJAX
Total Views : 55710
Add to my favorites
Email to friend
  
Introduction
This article explains about the Asp.Net AjaxControlToolkit's ModalPopup with Postback and Focus setting methodology.
Description
I have seen in many asp.net forums, the developers post lots of questions on Asp.net’s AjaxControlToolkit's ModalPopup extender. Actually they want to do postback operations and want to set focus on the controls placed in the ModalPopup window. So I think many web developers who used AjaxControlToolkit will be very much pleased if someone address this issue. May be Ajax Future Release might give the solution for this problem, but what about the current developments which is already undergone in AjaxControlToolkit.
Guys, I found a trick to solve this issue. So I would like to share it with you all, how I made the ModalPopup extender to postback and perform the code-behind operation. Also how I set the focus to the TextBox control in ModalPopup. Then click when you the OK button on the ModalPopup, it will postback.

To achieve this, in your MS Visual Studio, open a new Ajax Enabled Website. Add a Master Page and a Default Page. Set the MasterPageFile property of the Default page to the point to the Master Page. To the Default.aspx page, add a Hyperlink control, a Label and a Panel control. Inside the Panel, add a Textbox and two Buttons, one to OK and another for Cancel.
The actual flow is, when you click on the Hyperlink the ModalPopup has to open, the focus should be on the Textbox. You have to type some text in the Textbox, then click on the OK button. The page will get postback and your typed text will appear in the Label control on the main page. That’s it.

Now we will see how to do it. First set the TargetControlId of the ModalPopup extender to the Hyperlink name. Then set the CancelControlID to the Cancel Button, OkControlID to the OK Button and the PopupControlID to the Panel1 in your Page. Set the BackgroundCssClass property as to the following style class.
.modalBackground 
{
background-color:<Your Desired Color>;
filter:alpha(opacity=70);
opacity:0.7;
}
This style sheet class keeps the background effect dull. So now you can run your application. When you click the HyperLink, the ModalPopup comes to action. Sure it will impress you lot. No postback and no focus on your Textbox, only cancel button will function. Now we can see how to set focus on the Textbox.
<script>
        var clientid;
        function fnSetFocus(txtClientId)
        {
        	clientid=txtClientId;
        	setTimeout("fnFocus()",1000);
            
        }
  
        function fnFocus()
        {
            eval("document.getElementById('"+clientid+"').focus()");
        }

</script>
On your aspx page, copy and paste the above two javascript functions. As we are using the MasterPage, the client id of the TextBox will be changed. So the function fnSetFocus(txtClientId) is used to the assign the ClientId of the Textbox to a local variable. Then the setTimeout calls another function fnFocus(). This function will be called exactly after 1 second, that is after the ModalPopup gets popped up, then this function is responsible to set the focus on the Textbox. And in your code behind's page load event, you have to add a line of code as follows.
HyperLink1.Attributes.Add("onclick", "fnSetFocus('"+TextBox1.ClientID+"');");
This line adds the javascript function on the client-side click of the HyperLink. Now run your application, sure after the ModalPopup gets on screen, exactly after a second you can see the focus on the Textbox. Even you can adjust the timings to half second. Next, we will concentrate how to perform postback action when the OK button on the ModalPopup is clicked.

First, add a javascript function to call the __dopostback event. The function must look like the below.
function fnClickOK(sender, e) { 
__doPostBack(sender,e); 
} 
Then in your code-behind, you must assign the OnClientClick property of the OK Button on the page load event. The code is
Button2.OnClientClick = String.Format("fnClickOK('{0}','{1}')", Button2.UniqueID, ""); 
When the OK button is clicked, postback should perform some action. So let us assign the Textbox value to the Label control. Add the following piece of code in your OK Button's onclick event.
Label1.Text = "You Type "+TextBox1.Text;
That's it. Now run your application. When you click the Hyperlink, the ModalPopup will appear. Next you can see the focus on the TextBox. Type your name in the Textbox and press the OK Button. I'm sure your Name will be assigned in the Label. This I tried based on my knowledge, and its a trick to bring focus and postback. If you find better way to acheive this I welcome you to post that article here. Thanks.

Wanna to try my Sample, click here.
 
Viewer's Comments
Posted by Dee on 6/24/2007 12:33:16 PM
doesn't pop-up in firefox
 
Posted by Moderator1 on 6/26/2007 12:58:42 AM
We have checked this in both FireFox 1.0 and FireFox 2.0.0.3. The modalpopup and postbacking is working perfectly. please let us know which version you are using.
 
Posted by samzon on 6/28/2007 1:56:20 PM
Very nice job. I have been pondering this problem the last couple of days and by acident I stumbled across this web site and may I say "YOUR MY HERO" :) . Also no problems using it in either IE or Firefox.
 
Posted by erwin on 7/1/2007 9:04:44 AM
i tried it in vs.net 2005, but panel is not popup(window doesn't modal window), i can still click hyperlink. the page is not disable
 
Posted by chronos on 7/2/2007 10:17:02 PM
hi, do you know a way to connect modal popup to double click event in gridview? I can open a modal window in ie when double click event triggered, but I haven't find a way to call modal popup extender. Can you help me to connect the double click event in gridview with modalpopup extender? since the window.showdialog doesn't run in firefox
 
Posted by Khurram on 7/4/2007 6:49:04 AM
Hi, Just tried this and it works like a charm. Only one problem tho.... On postback my button's Click event is firing twice. Anybody having this issue?? Please let me know as this is quite annoying because I am inserting into database in the click event and it is inserting twice! thanks.
 
Posted by Khurram on 7/4/2007 8:27:14 AM
Re: click event firing twice... Ok, this was happening because I had set the handle for my button's click event. Apparently there is no need to and removing this will eliminate the double postpack. thx!
 
Posted by seadog on 7/9/2007 4:06:18 PM
I second the post by samzon. I had to get this implemented today and this works perfectly. Thanks very much for posting this solution!
 
Posted by ajaxr on 7/12/2007 4:07:47 PM
Works great! Thanks for the post!!
 
Posted by Swati Shah on 7/14/2007 10:41:35 PM
Can i have your source code? I am using usercontrols within usercontrols and am unable to make this work.
 
Posted by **** on 7/19/2007 12:27:34 PM
actually i want to set focus after click event is fired. All the application based on master pages so when we are using TextBox1.Focus().then focus is not coming after firing the event . Please solve this problem .
 
Posted by pankaj on 7/19/2007 12:29:26 PM
actually i want to set focus after click event is fired. All the application based on master pages so when we are using TextBox1.Focus().then focus is not coming after firing the event . Please solve this problem
 
Posted by tskillback on 7/23/2007 9:32:07 AM
Works like a charm. Thanks!
 
Posted by Hans on 7/23/2007 12:35:50 PM
It works well. However, I need to display the popup when clicking an image button in a gridview cell. Because there isn't a unique control that can be set as the TargetControlID, I'm stumped as to how to implement the Modal Popup. Any suggestions?
 
Posted by JW on 7/27/2007 11:33:04 AM
Hans, Try setting the target id to a button with style="visibility:hidden" Then set the onclick of the buttons in your grid to codebehind that calls the .show method of the modalpopupextender. It's working for me.
 
Posted by Danish Ali on 7/28/2007 3:01:16 AM
Great article.. But iam having a small problem while applying it... the back ground color is not changin....... can any one guide me with this... thankx
 
Posted by Rykio on 8/10/2007 9:52:41 PM
Very nice! But if i need postback for several times in the same popup,it holds on all the time and just closes when i close it, what should i do? The efficiency is not very important to my project ,but the developing friendly. Thanks,and my email: torykio@msn.com
 
Posted by Ed on 8/31/2007 1:05:29 PM
I setup my code to do the postback as illustrated in your article. In my OK Click event method the code inserted a record into a database and then redirected to a new page. At the point of the redirect an error occurred in the Ajax code. I resolved the problem by calling the Dispose method on the ModalPopup control. Here's my code (validation code removed for brevity): Sub OK_Click(ByVal sender As Object, ByVal e As EventArgs) Dim ShowsObj As TDF_Shows Modal.Dispose()
 
Posted by bruce on 9/25/2007 3:57:08 PM
Excellent job. It worked perfectly for me.
 
Posted by Alex on 9/28/2007 10:20:43 AM
Excelent! Y added the javascript and the onClientClick event in my code behind and it works ok.
 
Posted by Chance on 10/1/2007 12:27:14 PM
What do I need to change to get it to work when calling modal popup in code beind?
 
Posted by Chance on 10/2/2007 9:06:11 AM
I'm not sure where to call the javascript from because no button is clicked.
 
Posted by Brent on 10/10/2007 10:30:07 AM
Very nice! Exactly what I needed.
 
Posted by Brent on 10/10/2007 10:30:43 AM
Very nice! Exactly what I needed.
 
Posted by Dvir on 10/17/2007 6:59:31 AM
Thank you I used your JS method to focus after the event and it worked perfectly at my local server. but somehow it would work on the site... have any idea?
 
Posted by hariharan on 10/30/2007 1:50:32 AM
Really thanks a lot... You were helped me in solving my problem. Its working fine
 
Posted by Renjith on 12/7/2007 1:47:39 PM
Man, just superb!
 
Posted by stanley on 12/11/2007 8:19:53 PM
Works like a charm with IE, but Firefox (Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11) Does not cause the postback to happen. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load btnSubmit.OnClientClick = String.Format("fnClickOK('{0}','{1}')", btnSubmit.UniqueID, "") End Sub