Results 1 to 2 of 2

Thread: Default CssClass on Custom Control

Threaded View

  1. #1

    Thread Starter
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861

    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:
    1. Imports System
    2. Imports System.Web
    3. Imports System.Web.UI
    4. Imports System.Web.UI.WebControls
    5. Imports System.ComponentModel
    6.  
    7. Namespace JHATextBox
    8.  
    9.     Public Class JHATextBox
    10.         Inherits TextBox
    11.  
    12.         'This should be the default cssClass when the page loads.
    13.         Private _cssClass As String = "formBox"
    14.  
    15.         Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
    16.             Dim strScript As String = ""
    17.  
    18.             If Page.IsClientScriptBlockRegistered("ClientScript") = False Then
    19.                 strScript &= "<script language=""javascript"" type=""text/javascript"">" & vbCrLf
    20.                 strScript &= vbTab & "function activeField(ptr){" & vbCrLf
    21.                 strScript &= vbTab & vbTab & "if((ptr.disabled != true) && (ptr.readOnly != true)) {" & vbCrLf
    22.                 strScript &= vbTab & vbTab & vbTab & "ptr.className = 'formBoxSelected';" & vbCrLf
    23.                 strScript &= vbTab & vbTab & "}" & vbCrLf
    24.                 strScript &= vbTab & "}" & vbCrLf
    25.                 strScript &= vbTab & "function inactiveField(ptr) {" & vbCrLf
    26.                 strScript &= vbTab & vbTab & "if((ptr.disabled != true) && (ptr.readOnly != true)) {" & vbCrLf
    27.                 strScript &= vbTab & vbTab & vbTab & "ptr.className = 'formBox';" & vbCrLf
    28.                 strScript &= vbTab & vbTab & "}" & vbCrLf
    29.                 strScript &= vbTab & "}" & vbCrLf
    30.                 strScript &= "</script>" & vbCrLf
    31.  
    32.                 Page.RegisterStartupScript("ClientScript", strScript)
    33.             End If
    34.  
    35.             writer.AddAttribute("onBlur", "void inactiveField(this);")
    36.             writer.AddAttribute("onFocus", "void activeField(this);")
    37.             writer.RenderBeginTag("input")
    38.             writer.RenderEndTag()
    39.  
    40.         End Sub
    41.  
    42.         Public Overrides Property CssClass() As String
    43.             Get
    44.                 Return _cssClass
    45.             End Get
    46.             Set(ByVal Value As String)
    47.                 _cssClass = Value
    48.             End Set
    49.         End Property
    50.  
    51.     End Class
    52.  
    53. 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width