2009-08-04 32 views

回答

1

你可以這樣做通過編寫一個在onclick而不是href中輸出javascript的控制適配器。本頁面中的適用於控制適配器的補丁中列出了幾個linkbutton適配器: http://cssfriendly.codeplex.com/SourceControl/PatchList.aspx?ViewAll=true

我不知道鏈接按鈕適配器是否已添加到發行版本。

但是,您可以從此頁面下載一個,並在創建自己的適配器時將其用作參考。

。 。 。

更新2009/10/10:

其實我已經改變了我的答案了一下,控制適配器想法仍然可以工作,但它可能是更容易只是爲了創造一個新的控制,從繼承LinkBut​​ton的。我這樣說是因爲你可以得到渲染輸出並用onclick替換href,那麼你不必擔心失去linkbutton格式化的任何功能,唯一失去的就是OnClientClick

這是我寫了一個控件來添加一個NavigateURL屬性到一個linkbutton,並且當NavigateURL被設置時,它會在onclick中呈現回發javascript。它非常接近你想要的。我編寫這個控件的原因是因爲我在某些頁面上進行了分頁,並且我想提供搜索引擎可以關注的分頁鏈接,但是爲普通用戶提供了回發功能,這樣他們的過濾器就可以回傳了。

using System; 
using System.ComponentModel; 
using System.Drawing; 
using System.Globalization; 
using System.IO; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Text.RegularExpressions; 


namespace CustomControls 
{ 

    /// <summary> 
    /// This control renders the postback javascript in the onclick event if a href is passed in 
    /// this allow the control to work as normal for browsers, but will provide an alternate link for 
    /// crawlers to follow. If the href is not set it works identically to the original link button 
    /// </summary> 
    /// 
    [Themeable(true), ToolboxData("<{0}:OnClickLinkButton runat=server></{0}:OnClickLinkButton>")] 
    public class OnClickLinkButton : LinkButton 
    { 




     [Browsable(true), DefaultValue(""), Bindable(true), Category("Navigation"), Description("Set the hyperlink url for the linkbutton for browsers without javascript")] 
     public string NavigateURL 
     { 
      get 
      { 
       return ViewState["NavigateURL"] == null ? string.Empty : (string)ViewState["NavigateURL"]; 
      } 
      set 
      { 
       ViewState["NavigateURL"] = value; 
      } 
     } 

     protected override void Render(HtmlTextWriter output) 
     { 
      if (NavigateURL == string.Empty) 
      { 
       base.Render(output); 
      } 
      else 
      { 
       //we need to clear out the on client click because we are 
       //abusing the onclient click for our own purposes 
       OnClientClick = ""; 
       StringWriter sw = new StringWriter(); 
       HtmlTextWriter hw = new HtmlTextWriter(sw); 

       base.Render(hw); 

       string RenderedOutput = sw.ToString(); 

       Match m = Regex.Match(RenderedOutput, @"doPostBack\(.+\)", RegexOptions.IgnoreCase); 
       if (m.Success) 
       { 

        RenderedOutput = RenderedOutput.Replace("href", string.Format("href=\"{0}\" onclick", NavigateURL)) 
         .Replace(m.Value, m.Value + "; return false;"); 

       } 


       output.Write(RenderedOutput); 
      } 

     } 

    } 


} 
+0

謝謝克里斯,非常感謝。你所描述的情況幾乎正是我想要做的!我會拿你的代碼並且玩一下。 – 2009-08-12 15:34:07

1

您可以使用OnClientClick屬性設置javascript click事件,但默認情況下,LinkBut​​ton仍會觸發回發。如果不需要,你可以添加「return false;」到您的OnClientClick JavaScript調用的結尾。這會阻止鏈接離開頁面。

<asp:LinkButton OnClientClick="alert('hi');return false;" runat="server" PostBackUrl="#" ID="TEST">TEST</asp:LinkButton>

+0

你有'OnClientClient,而不是OnClientClick =) – 2009-08-04 15:59:04

1

如果你需要做的是改變在狀態欄中的文本,看看this

文本可以從onmouseover事件來改變

相關問題