View Full Version : creating custom user controls
pjoyce
04-05-2004, 05:44 AM
Nature,
There are a couple of ways you can do this, depending on what the desired result is. What I would do is instead of inheriting from Control, I would inherit from Table, and in your overridden Redner(), set the properties I want on the base class (not sure how it translates to vb but for the example below in C# it would be base.Border=1) and then use the base class' Render() function, passing in the HTMLWriter. (base.Render(output)).
In your asp page, you can then access the Rows and Cells collections of the table and add controls to the Controls collection of the Cell. remember that an uninitialized table has no rows. So you will have to add new Rows and then Cells to each row.
You could also create your own public functions like AddControl(control) which would add the controls to a new row/cell automatically.
nature
04-05-2004, 11:27 AM
I'm trying to create a user control that writes a table and puts other controls (labels, buttons, etc.) inside the table. Here's what I have so far but I don't know how to insert the controls. Thanks,Carl
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.collections.specialized
Namespace myControls
Public Class DisplayBox: Inherits Control implements IPostBackDataHandler
Public Text As String
Public css_class As String
Protected Overrides Sub Render( objTextWriter As HtmlTextWriter )
objTextWriter.AddAttribute( "border","1" )
objTextWriter.AddAttribute( "bgcolor","#eeeeee")
objTextWriter.AddAttribute( "width","100%" )
objTextWriter.RenderBeginTag( "table" )
objTextWriter.RenderBeginTag( "tr" )
objTextWriter.RenderBeginTag( "td" )
objTextWriter.Write( Text )
objTextWriter.RenderEndTag()
objTextWriter.RenderEndTag()
objTextWriter.RenderBeginTag( "tr" )
objTextWriter.RenderBeginTag( "td" )
objTextWriter.Write( Text )
objTextWriter.RenderEndTag()
objTextWriter.RenderEndTag()
objTextWriter.RenderEndTag()
End Sub
Sub CheckPassword( s As Object, e As EventArgs )
Dim strUsername, strPassword As String
Dim lblLabel As Label
strUsername = CTYPE( Controls( 2 ), TextBox ).Text
strPassword = CTYPE( Controls( 5 ), TextBox ).Text
lblLabel = CTYPE( Controls( 9 ), Label )
If strUsername = "joe" and strPassword = "secret" Then
lblLabel.Text = "Welcome Joe!"
Else
lblLabel.Text = "Invalid Password!"
End If
End Sub
Protected Overrides Sub CreateChildControls()
Me.Controls.Add( New LiteralControl( "<div style=""border: 5px " & _
"inset #cccccc;background-color:#eeeeee;width:50%;padding:10px"">" ) )
' Add Username
Me.Controls.Add( New LiteralControl( "[b]Username:</b> " ) )
Me.Controls.Add( New TextBox )
Me.Controls.Add( New LiteralControl( "<p>" ) )
' Add Password
Dim txtPass As New TextBox
Me.Controls.Add( New LiteralControl( "[b]Password:</b> " ) )
txtPass.TextMode = TextBoxMode.Password
Me.Controls.Add( txtPass )
Me.Controls.Add( New LiteralControl( "<p>" ) )
' Add Submit Button
Dim btnButton As New Button
btnButton.Text = "Login!"
AddHandler btnButton.Click, AddressOf checkPassword
Me.Controls.Add( btnButton )
Me.Controls.Add( New LiteralControl( "</div>" ) )
' Add Label Control
Dim lblLabel As New Label
lblLabel.EnableViewState = False
Me.Controls.Add( lblLabel )
End Sub
End Class
End Namespace
blabberblog
04-07-2004, 12:20 PM
After the td where you have the:
objTextWriter.Write( Text )
Render the controls like this:
objTextWriter.RenderControl(ctrl);
blabberblog
04-07-2004, 12:23 PM
Oops!
I meant like this:
ctrl.RenderControl(objTextWriter)
vBulletin® ©Jelsoft Enterprises Ltd.