Binding User Control Data on Button Click With Modal Popup

In this article, I will explain how to bind User Control data on a button click with a modal popup.

1. Create a user control. I created the UserInfo.ascx User Control in the given sample.

I added some controls to that User Control to bind the user information as given below:

  1. <table width="100%" border="0" cellspacing="0" style="background-color:white">  
  2.     <tr>  
  3.         <td>  
  4.             User Name:  
  5.         </td>  
  6.         <td>  
  7.             <asp:Label ID="UserNameLabel" runat="server"></asp:Label>  
  8.         </td>  
  9.     </tr>  
  10.     <tr>  
  11.         <td>Address1:</td>  
  12.         <td>  
  13.             <asp:Label ID="Address1Label" runat="server"></asp:Label>  
  14.         </td>  
  15.     </tr>  
  16.     <tr>  
  17.         <td>Address2:</td>  
  18.         <td>  
  19.             <asp:Label ID="Address2Label" runat="server">  
  20.             </asp:Label>  
  21.         </td>  
  22.     </tr>  
  23.     <tr>  
  24.         <td>  
  25.             City:  
  26.         </td>  
  27.         <td>  
  28.             <asp:Label ID="CityLabel" runat="server"></asp:Label>  
  29.         </td>  
  30.     </tr>  
  31.     <tr>  
  32.         <td>  
  33.             Country:  
  34.         </td>  
  35.         <td>  
  36.             <asp:Label ID="CountryLable" runat="server"></asp:Label>  
  37.         </td>  
  38.     </tr>  
  39.     <tr>  
  40.         <td colspan="2">  
  41. //For hide the modal popup  
  42.             <a href="#" id="Cancel" onclick="$find('ShowUserInfoModalPopUp').hide(); return false;">Cancel</a>  
  43.         </td>  
  44.    
  45.     </tr>  
  46. </table> 

2. In UserInfo.ascx.cs create a function BindUser(), to bind the user information.

  1. public void BindUser()  
  2. {  
  3.     UserNameLabel.Text = "Ish Bandhu";  
  4.     Address1Label.Text = "XYZ flat no 445";  
  5.     Address2Label.Text = "Street no 1";  
  6.     CityLabel.Text = "Noida";  
  7.     CountryLable.Text = "India";  
  8. }
3. Register that user control in your page.
  1. <%@ Register Src="~/UserInfo.ascx" TagName="UserInformation" TagPrefix="uc1" %>
4. Register the toolkit control in your page.
  1. <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>  
5. Define a CSS class to show the modal popup background as gray.
  1. <style type="text/css">  
  2.     .modalBackGround  
  3.  {  
  4.      background-colorgray;  
  5.      filter: alpha(opacity=50);  
  6.      opacity: 0.7;  
  7.   }  
  8. </style>
6. Add a script manager to your page:
  1. <asp:ScriptManager runat="server" ID="ScriptManager1">  
  2. </asp:ScriptManager>
7. Add a modalpopextender to your page:
  1. <asp:Button ID="TargetButton" runat="server" Style="display: none" />  
  2.         <ajaxToolkit:ModalPopupExtender ID="ShowUserInfoModalPopUp" runat="server" PopupControlID="UserInfoPanel" DropShadow="true"  
  3.             TargetControlID="TargetButton" BackgroundCssClass="modalBackGround" RepositionMode="RepositionOnWindowResizeAndScroll"  
  4. BehaviorID="ShowUserInfoModalPopUp">  
  5. </ajaxToolkit:ModalPopupExtender> 
// PopupControlID:This is panel name in which your user control is defined.

8. Add a panel and add the userinfo User Control to that panel:
  1. <asp:Panel ID="UserInfoPanel" runat="server" Style="display: none">  
  2.             <asp:UpdatePanel ID="UserInfoUpdatePanel" runat="server" UpdateMode="Conditional">  
  3.                 <ContentTemplate>  
  4.                     <uc1:UserInformation ID="UserInformationControl" runat="server" />  
  5.                 </ContentTemplate>  
  6.             </asp:UpdatePanel>        
  7. </asp:Panel>  

You can see that I added the userinfo User Control within the update panel, because after binding the user information we will call the update method of that update panel to update the user control information.

9. Add a button to show the user information in the modalpopup:

  1. <asp:Button ID="ShowUserInforButton" runat="server" OnClick="ShowUserInforButton_Click" Text="Show User Information" />
10. On the click event of the above mention button write the code:
  1. protected void ShowUserInforButton_Click(object sender, EventArgs e)  
  2. {  
  3.     UserInfo userInfo = (UserInfo)Page.FindControl("UserInformationControl");  
  4.     userInfo.BindUser();  
  5.     ShowUserInfoModalPopUp.Show();  
  6. }
Result.jpg


Similar Articles