PDA

Click to See Complete Forum and Search --> : Default CssClass on Custom Control


Memnoch1207
Mar 30th, 2004, 12:24 PM
I am creating a custom ASP.NET textbox control and I am trying to set the default CssClass, but I can't figure out how.
I have added attributes to the controls (OnBlur and OnFocus) those work fine changing the cssClass, but when the page initially loads the textbox doesn't have a default cssClass.

Here's my code

Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.ComponentModel

Namespace JHATextBox

Public Class JHATextBox
Inherits TextBox

'This should be the default cssClass when the page loads.
Private _cssClass As String = "formBox"

Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
Dim strScript As String = ""

If Page.IsClientScriptBlockRegistered("ClientScript") = False Then
strScript &= "<script language=""javascript"" type=""text/javascript"">" & vbCrLf
strScript &= vbTab & "function activeField(ptr){" & vbCrLf
strScript &= vbTab & vbTab & "if((ptr.disabled != true) && (ptr.readOnly != true)) {" & vbCrLf
strScript &= vbTab & vbTab & vbTab & "ptr.className = 'formBoxSelected';" & vbCrLf
strScript &= vbTab & vbTab & "}" & vbCrLf
strScript &= vbTab & "}" & vbCrLf
strScript &= vbTab & "function inactiveField(ptr) {" & vbCrLf
strScript &= vbTab & vbTab & "if((ptr.disabled != true) && (ptr.readOnly != true)) {" & vbCrLf
strScript &= vbTab & vbTab & vbTab & "ptr.className = 'formBox';" & vbCrLf
strScript &= vbTab & vbTab & "}" & vbCrLf
strScript &= vbTab & "}" & vbCrLf
strScript &= "</script>" & vbCrLf

Page.RegisterStartupScript("ClientScript", strScript)
End If

writer.AddAttribute("onBlur", "void inactiveField(this);")
writer.AddAttribute("onFocus", "void activeField(this);")
writer.RenderBeginTag("input")
writer.RenderEndTag()

End Sub

Public Overrides Property CssClass() As String
Get
Return _cssClass
End Get
Set(ByVal Value As String)
_cssClass = Value
End Set
End Property

End Class

End Namespace

If I just type formBox as the CssClass into the controls properties, it works fine, but I can't set it in the code above for some reason.

nemaroller
Mar 30th, 2004, 08:01 PM
Doesn't make sense to me.

If a control doesn't have a cssClass value, it grabs its parent by default.

A few things I would check:

It could be the designer wrote a value on the aspx side when you placed the control on the page.

Your javascript function is resetting it something.

Remember, styles classes are case sensitive.

Trying putting:

<DefaultValueAttribute("formBox")>
in front of your public cssclass property, which will initialize it to that value by default when one hasn't been assigned, when you place the control on a form.