Set Web User Control Properties in Design Time

This article will show the initilization of user control properties at design time.

Use your style sheet to set styles of your control. It is being added dynamic so you can provide class names at design time when you register control.[see below the form tag].

Contol Registration on any .aspx page:

<%@ Register TagPrefix="uc2" TagName="BNews" src="USERCONTROL.ascx" %>

Inside form tag :

<table width="100%">
  <tr>
  
<td width = 2%></td>
  
<td>
 
<uc2:BNews id="BNews1" caption="Editor Message" extensionlnktext="More..." 
  
extensionlnkstyle="normal" contentstyle="ContentTableStyle"
  
imagestyle="ImageStyle" runat="server" ImageTop = true ImageAlign = left LnkAlign = right>
 
</uc2:BNews></td>
 
<td width = 40%></td>
 
</tr>
</table>

you can see above lines that we are initilizing control properties when we are adding control on web page.

USERCONTROL.ASCX :

<%@ Control Language="c#" AutoEventWireup="false" Codebehind="BNews.ascx.cs" Inherits="BreakingNewsCtl.BNews" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<TABLE id="tblBreakingNewsHeader" cellSpacing="0" width="100%" class="OuterTableStyle" cellpadding="5">
    
<TR>
          <TD id="headerMidTd" runat="server">
               
<asp:Label id="lblCaption" runat="server"></asp:Label>
          </
TD>
     </TR>
</
TABLE>
<
TABLE id="contentTable" runat="server">
      <TR>
           <TD colSpan="2" class="paddingFive">
                  <table id="UpperImageTable" runat="server">
                       <tr>
                            
<td><IMG id="img" name="img" runat="server">
                            </
td>
                      
</tr>
                 
</table>
                  <div id="description" runat="server">
                 
</div>
          
</TD>
      
</TR>
       
<TR>
           
<TD colspan="2" class="paddingFive" id="lnkContainer" runat="server">
                
<a id="link" runat="server" target="_blank"></a>
           </TD>
      
</TR>
</
TABLE>

USERCONTROL.ASCX.CS :

namespace BreakingNewsCtl
{
      using System;
      using System.Drawing;
      using System.Web;
      using System.Web.UI.WebControls;
      using System.Web.UI.HtmlControls;
      public class BNews : System.Web.UI.UserControl
      {
              protected System.Web.UI.HtmlControls.HtmlImage img;
              protected System.Web.UI.HtmlControls.HtmlGenericControl description;
              protected System.Web.UI.HtmlControls.HtmlAnchor link;
              protected System.Web.UI.WebControls.Label lblCaption;
              protected System.Web.UI.HtmlControls.HtmlTableCell headerMidTd;
              protected System.Web.UI.HtmlControls.HtmlTable contentTable;
              private string caption = string.Empty;
              private string headerMid = string.Empty;
              private string tableBorder = string.Empty;
              private string imageHeight = string.Empty;
              private string imageStyle = string.Empty;
              private string imageURL = string.Empty;
              private string extensionLnkText = string.Empty;
              private string extensionLnkStyle = string.Empty;
              private bool imageTop = true;
              protected System.Web.UI.HtmlControls.HtmlTable UpperImageTable;
              protected System.Web.UI.HtmlControls.HtmlTable BottomImageTable;
              private string imageAlign = string.Empty;
              protected System.Web.UI.HtmlControls.HtmlTableCell lnkContainer;
              private string lnkAlign = string.Empty;
              private string contentDescription = string.Empty;
              private string linkURL = string.Empty;
              private string imagePath;
                 // This Load will bind the property values to control which you provided.
             
private void Page_Load(object sender, System.EventArgs e)
              {
                  if(!IsPostBack)
                  {
                       //This code says that how can we set style sheet class runtime ?
                      
headerMidTd.Attributes.Add("Class", HeaderMidStyle);
                       contentTable.Attributes.Add("Class", ContentStyle); 
                       img.Attributes.Add("Class", ImageStyle);
                       if(ImageAlign == "")
                       ImageAlign = "Right";
                       if(LnkAlign == "")
                       LnkAlign = "Right";
                       lnkContainer.Attributes.Add("Align", LnkAlign);
                       UpperImageTable.Attributes.Add("Align", ImageAlign);
                       if(ImageTop)
                       UpperImageTable.Visible = true
                       else
                       
{
                           UpperImageTable.Visible = false;
                           BottomImageTable.Visible = true;
                       }
                       link.InnerText = ExtensionLnkText; 
                       link.Visible = false;
                       lblCaption.Text = Caption;
                       img.Src = ImageURL;
                       if(Description == "")
                       description.InnerText = announceMentTable.Rows[0]["description"].ToString();
                       else
                       
description.InnerText = Description;
                       if(LnkURL == "")
                       link.HRef = ConfigurationSettings.AppSettings["NewsRootPath"];
                       else
                      
link.HRef = LnkURL;
                  }
              }
              public string Caption
              {
                   get{ return caption;}
                   set{ caption = value;}
              }
              public string HeaderLeftStyle
              {
                   get{ return headerLeft;}
                   set{ headerLeft = value;}
              }
              public string HeaderMidStyle
              {
                   get{ return headerMid;}
                   set{ headerMid = value;}
              }
              public string HeaderRightStyle
              {
                   get{ return headerRight;}
                   set{ headerRight = value;}
              }
              public string ContentStyle
              {
                   get{ return tableBorder;}
                   set{ tableBorder = value;}
              }
              public string ImageStyle
              {
                   get{ return imageStyle;}
                   set{ imageStyle = value;}
              }
              public string ImageURL
              {
                   get{ return imageURL;}
                   set{ imageURL = value;}
              }
              public string ExtensionLnkText
              {
                   get{ return extensionLnkText;}
                   set{ extensionLnkText = value;}
              }
              public string ExtensionLnkStyle
              {
                    get{ return extensionLnkStyle;}
                    set{ extensionLnkStyle = value;}
              }
              public bool ImageTop
              {
                    get{return imageTop;}
                    set{imageTop = value;}
              }
              public string ImageAlign
              {
                    get{return imageAlign;}
                    set{imageAlign = value;}
              }
              public string LnkAlign
              {
                    get{return lnkAlign;}
                    set{lnkAlign = value;}
              }
              public string Description
              {
                    get{return contentDescription;}
                    set{contentDescription = value;}
              }
              public string LnkURL
              {
                    get{return linkURL;}
                    set{linkURL = value;}
              }
              #region Web Form Designer generated code
              override protected void OnInit(EventArgs e)
              {
                  InitializeComponent();
                  base.OnInit(e);
              }
              private void InitializeComponent()
              {
                   this.Load += new System.EventHandler(this.Page_Load);
              }
              #endregion
       
}
}


Similar Articles