Results 1 to 2 of 2

Thread: Default CssClass on Custom Control

  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

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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:
    1. <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
  •  



Click Here to Expand Forum to Full Width