While preparing for the interview, prior to joining my current employer, I came across the term Reflection. Questions on Reflection had been asked many a times. It was high time to learn the concept of Reflection. In turn it helped me to solve my first assignment with current employer, Trigyn Technlogies Limited. I was able to solve the problem only because I knew that there is something called reflection which could help me to solve such kind of problems.
Now here was the requirement
- Needed to have a user control for Grid View
- To Bind data to above grid view, two parameters were require – Year, Department
WebUserControl1.ascx
<%@
Control
Language="C#"
AutoEventWireup="true"
CodeBehind="WebUserControl1.ascx.cs"
Inherits="ReflectionSample.WebUserControl1"
%>
<asp:GridView
ID="GridView1"
runat="server">
</asp:GrifView>
WebUserControl1.ascx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace ReflectionSample
{
public
partial
class
WebUserControl1 : System.Web.UI.UserControl
{
protected
void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = GetData(2008, "Support");
}
private
DataSet GetData(int year, string department)
{
throw
new
NotImplementedException();
}
}
}
Default.aspx
<div
id="placeUserControl"
runat="server">
</div>
Challenges
- User Control to be loaded dynamically into the .aspx page.
- Year, Department parameters' value were coming from the .aspx page (outside the user control). Into the above code snippet its hard coded.
Solution
- Added two properties for Year and Department into User control's code behind file.
- The above properties' value had been used for GetData method. User control's code behind file would look like below.
WebUserControl1.ascx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace ReflectionSample
{
public
partial
class
WebUserControl1 : System.Web.UI.UserControl
{
public
int Year { get; set; }
public
string Department { get; set; }
protected
void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = GetData(Year, Department);
}
private
DataSet GetData(int year, string department)
{
throw
new
NotImplementedException();
}
}
}
- Loaded user control (run time) into .aspx page.
- Code behind file of .aspx page would look like below
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
namespace ReflectionSample
{
public
partial
class
_Default : System.Web.UI.Page
{
protected
void Page_Load(object sender, EventArgs e)
{
int Year = 2008;
string Department = "Support";
UserControl userControl = Page.LoadControl("WebUserControl1.ascx") as
UserControl;
Type userControlType = userControl.GetType();
PropertyInfo propertyYear = userControlType.GetProperty("Year");
PropertyInfo propertyDepartment = userControlType.GetProperty("Department");
propertyYear.SetValue(userControl, Year, null);
propertyDepartment.SetValue(userControl, Department, null);
placeUserControl.Controls.Clear();
placeUserControl.Controls.Add(userControl);
}
}
}
Your comments, suggestions, alternate solutions are welcome J