繁体   English   中英

从ASP.Net代码隐藏触发FancyBox

[英]Triggering FancyBox from ASP.Net Codebehind

我想在asp.net页面上使用JQuery FancyBox,但是我发现的所有示例都显示了从锚标记(<a>)触发fancybox的情况。 我找不到从代码隐藏触发fancybox的示例。 更具体地说,我在LinkBut​​ton单击时动态创建了一个pdf文件。 创建文件后,我想使用fancybox展示它(我是第一次使用Jquery和FancyBox)。 任何显示如何执行此操作的示例将不胜感激。 谢谢。

在您的linkbutton刷新页面(回发)之后,您想在页面中注入一些脚本。

您的页面应该已经设置了这样的内容

<a href="#" id="hidden_link" style="display:none;"></a>
<script type="text/javascript">
    function LaunchFancyBox() { 
          $("#hidden_link").fancybox().trigger('click'); 
    } 
</script>

然后,您将从按钮单击处理程序的页面底部注入一些脚本。

因此,在页面底部,您将添加类似

<asp:Literal runat="server" ID="Literal1" />

然后在按钮单击事件处理程序中,您将拥有

Public Sub Button1_Click()
    Literal1.Text = "<script>$(document).ready(LaunchFancyBox());</script>"
End Sub

他们博客上的no.6也有助于解释这一点,但是他们每次都在页面加载时启动它,因此他们不需要注入脚本。 但是因为您想在回发时执行此操作,所以需要执行脚本注入位。

解决了。 只是对Rockin的回复做了一些小的调整。 不得不放置

<script language="javascript" type="text/javascript">
$(document).ready(function() {
    $("#hidden_link").fancybox({
        'title'         : 'Test Document',
        'titleShow'     : true,
        'titlePosition' : 'over',
        'titleFormat'   : 'formatTitle',
        'type'          : 'iframe',
        'width'         : '98%',
        'height'        : '98%',
        'hideOnOverlayClick': false,
        'hideOnContentClick' : false,
        'overlayOpacity': 0.7,
        'enableEscapeButton' : false
    });
});
</script>

在head标签中,并注入以下代码:

protected void SomeButton_Click(object sender, EventArgs e)
{          
    hidden_link.Attributes["href"] = "some_file.pdf";
    Literal1.Text = "<script>jQuery(document).ready(function() {$(\"#hidden_link\").trigger('click');});</script>";
}

感谢rockinthesixstring建议正确的方向!

我使用ASP.Net和VB.Net作为背后的代码

ASPX:1)我有一个样式为display:none的隐藏锚,并且没有文字添加隐藏的锚标记id =“ hidden_​​link” clientidmode =“ static” style =“ display:none;” class =“ fancybox fancybox.iframe”

在内容区域或标题中:

 script type="text/javascript">
             $("#hidden_link").fancybox({
             //'title': 'Test Document',`enter code here
             //'titleShow': true,
             //'titlePosition': 'over',
             //'titleFormat': 'formatTitle',
             href: "SessioDocqReportPreview.aspx",
             'width': '98%',
             'height': '98%',
             'hideOnOverlayClick': false,
             'hideOnContentClick': false,
             'overlayOpacity': 0.7,
             'enableEscapeButton': false            
         }); 

     });
2) In the code behind button click event 
     Dim csMgr As ClientScriptManager = Me.ClientScript
     If Not csMgr.IsStartupScriptRegistered("OpenPopUp_Script") Then
       Dim sb As New StringBuilder()
       sb.AppendLine("$(document).ready(function() {")
       sb.AppendLine("$(""#hidden_link"").trigger('click');")
       sb.AppendLine("return false;")
       sb.AppendLine("});")
       hidden_link.Attributes("href") = "xyz.aspx"
       csMgr.RegisterStartupScript(Me.GetType(), "OpenPopUp_Script", sb.ToString(), True)
     End If
3) This way we can get rid of Literal control, if still want to use the set the sb.Tostring to the text of the literal control with script tags and ofcourse remove the StatupScript.
BTW my fancybox is from www.fancyapps.com and v2.0.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM