2013-05-17 40 views
0

我正在創建一個需要在模態彈出窗口中打開一個可配置頁面的asp鏈接按鈕。Linkbutton打開一個模式彈出窗口,但其彈出窗口立即關閉

打開模式彈出窗口,但立即關閉。 我把href#但它不工作。

如果我檢查生成的HTML是:

<a id="ctl00_ctl38_g_69332c4e_2a87_4bb4_ad70_f7debbd14e91_LnkButton" onclick="OpenModalPopup('www.google.com', '800', '300');" href="javascript:__doPostBack('ctl00$ctl38$g_69332c4e_2a87_4bb4_ad70_f7debbd14e91$LnkButton','')"> 

ASCX或web部件文件

<script type="text/javascript"> 
    function OpenModalPopup(pageUrl, widthParameter, heightParameter) { 
     var options = { url: pageUrl, width: widthParameter, height: heightParameter, showClose: true }; 
     SP.SOD.execute('sp.ui.dialog.js', 'SP.UI.ModalDialog.showModalDialog', options); 
    } 
</script> 
<asp:LinkButton CssClass="pwcLinkButton" ID="LnkButton" runat="server" href="#"></asp:LinkButton> 

的.cs

using System; 
using System.ComponentModel; 
using System.Web.UI.WebControls.WebParts; 

namespace xx.SP.xx.WebParts.VisualWebParts.LinkButton 
{ 
    [ToolboxItemAttribute(false)] 
    public partial class LinkButton : WebPart 
    { 
     private string _LinkText; 
     private Uri _Link; 
     private Boolean _OpenModal; 
     private int _Width; 
     private int _Height; 

     [WebBrowsable(true), WebDisplayName("LinkText"), WebDescription("Text for the link"), 
     Personalizable(PersonalizationScope.Shared), Category("xx- xx"), 
     System.ComponentModel.DefaultValue("")] 
     public string LinkText 
     { 
      get { return _LinkText; } 
      set { _LinkText = value; } 
     } 

     [WebBrowsable(true), WebDisplayName("Link"), WebDescription("Link"), 
     Personalizable(PersonalizationScope.Shared), Category("xx- xx"), 
     System.ComponentModel.DefaultValue("")] 
     public Uri Link 
     { 
      get { return _Link; } 
      set { _Link = value; } 
     } 

     [WebBrowsable(true), WebDisplayName("OpenModal"), WebDescription("OpenModal"), 
     Personalizable(PersonalizationScope.Shared), Category("xx - xx"), 
     System.ComponentModel.DefaultValue("")] 
     public Boolean OpenModal 
     { 
      get { return _OpenModal; } 
      set { _OpenModal = value; } 
     } 

     [WebBrowsable(true), WebDisplayName("Width"), WebDescription("Width"), 
     Personalizable(PersonalizationScope.Shared), Category("xx- xx"), 
     System.ComponentModel.DefaultValue("")] 
     public int WidthPopup 
     { 
      get { return _Width; } 
      set { _Width = value; } 
     } 

     [WebBrowsable(true), WebDisplayName("Height"), WebDescription("Height"), 
     Personalizable(PersonalizationScope.Shared), Category("xx- xx"), 
     System.ComponentModel.DefaultValue("")] 
     public int HeightPopup 
     { 
      get { return _Height; } 
      set { _Height = value; } 
     } 

     // Uncomment the following SecurityPermission attribute only when doing Performance Profiling on a farm solution 
     // using the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready 
     // for production. Because the SecurityPermission attribute bypasses the security check for callers of 
     // your constructor, it's not recommended for production purposes. 
     // [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)] 
     public LinkButton() 
     { 
     } 

     protected override void OnInit(EventArgs e) 
     { 
      base.OnInit(e); 
      InitializeControl(); 
     } 

     protected void Page_Load(object sender, EventArgs e) 
     { 
      if (LnkButton != null && Link !=null && LinkText != null) 
      { 
       LnkButton.Text = LinkText; 
       if (OpenModal) 
       { 
        LnkButton.Attributes.Add("onclick", "OpenModalPopup('" + Link.ToString() + "', '" + WidthPopup.ToString() + "', '" + HeightPopup.ToString() + "');"); 
       } 
       else 
       { 
        LnkButton.Attributes.Remove("onclick"); 
        LnkButton.PostBackUrl = Link.ToString(); 
       } 
      } 
     } 
    } 
} 

回答

2

更改您的代碼,看看是否它幫助:

LnkButton.Attributes.Add("onclick", "OpenModalPopup('" + Link.ToString() + "', '" + WidthPopup.ToString() + "', '" + HeightPopup.ToString() + "'); return false;"); 
0

看來,它不是你的彈出窗口關閉,但你的鏈接導致回發,所以頁面刷新。如果您的鏈接onclick處理函數返回false,它將被瀏覽器解釋爲取消鏈接clic,因此不會導致回發。所以改變這一行爲:

LnkButton.Attributes.Add("onclick", "OpenModalPopup('" + Link.ToString() + "', '" + WidthPopup.ToString() + "', '" + HeightPopup.ToString() + "'); return false;"); 

應該做的伎倆。