web 2.0

Passing Parameter To User Control On A Modal Popup

This is the most demanded scenario for the one who are using Asp.net AJAX Control Toolkit, I mean I have seen most of the people asking for this feature on asp.net forum.

Unfortunately, we have no such functionality provided in Modal Popup because the control get rendered once the page is loaded and the Modal Popup is just a JavaScript which work is just to display a div which is hidden.

Here is a little work around for those who wants to pass parameter to user control using Modal Popup. Basically, the idea is to keep the user control in a separate page and call that page using JavaScript and put the response on the a Modal Popup Div.

Remember, as this is only a JavaScript we need to set the parameter using JavaScript or we need to save the parameters on Page_Load in any Hidden field and access that later from JavaScript.

To start, I have created two pages and a user control following is the naming stuff for them

  1. Default.aspx (Contains Modal Popup)
  2. ControlCaller.aspx  (Contains User Control)
  3. Controls.ascx (User Control)

Default.aspx page html will look like as follows

   1: <form id="form1" runat="server">
   2:     <div>
   3:     
   4:         <asp:ScriptManager ID="ScriptManager1" runat="server">
   5:         </asp:ScriptManager>
   6:         <asp:Button ID="Button1" runat="server" Text="Show Popup" OnClientClick="setupParam()" />
   7:     
   8:     </div>
   9:     <cc1:ModalPopupExtender ID="ModalPopupExtender1" runat="server" PopupControlID="pnlControl" TargetControlID="Button1">
  10:     </cc1:ModalPopupExtender>
  11:     <asp:Panel ID="pnlControl" runat="server">
  12:             
  13:     </asp:Panel>
  14:     
  15:     <asp:HiddenField ID="hf_username" runat="server" />
  16:     <asp:HiddenField ID="hf_password" runat="server" />
  17:     <asp:HiddenField ID="hf_registredDate" runat="server" />
  18:     
  19:     </form>

Keep in mind that the three hidden fields I have taken here are for the Parameters.

Now Let's see what we have in ControlCaller.aspx

   1: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ControlCaller.aspx.cs" Inherits="LearnWebApp.ControlCaller" %>
   2: <%@ Register src="Controls/Control.ascx" tagname="Control" tagprefix="uc1" %>
   3:     <uc1:Control ID="Control1" runat="server" />

Just user control implementation and nothing else. Please make sure to remove all the head, html and form tags.

Following is the html of Control.ascx

   1: <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Control.ascx.cs" Inherits="LearnWebApp.Controls.Control" %>
   2: User Name :
   3: <asp:Label ID="lblUserID" runat="server" Text="Label"></asp:Label>
   4: <br />
   5: Registered Date :
   6: <asp:Label ID="lblDate" runat="server" Text="Label"></asp:Label>
   7: <br />
   8: Password:
   9: <asp:Label ID="lblPassword" runat="server" Text="Label"></asp:Label>

And here is the code behind of the Control

   1: private string _userName;
   2:         private string _RegisteredDate;
   3:         private string _Password;
   4:  
   5:         public string userName
   6:         {
   7:             get { return _userName; }
   8:             set { _userName = value; }
   9:         }
  10:         public string RegisteredDate
  11:         {
  12:             get { return _RegisteredDate; }
  13:             set { _RegisteredDate = value; }
  14:         }
  15:         public string Password
  16:         {
  17:             get { return _Password; }
  18:             set { _Password = value; }
  19:         }
  20:         protected void Page_Load(object sender, EventArgs e)
  21:         {
  22:             if (!Page.IsPostBack) 
  23:             {
  24:                 lblDate.Text = _RegisteredDate;
  25:                 lblPassword.Text = _Password;
  26:                 lblUserID.Text = _userName;
  27:             }
  28:         }

Please bear in mind, that the properties we have taken here are for the parameters as you can see I am also setting these properties on label at Page_Load.

and here we have the script which we need to add on the default.aspx head section

   1: <script language="javascript">
   2:     var request = false;
   3:        try {
   4:          request = new XMLHttpRequest();
   5:        } catch (trymicrosoft) {
   6:          try {
   7:            request = new ActiveXObject("Msxml2.XMLHTTP");
   8:          } catch (othermicrosoft) {
   9:            try {
  10:              request = new ActiveXObject("Microsoft.XMLHTTP");
  11:            } catch (failed) {
  12:              request = false;
  13:            }  
  14:          }
  15:        }
  16:        
  17:       function GetResult() {
  18:         var divComm = document.getElementById('pnlControl');
  19:         divComm.innerHTML = "Please wait processing your request!!!";
  20:         var rnd = Math.random() * 1000000;
  21:         var url = 'ControlCaller.aspx?userName=' +document.getElementById("hf_username").value + "&password=" + document.getElementById("hf_password").value  + "&Date="+ document.getElementById("hf_registredDate").value  +'&rnd=' + rnd;
  22:         request.open("GET", url, true);
  23:         request.onreadystatechange = GetResultComplete;
  24:         request.send(null);
  25:     }
  26:     function GetResultComplete() {
  27:         if (request.readyState == 4) {
  28:             //alert(request.responseText);
  29:             if (request.status == 200) {
  30:                 var divComm = document.getElementById('pnlControl');
  31:                 if (divComm) {
  32:                     divComm.innerHTML = request.responseText;
  33:                 }
  34:             }
  35:         }
  36:     }
  37:     function setupParam()
  38:         {
  39:             document.getElementById("hf_username").value = "User is for test !!!";
  40:             document.getElementById("hf_password").value  = "testing.....";
  41:             document.getElementById("hf_registredDate").value  = "10/04/84";
  42:             GetResult();
  43:             
  44:         }
  45:     </script>

Notice the setupParam() function, we are setting our parameter here and then call the GetResult function which is making an AJAX call to the page we have created. That's it by using this method we can have parameterized user control inside Modal Popup.

You can download the full Visual Studio 2008 Project from here.

Comments

Ernesto , on 2/13/2009 1:04:16 PM Said:

Ernesto

Hi.

how about setting a public property in the user control. And if you need to notify something to the calling page you can raise an event. It's just an idea and of course you would need to open the modal from code behind so you have the time to set the usercontrols properties.

PHP Assignment Russia, on 7/6/2009 8:55:18 AM Said:

PHP Assignment

Great, thanks!

how to attract women United States, on 7/7/2009 7:34:02 AM Said:

how to attract women

this sounds ridiculous, i may take a look further to check out the detail
anyway, thanks

sheartech United States, on 7/12/2009 11:45:47 PM Said:

sheartech

keep providing solutions to our problems.its very useful.

gioca a poker gratis United States, on 7/22/2009 10:00:17 AM Said:

gioca a poker gratis

Awesome, this was a really quality post. In theory I'd like to write like this too - taking time and real effort to make a good article... but what can I say... I procrastinate alot and never seem to get something done.

How to Fix Blue Screen United States, on 8/1/2009 8:32:31 AM Said:

How to Fix Blue Screen

Hi,
I was looking for that solution nearly all day.Can i copy this article into my blog?If you  are agree,we can exchange useful articles in the future?

Best Regards,
Chris

how to get your ex girlfriend back United States, on 8/1/2009 8:33:18 AM Said:

how to get your ex girlfriend back

Hi,
I was looking for that solution nearly all day.Can i copy this article into my blog?If you  are agree,we can exchange useful articles in the future?

Best Regards,
Chris

how to get your ex girlfriend back United States, on 8/1/2009 8:33:25 AM Said:

how to get your ex girlfriend back

Hi,
I was looking for that solution nearly all day.Can i copy this article into my blog?If you  are agree,we can exchange useful articles in the future?

Best Regards,
Chris

Commercial Juicers United States, on 8/18/2009 5:17:47 AM Said:

Commercial Juicers

Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It's always nice when you can not only be informed, but also entertained! I'm sure you had fun writing this article.

Guaranteed Fast Cash Loan United States, on 8/19/2009 6:24:34 AM Said:

Guaranteed Fast Cash Loan

Hi webmaster, commenters and everybody else !!! The blog was absolutely fantastic! Lots of great information and inspiration, both of which we all need!b Keep 'em coming... you all do such a great job at such Concepts... can't tell you how much I, for one appreciate all you do!

Contextual links United States, on 8/23/2009 6:48:01 AM Said:

Contextual links

Wow really a great post. I like this.I just passed this onto a colleague who was doing a little research on that. And he actually bought me lunch because I found it for him smile So let me rephrase that .Lots of great information and inspiration, both of which we all need!

Song Lyrics United States, on 8/23/2009 8:42:57 PM Said:

Song Lyrics

Keep 'em coming... you all do such a great job at such Concepts... can't tell you how much I, for one appreciate all you do!

LimoGrabber United States, on 8/28/2009 9:31:30 AM Said:

LimoGrabber

Thank you so much. I tried many ways but did not work. This is great.....

Linda Mirano United States, on 8/28/2009 12:48:28 PM Said:

Linda Mirano

Hi webmaster, commenters and everybody else !!! The blog was absolutely fantastic! Lots of great information and inspiration, both of which we all need!b Keep 'em coming... you all do such a great job at such Concepts... can't tell you how much I, for one appreciate all you do!

Link building services Tajikistan, on 8/28/2009 7:40:07 PM Said:

Link building services

hi mate thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic.I was looking for this data for a long time, bit I was not able to find the trusted source you all do such a great job at such Concepts... can't tell you how much I, for one appreciate all you do!thanks for you sharing.
regards,

Acai Berry Diet United States, on 9/4/2009 1:53:39 AM Said:

Acai Berry Diet

Pretty Interesting post. Couldnt be written any better. Reading this post reminds me of my old room mate! He always kept talking about this. I will forward this post to him. Pretty sure he will have a good read. Thanks for sharing!

Scooby Games United States, on 9/9/2009 6:42:20 AM Said:

Scooby Games

This is easier and surely gives comfort to internet users. Thanks for sharing. Post like this offers great benefit. Thank you!

Tom Ford Eyeglasses United States, on 9/12/2009 11:30:57 PM Said:

Tom Ford Eyeglasses

Nicely presented information in this post, I prefer to read this kind of stuff. The quality of content is fine and the conclusion is good. Thanks for the post.

Reverse Phone Lookup United States, on 9/15/2009 5:08:28 AM Said:

Reverse Phone Lookup

Never seen such cool post. I read it all the way to the end. Keep them coming.

product reviews United States, on 9/16/2009 3:53:29 AM Said:

product reviews

Thats an interesting post. It was worth visiting your blog. Hope to visit again.

cheap xbox 360 United States, on 9/16/2009 4:43:22 PM Said:

cheap xbox 360

Just wanted to give you a shout from the valley of the sun, great information.  I will forward this post to him. Pretty sure he will have a good read. Thanks for sharing!

London Escorts United Kingdom, on 9/17/2009 3:37:42 AM Said:

London Escorts

Its great to know about new tricks in ajax thank you

Presentation Skills United States, on 9/18/2009 1:28:19 AM Said:

Presentation Skills

Nice to be visiting your blog again, it has been months for me. Well this article that i've been waited for so long. I need this article to complete my assignment in the college, and it has same topic with your article. Thanks, great share.

Registry Easy United States, on 9/19/2009 6:36:52 AM Said:

Registry Easy

Looks like an interesting blog. Will make visit again.

Speed up windows xp United States, on 9/20/2009 4:55:52 AM Said:

Speed up windows xp

Loved reading this post.

Killer review United States, on 9/23/2009 2:00:22 AM Said:

Killer review

Your posts are always great. Keep them coming.

cocktail dresses United States, on 9/30/2009 3:15:00 AM Said:

cocktail dresses

Thanks again and good luck!

mortgages for contractors United States, on 9/30/2009 4:59:49 PM Said:

mortgages for contractors

You got a really useful blog I have been here reading for about an hour. I am a newbie and your success is very much an inspiration for me.

leopard print fur coat United States, on 10/1/2009 5:44:26 PM Said:

leopard print fur coat

That is some inspirational stuff. Never knew that opinions could be this varied. Thanks for all the enthusiasm to offer such helpful information here.

electronic cigarette United States, on 10/1/2009 9:57:49 PM Said:

electronic cigarette

I truly enjoy returning back to this site. You always seem to have interesting articles to read. Thanks so much. Have you bookmarked now!

women's sandals United States, on 10/3/2009 10:13:18 AM Said:

women's sandals

changing the name of the Modal popup page would cause errors in loading? Ive been consistently getting errors by doing this... any help?

Guaranteed Loans United States, on 10/3/2009 8:30:27 PM Said:

Guaranteed Loans

That is some inspirational stuff. Never knew that opinions could be this varied. Thanks for all the enthusiasm to offer such helpful information here.

Fat Loss 4 Idiots Review United States, on 10/5/2009 2:36:56 PM Said:

Fat Loss 4 Idiots Review

Hi blog admin and all commentators. I wish you MERRY CHRISTMAS. Have a joyful holiday Smile

Niche Blueprint 2.0 Review United States, on 10/7/2009 11:23:24 PM Said:

Niche Blueprint 2.0 Review

I like your blog so much that I feel I have to wish you. Happy New Year in advance. Have a nice and prosperous year ahead

membership site software United States, on 10/8/2009 1:12:21 PM Said:

membership site software

You're right, when there's no hope, there's no success. You've got a great blog. Keep it hot! Thanks a lot!

Digital Signage Saskatoon United States, on 10/8/2009 2:02:32 PM Said:

Digital Signage Saskatoon

Very interesting... I always enjoy the topics you choose on your site. Got you saved in my favorites now.

Digital Signage United States, on 10/8/2009 2:13:17 PM Said:

Digital Signage

Thanks for the great info you always seem have on your site. They are always interesting.

Drug Testing United States, on 10/8/2009 10:42:55 PM Said:

Drug Testing

I love returning back to this site. You always have great interesting stuff to read. keep it up. Will be returning, thats for sure.

Digital Signage Toronto United States, on 10/10/2009 2:10:57 PM Said:

Digital Signage Toronto

Your posts are always so interesting and informative. Always do enjoy returning back to read more. I keep your site bookmarked now. Thanks!

golf cart accessories United States, on 10/11/2009 3:48:07 AM Said:

golf cart accessories

Great post aghausman, haven't you thought of setting up a RSS feed for ppl to keep in touch with your postings?? Keep it up!!

cord blood banks United States, on 10/11/2009 4:56:12 AM Said:

cord blood banks

what a fantastic postaghausman!! Keep it coming, I'll continue sharing these with my mates

online poker strategy United States, on 10/11/2009 9:48:17 AM Said:

online poker strategy

Which is the best and cheap nvidia graphic card to be purchased which can play all games?

Digital Signage Vancouver United States, on 10/11/2009 8:53:46 PM Said:

Digital Signage Vancouver

Thanks so much for the great content you always seem to provide on your site. As always ..a very interesting article.

E Cigarette Canada, on 10/12/2009 12:06:02 AM Said:

E Cigarette

thanks, i was trying to figure this out forever.

Niche Blueprint 2.0 Review United States, on 10/15/2009 9:44:37 PM Said:

Niche Blueprint 2.0 Review

You write very interestingly. I think Google is becoming very smart. It can sense which website has interesting posts Smile

Niche Blueprint Review United States, on 10/18/2009 9:59:31 PM Said:

Niche Blueprint Review

How is the new year going? I hope to read more interesting posts like last year

Niche Blueprint United States, on 10/23/2009 5:42:39 AM Said:

Niche Blueprint

You write very well. Kept me really engaged for some time Smile

poker stars United States, on 10/24/2009 4:34:27 PM Said:

poker stars

Video games are certainly cool, the graphics are spectacular and the story lines can be engaging, and boys really seem to love them. While a little video game playing isn't harmful there is real danger lurking in these machines. Have you ever wondered about the potential harm of video games or even had concerns about a possible addiction? There is growing evidence that while there can be some benefits of video games, there is some real dangers.

building inspection sydney United States, on 10/24/2009 6:40:02 PM Said:

building inspection sydney

If you are a regular sufferer of Urinary Tract Infections, you may well have sought treatments and cures you could try at home, cutting out an expensive and time-consuming visit to the doctors. When the infection is mild, some of the more common UTI home remedies can be particularly effective at providing relief. However, a more persistent infection may require a different type of cure which is guaranteed to work.

boat charters India, on 10/28/2009 1:52:36 AM Said:

boat charters


28.   Interesting read, thanks for helping keep me busy at work ;)

holiday cottages cornwall United States, on 10/28/2009 7:14:30 PM Said:

holiday cottages cornwall

Self catering holiday cottages in Cornwall England featuring Late Availability discounts and Short Breaks.  Select from the best of UK Holiday Cottages in Cornwall. England

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading