MasterPage, UserControl, ModalPopup And UpdatePanel - Passing Values To Parent Page

A developer in the forums is asking how to pass data that is coming from a UserControl control to the main page. Basically, the scenario is that he has a user control which contains a GridView which is wrapped around within an ASP.NET AJAX UpdatePanel control. The UserControl will then be used in the page that is hosted within a master page. The page contains some TextBox in which it will be populated once the user selects a row from the GridView, also the ModalPopup is defined within that page.

The question is quite interesting, and so I’ve given it a shot and created a simple demo. Here’s the solution that I came up with, and provided it to the developer.

The Page Markup

  1. <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Modal.aspx.cs" Inherits="WebAppDemo.Modal" %>  
  2.   
  3. <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>  
  4. <%@ Register src="UserControl/WebUserControl1.ascx" tagname="WebUserControl1" tagprefix="uc1" %>  
  5.   
  6. <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">  
  7.     <style type="text/css">  
  8.         .modal-bg{  
  9.             background-color:Gray;  
  10.             filter:alpha(opacity=50);  
  11.             opacity:0.6;  
  12.             z-index:999;  
  13.         }  
  14.         .modal{  
  15.             position:absolute;  
  16.         }  
  17.     </style>  
  18.   
  19. </asp:Content>  
  20. <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">  
  21.     <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">  
  22.     </asp:ToolkitScriptManager>  
  23.   
  24.     <asp:TextBox ID="txtProductName" runat="server" />  
  25.     <asp:TextBox ID="txtPrice" runat="server" />  
  26.   
  27.   
  28.     <asp:LinkButton ID="LinkButton1" runat="server" Text="Show" />  
  29.     <asp:Panel ID="pnlPopUp" runat="server" style="display:none" CssClass="modal" >  
  30.                     <uc1:WebUserControl1 ID="WebUserControl11" runat="server" />  
  31.     </asp:Panel>  
  32.     <asp:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="LinkButton1" PopupControlID="pnlPopUp" BackgroundCssClass="modal-bg">  
  33.     </asp:ModalPopupExtender>  
  34.   
  35. </asp:Content>  
There’s nothing really fancy about the markup above. It just contains two TextBoxes, a ModalPopup, a LinkButton as the target control for the Modal, and a Panel control that contains a UserControl.

You may also have noticed that I’ve created a simple CSS for the modal.

UserControl Markup
  1. <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebAppDemo.UserControl.WebUserControl1" %>  
  2. <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>  
  3.   
  4. <asp:UpdatePanel ID="UpdatePanel1" runat="server">  
  5.    <ContentTemplate>  
  6.          <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">  
  7.                     <Columns>  
  8.                          <asp:BoundField DataField="ID" />  
  9.                          <asp:BoundField DataField="ProductName" HeaderText="Product Name" />  
  10.                          <asp:TemplateField>  
  11.                             <ItemTemplate>  
  12.                                 <asp:TextBox ID="txtPrice" runat="server" Text='<%# Eval("Price") %>' />  
  13.                             </ItemTemplate>  
  14.                          </asp:TemplateField>  
  15.                          <asp:TemplateField>  
  16.                             <ItemTemplate>  
  17.                                 <asp:TextBox ID="txtAmount" runat="server" Text='<%# Eval("Amount") %>' />  
  18.                             </ItemTemplate>  
  19.                          </asp:TemplateField>  
  20.                          <asp:TemplateField>  
  21.                             <ItemTemplate>  
  22.                                 <asp:Button ID="Button1" runat="server" Text="Select" onclick="Button1_Click" />  
  23.                             </ItemTemplate>  
  24.                          </asp:TemplateField>  
  25.                     </Columns>  
  26.         </asp:GridView>  
  27.    </ContentTemplate>  
  28.    <Triggers>  
  29.         <asp:PostBackTrigger ControlID="GridView1" />  
  30.    </Triggers>  
  31. </asp:UpdatePanel>  
The UserControl is where we define the GridView. Noticed that the GridView is wrapped within an UpdatePanel control? We then set the GridView as the PostbackTrgiggerControlID to trigger the control events within GridView – in this case, the Button control.

CODE BEHIND

Now, here’s the code for the UserControl.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using AjaxControlToolkit;  
  9.   
  10. namespace WebAppDemo.UserControl  
  11. {  
  12.     public partial class WebUserControl1 : System.Web.UI.UserControl  
  13.     {  
  14.   
  15.         private DataTable GetSampleData()  
  16.         {  
  17.   
  18.             //NOTE: THIS IS JUST FOR DEMO  
  19.             //If you are working with database  
  20.             //You can query your actual data and fill it to the DataTable  
  21.             DataTable dt = new DataTable();  
  22.             DataRow dr = null;  
  23.   
  24.             //Create DataTable columns  
  25.             dt.Columns.Add(new DataColumn("ID"typeof(string)));  
  26.             dt.Columns.Add(new DataColumn("ProductName"typeof(string)));  
  27.             dt.Columns.Add(new DataColumn("Price"typeof(string)));  
  28.             dt.Columns.Add(new DataColumn("Amount"typeof(string)));  
  29.   
  30.   
  31.             //Create Row for each columns  
  32.             dr = dt.NewRow();  
  33.             dr["ID"] = 1;  
  34.             dr["ProductName"] = "Galaxy S";  
  35.             dr["Price"] = "100";  
  36.             dr["Amount"] = "10";  
  37.   
  38.             dt.Rows.Add(dr);  
  39.   
  40.             dr = dt.NewRow();  
  41.             dr["ID"] = 2;  
  42.             dr["ProductName"] = "iPhone 4";  
  43.             dr["Price"] = "200";  
  44.             dr["Amount"] = "2";  
  45.   
  46.             dt.Rows.Add(dr);  
  47.   
  48.             dr = dt.NewRow();  
  49.             dr["ID"] = 3;  
  50.             dr["ProductName"] = "HTC Mobile";  
  51.             dr["Price"] = "50";  
  52.             dr["Amount"] = "10";  
  53.   
  54.             dt.Rows.Add(dr);  
  55.             return dt;  
  56.         }  
  57.   
  58.         private void BindGrid(DataTable source)  
  59.         {  
  60.             GridView1.DataSource = source;  
  61.             GridView1.DataBind();  
  62.         }  
  63.         protected void Page_Load(object sender, EventArgs e)  
  64.         {  
  65.             if (!IsPostBack)  
  66.                 BindGrid(GetSampleData());  
  67.         }  
  68.   
  69.         protected void Button1_Click(object sender, EventArgs e)  
  70.         {  
  71.             Button b = (Button)sender;  
  72.             GridViewRow gvRow = (GridViewRow)b.NamingContainer;  
  73.   
  74.             string productName = gvRow.Cells[1].Text;  
  75.             string price = ((TextBox)gvRow.FindControl("txtPrice")).Text;  
  76.   
  77.             Type thePage = Page.GetType();  
  78.             System.Reflection.PropertyInfo[] pi = thePage.GetProperties();  
  79.             foreach (System.Reflection.PropertyInfo pinfo in pi)  
  80.             {  
  81.                 if (pinfo.Name == "ProductName")  
  82.                 {  
  83.                     pinfo.SetValue(Page, productName, null);  
  84.                 }  
  85.                 else if (pinfo.Name == "Price")  
  86.                 {  
  87.                     pinfo.SetValue(Page, price, null);  
  88.                 }  
  89.             }  
  90.   
  91.             ((ModalPopupExtender)this.Parent.Parent.FindControl("ModalPopupExtender1")).Hide();   
  92.               
  93.         }  
  94.     }  
  95. }  
You may have noticed that I just used a fake data as the data source for GridView. At the Button’ s Click event, I have used reflection to access the property metadata. I then used the SetValue to populate the controls in the main page based on the selected row values from the GridView. Keep in mind that you can also use the FindControl() method to reference a control and assign a value, but I tend to use reflection in this particular demo because I don’t want to deal with naming containers to find specific controls.

You can also use properties if you want to get the value from a UserControl in the main page. For example, you expose a public property within your UserControl. Then in your main page, you simply reference the property defined in your UserControl. For example, at Button’s click event. Though using properties isn’t applicable for this particular scenario, since we are passing data to the main page and not getting data.

Output

Running the page will display something like this,

Output

After clicking the “Show” link,

Show

After selecting a row from GridView,

GridView
That’s it! I hope someone finds this post useful.

 

Next Recommended Readings