这是一个ASP.NET 4.5 Web窗体应用程序。
我创建了一个继承Button类的控件,它只是确保用户不能双击它。我的基础是像this one这样的例子。
我的问题是它放在页面上时没有取出任何css格式。同一页面上的标准按钮确实显示来自css的样式。为了解决这个问题,我试图添加CssClass =" uiButton"在标记中,但这也不起作用。该按钮没有应用格式。
我似乎无意中破坏了按钮使用css进行格式化的能力。 任何人都有任何关于寻找什么的想法?我没有领先。谢谢!
马特
这是我的按钮类:
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
这是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;
}
这是标记。 css添加在母版页中,只是由标准按钮继承。它已经工作了多年,所以这个问题似乎在我实现这个按钮。
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div>
Testing 123
</div>
<div>
<cc1:MyButton ID="MyButton1" runat="server" text="Push me!" CssClass="uiButton" />
<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>
我添加了CssClass =&#34; uiButton&#34;作为一种解决方法(不起作用),但该标记中的标准按钮使用整个应用程序中使用的uiButton类的正确样式呈现。 MyButton渲染时未应用任何样式。