PDA

View Full Version : ASP.NET (VB) 2.0 custom Control issues


Strange_Will
02-01-2006, 11:49 AM
Default.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!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" >
<head runat="server">
<title>Title</title>
</head>
<body>
<form id="form_main" runat="server">
<asp:Panel ID="Main" runat="server" />
</form>
</body>
</html>


Default.aspx.vb

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.Load
Dim no_NoteAdd As New Control
no_NoteAdd = LoadControl("App_Controls\noteadd.ascx")
Main.Controls.Add(no_NoteAdd)
End Sub
End Class

NoteAdd.ascx

<%@ Control Language="VB" ClassName="noteadd" CodeFile="~/App_Controls/noteadd.ascx.vb" Inherits="noteadd"%>
<table>
<tr>
<td>
<asp:label id="test" runat="server"/>
</td>
<td>
A CONTROL!
</td>
</tr>
</table>


Noteadd.ascx.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Web
Imports System.Web.UI

Public Class noteadd : Inherits Control
Public Shared _message As String

Public Shared Property Message() As String
Get
Return _message
End Get
Set(ByVal value As String)
_message = value
End Set
End Property

Protected Sub Render(ByVal Output As HtmlTextWriter)
test.Text = "This is a test on test.text"
'test.text = _message
End Sub

End Class


I've been trying custom controls and I'm having an issue with a few things here, first I get an error:

C:\Documents and Settings\User\My Documents\Visual Studio 2005\WebSites\test1\App_Controls\noteadd.ascx.vb(1): error ASPNET: Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl).

And second

I want to set _message to a value, and have it change on the control, I've been reading up a little on properties, but this is a mess and I can't seem to get it right and I can't find a site that is clear on how this works.

Any ideas?

wayneph
02-01-2006, 02:45 PM
Something like this should work..

1. Inherit from UserControl instead of Control
2a. Don't make properties shared. Have the actual member private, and just make the property public.
2b. When loading the control DirectCast will let you create a strongly typed object so you can use the properties.

Imports Microsoft.VisualBasic
Imports System
Imports System.Web
Imports System.Web.UI

Public Class noteadd : Inherits UserControl
Private _message As String

Public Property Message() As String
Get
Return _message
End Get
Set(ByVal value As String)
_message = value
End Set
End Property

Protected Sub Render(ByVal Output As HtmlTextWriter)
'test.Text = "This is a test on test.text"
test.text = _message
End Sub

End Class

Dim no_NoteAdd As noteadd
no_NoteAdd = DirectCast(LoadControl("App_Controls\noteadd.ascx"), noteadd)
no_NoteAdd.Message = "Something Dynamic"
Main.Controls.Add(no_NoteAdd)

Strange_Will
02-01-2006, 03:26 PM
Default.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!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" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form_main" runat="server">
<asp:Panel ID="Main" runat="server" />
</form>
</body>
</html>


Default.aspx.vb


Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.Load
Dim no_NoteAdd As noteadd()
no_NoteAdd = DirectCast(LoadControl("App_Controls\noteadd.ascx"), noteadd)
no_NoteAdd.Message = "Something Dynamic"
Main.Controls.Add(no_NoteAdd)
End Sub
End Class


Noteadd.ascx

<%@ Control Language="VB" ClassName="noteadd" CodeFile="~/App_Controls/noteadd.ascx.vb" Inherits="noteadd"%>
<table>
<tr>
<td>
<asp:label id="test" runat="server"/>
</td>
<td>
A CONTROL!
</td>
</tr>
</table>


Noteadd.ascx.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Web
Imports System.Web.UI

Public Class noteadd : Inherits UserControl
Private _message As String

Public Property Message() As String
Get
Return _message
End Get
Set(ByVal value As String)
_message = value
End Set
End Property

Protected Overrides Sub Render(ByVal Output As HtmlTextWriter)
'test.Text = "This is a test on test.text"
test.text = _message
End Sub

End Class



Now I get a few errors:

type 'noteadd' is not declared
name 'test' is not declared

hmm.

Strange_Will
02-02-2006, 08:56 AM
Any ideas?

wayneph
02-03-2006, 10:04 PM
Whoo hoo! I think I finally found it. (been playing with it for an hour or so now...)

This is the page that finally turned the corner: http://odetocode.com/Blogs/scott/archive/2005/09/12/2186.aspx

I had to make a few changes. In the default.aspx file I had to add a @Register directive to the top (right under the @Page directive):
<%@ Reference Control="~/App_Controls/noteadd.ascx" %>

Here is what the PageLoad ended up looking like. (Notice the controls type is App_Controls_noteadd.)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim no_NoteAdd As App_Controls_noteadd
no_NoteAdd = DirectCast(LoadControl("~/App_Controls/noteadd.ascx"), App_Controls_noteadd)
no_NoteAdd.Message = "Something Dynamic"
Main.Controls.Add(no_NoteAdd)
End Sub

There was also a problem with your UserControl that I missed earlier. This is what I have now (explained below):
Public Property Message() As String
Get
Return test.Text
End Get
Set(ByVal value As String)
test.Text = value
End Set
End Property

I'm just having the propety directly update the control. If you have the private member, at some point you'd have to put it in the control anyway. If you override the Render method (like we initially had) you have to rewrite the render for everything in the control, not just set the controls value. it ends up being a lot more work.

Give this a try and let me know what you end up with.

Strange_Will
02-08-2006, 11:41 AM
Awesome! Okay so it works on Default.aspx, trouble is with master pages I can't put refrence in the default.aspx, and if I put it in the master it can't be called by default.aspx


That should be easier than the last question, but wow thanks man you're a huge help!

Strange_Will
02-08-2006, 11:49 AM
Wait nevermind got the master file to work!

Thanks man!