SIGN UP MEMBER LOGIN:    
ARTICLE

Set Web User Control Properties in Design Time

Posted by Ashish Singhal Articles | How do I January 27, 2006
This article will show that how can we initialize properties of web user control when we register it on some web page.
Reader Level:

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
       
}
}

Login to add your contents and source code to this article
share this article :
post comment
 

Can you please help me in understanding this one http://www.codeproject.com/KB/webforms/costom_gridview_controls.aspx

Posted by Mayur Gujrathi Nov 29, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor