Search   Articles   Dev Forums   Personalize   Favorites   Member Login   ASP.Net Hosting
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Sunday, February 19, 2006

Search Directory
ESET
ASP.NET
VB.Net
C-Sharp
SQL Server
 
More Partners >>

Creating User Controls without Interface

Author: Faraz
Download Source Code : 721_SampleCode.zip

In this sample article I will try to explain you how you can create user controls in ASP.Net without any user control interface.

This sample article will elaborate you how you can create the user controls only containing the server functions. This sometimes help you in creating web controls which has limited functionalities means you don’t want to add the any server controls or you want to access the properties by using getter setter.

 

To begin with create a solution in VS 2005 and add the user control in your solution. Note you must uncheck the option of adding the .vb file (code behind file)

 

The final code of your user control will be look like as follows:

 

<%@ Control Language="VB" ClassName="userCtrl" %>

 

<script type="text/VB"  runat="server">

 

    Public ReadOnly Property ReturnPi() As Double

        Get

            ReturnPi = Math.PI

        End Get

    End Property

   

    Public Function RoundPI(ByVal DecimalPlaces As Integer) As Double

        RoundPI = Math.Round(Math.PI, DecimalPlaces)

    End Function

   

 

</script>

Here you will see I have defined one function and one property with only getter that gets the PI value and the function that returns the PI value by rounding it to up to three decimal places.  

On your .aspx page you need to register the user control using register tag:

<%@ Register Src="~/userCtrl.ascx" TagName="uCtrl" TagPrefix="uc"  %>

 

On form add the user control and two label control which is used to display the value on web form so the control on your aspx form will be look like as follows:

 

  <uc:uCtrl ID="myUserCtrl" runat="server" />

  <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

  <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>

 

The only thing left is call your user control methods using the user control id and this will displays the PI value on your web form like:

 

     Label2.Text = myUserCtrl.RoundPi


Article Comments
Name:  guest 
note 1 note 1 note 1 note 1 note 1 note 1 note 1 note 1 note 1 note 1 note 1 note 1 note 1 note 1 note 1 note 1 note 1 note 1 note 1 note 1 note 1 note 1 note 1 note 1 note 1 note 1 note 1 note 1 note 1 note 1 note 1 note 1 note 1 note 1



Disclaimer - Privacy
Copyright © 2008 DevASP.net