|
-
Mar 30th, 2004, 01:24 PM
#1
Thread Starter
Frenzied Member
Default CssClass on Custom Control
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
VB 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.
Last edited by Memnoch1207; Mar 30th, 2004 at 02:30 PM.
Being educated does not make you intelligent.
Need a weekend getaway??? Come Visit
-
Mar 30th, 2004, 09:01 PM
#2
I wonder how many charact
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:
VB Code:
<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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|