How to classify and Target “class of controls” as a set
-
14-07-2021 - |
سؤال
the scenario is like so :
say i have a table , html table , the table has few <td>
Controls each has a set of controls,
that is related to that specific <td>
, but not necessarily enclosed within the <td>
scope.
for example :
<asp:Label ID="Lbl_TlTp1" runat="server">
<table>
<tr>
<td ID="TD_1" runat="server">
<asp:Label ID="LBL_1" runat="server" />
</td>
<td>
<asp:TextBox ID="TBX_1" runat="server" />
</td>
</tr>
</table>
now i want to be able to address tips Label Lbl_TlTp1
, DataLabel LBL_1
And the TextBox TBX_1
as A set or a collection, to be very flexible (allowing Enumeration etc'..)
is it by making it(that "set" ) as a class or object that encapsulates those control items... each as a member ?
i would like to later be able to manipulate the "set" as one entity
setOfControls Sct1 = new setOfControls()
public void ActAppon(setOfControls section)
{
execProcedure1(section);
foreach(control ctr in section)
{
doTheThing(ctr);
}
}
...etc'
what is the approach to achieve this solution ?
المحلول
Make a UserControl (.ascx)
for the markup, and expose the properties for your web-controls, and work on that..
Make a UserControl (.ascx)
named as ControlGroup.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ControlGroup.ascx.cs" Inherits="NameSpaceName.ProjectName.ControlGroup" %>
<asp:Label ID="Lbl_TlTp1" runat="server">
<table>
<tr>
<td ID="TD_1" runat="server">
<asp:Label ID="LBL_1" runat="server" />
</td>
<td>
<asp:TextBox ID="TBX_1" runat="server" />
</td>
</tr>
</table>
In its code behind (ControlGroup.ascx.cs)
make Properties. like
namespace NameSpaceName.ProjectName
{
public partial class ControlGroup: System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{ }
public Label T1Tp1 { get { return Lbl_TlTp1; }}
public Label LBL { get { return Lbl_TlTp1; }}
public TextBox TBX{ get { return Lbl_TlTp1; }}
}
}
And Now you can use this UserControl In any ASPX Page. For it register your usercontrol on a page like
<%@ Page ....... %>
<%@ Register TagPrefix="uc1" TagName="CG" Src="ControlGroup.ascx" %>
And Use this Control in markup like
<uc1:CG runat="server" id="cg1" />
And In Page load of the page you can access this control and its property. like
protected void Page_Load(object sender, EventArgs e)
{
cg1.T1Tp1.Text = "Some Text";
cg1.LBL.Text = "Some Lable Text";
cg1.TBX.Text = "Some TextBox Text";
}
Or you can load your usercontrol programatic. like
List<ControlGroup> setOfControls = new List<ControlGroup>();
for(int i=0; i<10 ;i++)
{
ControlGroup cg1 = Page.LoadControl("~/ControlGroup.ascx") as ControlGroup;
if(cg1 != null)
{
cg1.T1Tp1.Text = "";
cg1.LBL.Text = "";
cg1.TBX.Text = "";
setOfControls.Add(cg1);
}
}
and can add it in another control, on even populate a list for these controls.
I hope It will help you.