PDA

View Full Version : Custom Web Controls??


david7777
06-09-2003, 01:12 PM
Im new to ASP.net, and dont really know much of the correct terms to use, so please bare with me...

I have just installed Visual Studio.net, and want to create my own web control.
ie:

If i use the following code:

<db:contentbox title="Header: This is a content Box">
<db:body>
Some content goes here...
</db:body>
<db:footer>
This is a footer...
</db:footer>
<db:/contentbox>

I want it to output the following when viewed by a browser:

<table>
<tr class="header">
<td>
Header: This is a content Box
</td>
</tr>
<tr class="body">
<td>
Some content goes here...
</td>
</tr>
<tr class="footer">
<td>
This is a footer...
</td>
</tr>
</table>

First of all - what is that called? A custom web control or a custom user control? Or something else?

Secondly, can anyone help me? - Any online tutorials?

ReyN
06-09-2003, 03:53 PM
>> Im new to ASP.net, and dont really know much of the correct terms to use, ...

hmmm, i suggest then that you start and get thoroughly familiar with the base control classes, before you attempt to build custom or user controls.

but to answer your other question ...

>> First of all - what is that called? A custom web control or a custom user control? Or something else?

That depends on how you design, store, and intend to use the file, but basically ...

a user control is just like any web form ( without the <form runat="server"> tags ), which you can insert into any other web form, much like using includes. the main difference is that the entire user control, or pagelet as they were previously called, as well as any of its controls, is completely programmable.

whereas a custom control is a class that you define, and which can derive or inherit from any of the base control classes. custom classes can have user-defined properties, methods and events, and are much more adaptable to custom applications, but are a bit more involved to implement.

anyway, from what you have posted, i believe you can accomplish that using any of the available templated controls ( Repeater, DataList or DataGrid ).unless you have special custom needs, i can safely say that these controls will suffice for most needs.

david7777
06-09-2003, 04:04 PM
Thanx for the reply...

I have been working with asp.net for a short while, but not using code-behind... So i have a pretty good understanding of the web controls. My problem is that i only just started using code-behind, and want to go into using classes and all that fun stuff :)

A repeater or data control wont do the trick in this case...

I have programmed in Java, so i understand how to use the object oriented aproach, but do you know of any good sites with nice tutorials to start out?

PS: Where should i start with solving problem in my previous post?

allida77
06-09-2003, 05:05 PM
something I used was one of the starterkits (timertracker vbsdk) :

http://www.asp.net/Default.aspx?tabindex=9&tabid=47

It really gave me a good understanding of coding oop with vb.net and asp.net

angiras
06-09-2003, 07:57 PM
do a class

public class myTable : inherites htmlTable


do 3 properties as string for header content footer

and add rows with innertext = header ......

then
you just put in your page

dim mT as myTable = new myTable()

mt.Header = "oh my head !"
mt.content

ect..

then

anyContainer.controls.add(mt)


and you ar done :-))

it's really easy in this way

david7777
06-10-2003, 08:46 AM
Thax for the replies!

The thing is that i want to create my own tags... The reason is that i am trying to create a website template system, where the front end designer will be able to use tags like <db:menu> and <db:advert size="medium"> etc... Then that person wont need to know asp.net, but only learn which tags to use. Then he can place the items where wanted, with no coding involved...

I dont even know where to start with that? :confused:

angiras
06-10-2003, 11:10 AM
then you need to use masterpage which will be in the future version of .net 2.0 (template class)

there are a few models

one from microsoft (http://www.asp.net/ControlGallery/ControlDetail.aspx?Control=385)

one from Paul Wilson (http://www.aspalliance.com/PaulWilson/Articles/?id=13)

and I have adapted mine for my needs

it is nearlly the only good solution to works with designers

david7777
06-10-2003, 11:31 AM
Thats exactly what im using...
The one you gave me actually :D

At the moment, i am trying to get it to work with vs.net, but i dont know how... Is it possible?

angiras
06-10-2003, 03:17 PM
which one are you using : both works fine


I am using a "variante" of Wlison's masterpage, I have rewrited both versins vb net or C#

what is exactly your problem with masterpages ?
what is not working ?

I can send you any version of mine (C# or VB net)

david7777
06-11-2003, 09:43 AM
Thanx - im using masterpage - VB version

It runs perfectly, but i want to get it to work in VS.net, and not display an error in the design view.

At the moment, it says: "Error creating control- cc" "Parser Error: Unknown server tag 'ms:ContentContainer'."

angiras
06-11-2003, 10:37 AM
be carefull

<%@ Register TagPrefix="mp " namespace="Microsoft.Web.Samples.MasterPages" assembly="MasterPages" %>


<mp :contentcontainer runat="server" MasterPageFile="sub/TestMasterPage.ascx">


[assembly: TagPrefix("Microsoft.Web.Samples.MasterPages", "mp")]

namespace Microsoft.Web.Samples.MasterPages {

but wilson's version is better

david7777
06-11-2003, 11:36 AM
Ok great - ill have a look at wilsons version... Thanx for the advice!

david7777
06-11-2003, 11:47 AM
Angiras - Please will you send the vb version for me? (Wilsons version) Thanx!:D

angiras
06-11-2003, 11:59 AM
Namespace masterPage

Public Class container : Inherits HtmlContainerControl

#Region " variables "

Private _defaultFile As String
Private _defaultContent As String

Private template As Control = Nothing
Private defaults As New contentRegion()
Private contents As New ArrayList()

#End Region

#Region " proprietes "

<Category("MasterPage"), Description("chemin de l'User Control")> _
Public Property defaultFile() As String
Get
If (_defaultFile.Equals("")) Or (_defaultFile Is Nothing) Then
Return getConfig("masterPage.defaultFile")
Else
Return _defaultFile
End If
End Get
Set(ByVal Value As String)
_defaultFile = Value
End Set
End Property

Private ReadOnly Property defaultFolder() As String
Get
Dim s As String
s = getConfig("masterPage.defaultFolder")
If s = "" Then s = "~/modeles/"
Return s
End Get
End Property

Private ReadOnly Property templateFile() As String
Get
Return defaultFolder + defaultFile
End Get
End Property

<Category("MasterPage"), Description("Control ID for Default Content")> _
Public Property defaultContent() As String
Get
If (_defaultContent.Equals("")) Or (_defaultContent Is Nothing) Then
Return getConfig("masterPage.defaultContent")
Else
Return _defaultContent
End If
End Get
Set(ByVal Value As String)
_defaultContent = Value
End Set
End Property

#End Region

#Region " constructeur "

Public Function getConfig(ByVal key As String) As String
Dim nameValue As NameValueCollection
nameValue = CType(ConfigurationSettings.GetConfig("parametres/global"), NameValueCollection)
Return (nameValue(key))
End Function


Public Sub New()
MyBase.new()
If _defaultContent Is Nothing Then
_defaultContent = "Content"
End If
End Sub 'New

Protected Overrides Sub RenderBeginTag(ByVal w As HtmlTextWriter)
End Sub 'RenderBeginTag

Protected Overrides Sub RenderEndTag(ByVal w As HtmlTextWriter)
End Sub 'RenderEndTag


Protected Overrides Sub AddParsedSubObject(ByVal obj As Object)
If TypeOf obj Is contentRegion Then
Me.contents.Add(obj)
Else
Me.defaults.Controls.Add(CType(obj, Control))
End If
End Sub 'AddParsedSubObject


Protected Overrides Sub OnInit(ByVal e As EventArgs)
Me.buildMasterPage()
Me.buildContents()
MyBase.OnInit(e)
End Sub 'OnInit

#End Region

#Region " methodes "

Private Sub buildMasterPage()
If Me.defaultFile = "" Then
Throw New Exception("defaultFile Property for MasterPage must be Defined")
End If
Me.template = Me.Page.LoadControl(Me.templateFile)
Me.template.ID = Me.ID + "_template"

Dim count As Integer = Me.template.Controls.Count
Dim index As Integer
For index = 0 To count - 1
Dim control As control = Me.template.Controls(0)
Me.template.Controls.Remove(control)
If control.Visible Then
Me.Controls.Add(control)
End If
Next index
Me.Controls.AddAt(0, Me.template)
End Sub 'BuildMasterPage


Private Sub buildContents()
If Me.defaults.HasControls() Then
Me.defaults.ID = _defaultContent
Me.contents.Add(Me.defaults)
End If

Dim content As contentRegion
For Each content In Me.contents
Dim [region] As control = Me.FindControl(content.ID)
If [region] Is Nothing Or Not TypeOf [region] Is contentRegion Then
Throw New Exception("ContentRegion with ID '" + content.ID + "' must be Defined")
End If
[region].Controls.Clear()

Dim count As Integer = content.Controls.Count
Dim index As Integer
For index = 0 To count - 1
Dim control As control = content.Controls(0)
content.Controls.Remove(control)
[region].Controls.Add(control)
Next index
Next content
End Sub 'BuildContents

#End Region

End Class 'container

Public Class contentRegion : Inherits Panel

Public Sub New()
MyBase.new()
End Sub 'New

Public Overrides Sub RenderBeginTag(ByVal w As HtmlTextWriter)
End Sub 'RenderBeginTag

Public Overrides Sub RenderEndTag(ByVal w As HtmlTextWriter)
End Sub 'RenderEndTag

End Class 'contentRegion

End Namespace
----------------------------

only the getConfig method use personal section of web.config file
you can remove this method
and use configurationSetting.AppSetting("")

and you need 2 or 3 keys (depending if you want defaultFolder as readonLy or not)


<add key="masterPage.defaultFolder" value="~/modeles/" />
<add key="masterPage.defaultFile" value="template.ascx" />
<add key="masterPage.defaultContent" value="content" />



into your ascx file :

<%@ Register TagPrefix="any" Namespace="angiras.webTemplates.masterPage" Assembly="angiras.webTemplates" %>

david7777
06-11-2003, 01:44 PM
Thax again! So now i cant get it to work properly... :(

I feel like such an idiot... :o i guess thats how you learn...

I am trying to get everything to work with vs.net...

I know that some of my stuff is DEFINATELY wrong, but i dont know the right way...

I have created the pages as follows:

I Took out the code - takes too much space

So there everything is. I have tried all sorts of things, but cant get it to compile. I want the code to compile using vs.net, not my own compile bat file if possible...

At the moment, this is all under its own project in vs.net.

Any suggestions?

angiras
06-12-2003, 08:28 AM
what is your problem to compile it with visual studio ?

here is my template.ascx (simplified)



<?xml version="1.0" encoding="iso-8859-1" ?>
<%@ Control Codebehind="template.ascx.vb" Inherits="angiras.webSite.template" %>
<%@ Register TagPrefix="ys" Namespace="ys.webTemplates.masterPage" Assembly="ys.webTemplates" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='fr'>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><ys:contentregion id="ysTitle" runat="server">default title</ys:contentregion></title>
<ys:contentregion id="ysMetasHead" runat="server">
</head>
<body><form id="frm" runat="server">
<div id="webSite">
<ys:contentRegion id="ysContent" runat="server" />
</div>
</form></body>
</html>


-------------------------
template.ascx.vb

--------------------------

Namespace webSite

Public MustInherit Class template : Inherits UserControl


#Region " controles "

Protected WithEvents ysTitle As masterPage.contentRegion
Protected WithEvents ysMetasHead As masterPage.contentRegion
Protected WithEvents ysContent As masterPage.contentRegion

#End Region

#Region " Web Form Designer Generated Code "

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

End Class
End Namespace

angiras
06-12-2003, 09:05 AM
with this version you don't implements INamingContainer and you avoid in that way esoterics like ctrl_0_ctrl_0_myID

but to access your control you need to do

Me.FindControl("region").Controls.Add(any)

or

Dim c As Control = Me.FindControl("region")


and

c.controls.add(any)

david7777
06-12-2003, 01:15 PM
thanx! ill try it out and get back to you... :thumbsup:

david7777
06-12-2003, 01:30 PM
<Category("MasterPage"), Description("chemin de l'User Control")> _
Public Property defaultFile() As String


The compiler has a problem with that line -
C:\..\masterpage.vb(18): Type 'Category' is not defined.
C:\..\masterpage.vb(18): Type 'Description' is not defined.

david7777
06-12-2003, 04:56 PM
Ok - i sorted out the above problem by just commenting out "<Category("MasterPage"), Description("chemin de l'User Control")>"

So everything seems to compile now. My compiled file is called WebProject1.dll, so does that mean that my assembly is WebProject1?

When i run the application, i get the following error:
"Could not load type masterPage.container from assembly WebProject1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null."

I have this code for my default.aspx page:

<%@ Register TagPrefix="ms" namespace="masterPage" assembly="WebProject1" %>
<%@ Page CodeBehind="default.aspx.vb" Language="vb" AutoEventWireup="false" Inherits="WebProject1.mainPage" %>
<ms:container id="cc" masterPageFile="" runat="server">
<ms:contentRegion id="ysMetasHead" runat="server">
<META content="">
</ms:contentRegion>
<ms:contentRegion id="ysTitle" runat="server">Home</ms:contentRegion>
<ms:contentRegion id="ysContent" runat="server">
<H2>Welcome to our site!</H2>
<P>Please use the menu...</P>
</ms:contentRegion>
</ms:container>

angiras
06-12-2003, 09:09 PM
in visual studio look what is your assembly name(root)

for me :

<%@ Register TagPrefix="ys" Namespace="angiras.webTemplates.masterPage" Assembly="angiras.webTemplates" %>


means

DLL = angiras.webTemplates.dll
ASSEMBLY = angiras.webTemplates.masterPage

my root namespace is angiras.webTemplates
and in it I have many namespace, for our subject it is masterpage


then try


<%@ Register TagPrefix="ms" namespace="WebProject1.masterPage" assembly="WebProject1" %>

david7777
06-13-2003, 09:41 AM
Its working!! :D

Thanx a million angiras! :thumbsup:

angiras
06-13-2003, 09:47 AM
Great! I'm glad :))