Risposta molto tardiva:
Ho lottato con questo acceso e spento da quando ho iniziato a lavorare in ASP. Ecco il meglio che ho ideato:
Concetto: creo un controllo personalizzato che ha un tag. Quindi nel pulsante inserisco un evento onclick che imposta document.location sul valore desiderato con JavaScript.
Ho chiamato il controllo ButtonLink, in modo da poter essere facilmente confuso con LinkButton.
aspx:
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="ButtonLink.ascx.vb" Inherits="controls_ButtonLink" %>
<asp:Button runat="server" ID="button"/>
codice dietro:
Partial Class controls_ButtonLink
Inherits System.Web.UI.UserControl
Dim _url As String
Dim _confirm As String
Public Property NavigateUrl As String
Get
Return _url
End Get
Set(value As String)
_url = value
BuildJs()
End Set
End Property
Public Property confirm As String
Get
Return _confirm
End Get
Set(value As String)
_confirm = value
BuildJs()
End Set
End Property
Public Property Text As String
Get
Return button.Text
End Get
Set(value As String)
button.Text = value
End Set
End Property
Public Property enabled As Boolean
Get
Return button.Enabled
End Get
Set(value As Boolean)
button.Enabled = value
End Set
End Property
Public Property CssClass As String
Get
Return button.CssClass
End Get
Set(value As String)
button.CssClass = value
End Set
End Property
Sub BuildJs()
' This is a little kludgey in that if the user gives a url and a confirm message, we'll build the onclick string twice.
' But it's not that big a deal.
If String.IsNullOrEmpty(_url) Then
button.OnClientClick = Nothing
ElseIf String.IsNullOrEmpty(_confirm) Then
button.OnClientClick = String.Format("document.location='{0}';return false;", ResolveClientUrl(_url))
Else
button.OnClientClick = String.Format("if (confirm('{0}')) {{document.location='{1}';}} return false;", _confirm, ResolveClientUrl(_url))
End If
End Sub
End Class
Vantaggi di questo schema: sembra un controllo. Scrivi un singolo tag per questo, <ButtonLink id = "mybutton" navigateurl = "blahblah" />
Il pulsante risultante è un pulsante HTML "reale" e quindi sembra proprio un pulsante reale. Non devi provare a simulare l'aspetto di un pulsante con CSS e quindi lottare con look diversi su browser diversi.
Mentre le abilità sono limitate, puoi facilmente estenderlo aggiungendo più proprietà. È probabile che la maggior parte delle proprietà debba semplicemente "passare" al pulsante sottostante, come ho fatto per il testo, abilitato e cssclass.
Se qualcuno ha una soluzione più semplice, più pulita o comunque migliore, sarei felice di ascoltarla. Questo è un dolore, ma funziona.