Custom Server Control won't pick up css formatting

44 views Asked by At

This is a ASP.NET 4.5 Web Forms app.

I created a control that inherits the Button class and it just ensures users can't double click it. I based it on examples like this one.

My problem is it didn't pick up any of the css formatting when placed on a page. A standard button on the same page does display the styles from css. To get around that I tried to just add CssClass="uiButton" in the markup but that doesn't work either. The button has no formatting applied.

I seem to have inadvertently broken the button's ability to consume css for formatting. Anyone got any ideas on what to look for? I'm out of leads. Thanks!

Matt

Here's my button class:

    Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
'<Assembly: TagPrefix("ServerControl1", "cc1")> 


<DefaultProperty("Text"), ToolboxData("<{0}:MyButton runat=server></{0}:MyButton>")>
Public Class MyButton
    Inherits Button
    Public Sub New()
        MyBase.New()
        MyBase.UseSubmitBehavior = False
    End Sub

    Protected Overrides Sub OnPreRender(e As EventArgs)
        'OnClientClick = InlineAssignHelper(Me.Enabled = False, True) & OnClientClick
        OnClientClick = "this.disabled=true;" + OnClientClick

        RegisterClientScript()
        RegisterStartupScript()

        ' MyBase.CssClass = "uiButton"

        MyBase.OnPreRender(e)
    End Sub
    <DefaultValue(False)> _
    Public Overrides Property UseSubmitBehavior() As Boolean
        Set(value As Boolean)
        End Set
        Get
        End Get
    End Property

    Private Sub RegisterClientScript()
        If Page Is Nothing Then
            Throw New Exception("Page is null, MyButton can't register scripts.")
        End If

        Dim key, url As String
        key = "MyButton"
        url = ResolveClientUrl("/scripts/helpers/MyButton.js")
        If Not Page.ClientScript.IsClientScriptIncludeRegistered(key) Then
            Page.ClientScript.RegisterClientScriptInclude(key, url)
        End If

    End Sub

    Private Sub RegisterStartupScript()
        If Page Is Nothing Then
            Throw New Exception("Page is null, MyButton can't register scripts.")
        End If

        Dim key, script As String
        key = UniqueID & "_Startup"
        script = String.Format("MyButton_InitOnClick ( '{0}' )", UniqueID)
        If Not Page.ClientScript.IsStartupScriptRegistered(key) Then
            Page.ClientScript.RegisterStartupScript(Page.GetType, key, script, True)
        End If
    End Sub

End Class

Here's the javascript:

function SubmitButton_Click() {
    var src = event.srcElement;
    src.disabled = true;
    src.aspnet_onclick();
    src.disabled = typeof( Page_IsValid ) != “undefined” ? Page_IsValid : true;
}

function SubmitButton_InitOnClick( id )
{
    var sb = document.getElementById( id );
    sb.aspnet_onclick = sb.onclick;
    sb.onclick = SubmitButton_Click;
}

Here's the markup. The css is added in the master page and is just inherited by standard buttons. It's been working for years so the issue seems to be in my implementation of this button.

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <div>
        Testing 123
    </div>
    <div>
        <cc1:MyButton ID="MyButton1" runat="server" text="Push me!" CssClass="uiButton" />
        &nbsp;
        <asp:Button ID="Button1" runat="server" Text="Button" />
        <br />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
    </div>

</asp:Content>

I have added the CssClass="uiButton" as a workaround (that didn't work) but the standard button in that markup renders with the correct style from the uiButton class used throughout the application. MyButton renders with no style applied.

0

There are 0 answers