开发者

Triggering FancyBox from ASP.Net Codebehind

开发者 https://www.devze.com 2023-01-06 12:57 出处:网络
I want to use the JQuery FancyBox on an asp.net page but all the examples I have found开发者_如何学运维 show triggering a fancybox from anchor tag (< a >). I am not able to find an example where a

I want to use the JQuery FancyBox on an asp.net page but all the examples I have found开发者_如何学运维 show triggering a fancybox from anchor tag (< a >). I am not able to find an example where a fancybox is triggered from codebehind. To be more specific, I create a pdf file on the fly on a LinkButton click. After the file has been created, I want to show it using fancybox (I am using Jquery and FancyBox for the first time). Any examples showing how to do this will be much appreciated. Thanks.


after your linkbutton refreshes the page (postback), then you want to inject some script into your page.

Your page should have something like this already set

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

Then you would inject some script at the bottom of the page from your button click handler.

So at the bottom of your page you will add something like

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

Then in your button click event handler you will have

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

no.6 on their blog helps explain this as well, however they're launching it on page load ever time so they don't need to inject the script. But because you want to do it on postback, you need to do the script injection bit.


Solved. Just some minor adjustments to rockin's reply. Had to place

<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>

In the head tag, and Injected the code below:

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>";
}

Thanks rockinthesixstring for suggesting the right direction!


I was using ASP.Net with VB.Net as code behind

ASPX: 1) I have a hidden anchor with style as display:none and no Literal Add hidden anchor tag id="hidden_link" clientidmode="static" style="display:none;" class="fancybox fancybox.iframe"

In the content area or header:

 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.
0

精彩评论

暂无评论...
验证码 换一张
取 消