Blue Theme Orange Theme Green Theme Red Theme
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » ASP.NET » Set Web User Control Properties in Design Time

Set Web User Control Properties in Design Time

This article will show that how can we initialize properties of web user control when we register it on some web page.

Author Rank :
Page Views : 14347
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

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

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Ashish Singhal
Ashish has been working as a software engineer since 2002. He has written articles for C# Corner . He specializes in the implementation of client/server, database, graphics and/or Internet-based systems using Visual Studio .NET suite. His area of expertise include: C#, ADO.NET, GDI+, Windows Forms, Web Services, Tablet PC and ASP.NET.Ashish's background includes Master's in Computer Science.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
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.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
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!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments

 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.