2

In my asp.net project with C#, I need to call a JQuery function from code behind. Below is the function include the required script:

<link rel="stylesheet" href="/fancybox/jquery.fancybox-1.3.4.css" type="text/css" media="screen" />

   function showFancy(url, title, width, height) {
        //alert(url);
        $(".fancyBox").fancybox({
            'width': width,
            'height': height,
            'autoScale': true,
            'transitionIn': 'elastic',
            'transitionOut': 'none',
            'type': 'iframe',
            'overlayColor': '#000000',
            'overlayOpacity': 0.7,
            'position': 'fixed',
            'scrolling': 'yes',
            'modal': false,
            'target': "_parent",

            "onClosed": function () {
                window.location = window.location;
            }

        });
        $('.fancyBox').attr({ href: url, title: title });
        $('.fancyBox').click();

    }

and the way I call from aspx.cs :

           protected void Button1_Click(object sender, EventArgs e)
        {
ClientScript.RegisterClientScriptBlock(this.GetType(), "Script", "showFancy('NewForm.aspx?ordernumber2=' + $('#TxtContractNumber').val(), 'Send', '55%', '65%');", true);
            }

It works fine when I call function from client side using an input button but when I call it from server side It doesn't call the newform.aspx (when I Uncomment "alert(url);" from showFancy function, alert works) . I believe there is one issue for the way I call 'newform'. Thanks in advance for your time and answer.

1 Answer 1

1

If you want to execute this javascript on click of the button Button1 you need to add it as onclick attribute in code behind. You can add this attribute on Page_Load or OnPreRender.

Button1.Attributes.Add("onclick", "showFancy(...); return true;")

Return true at the end will call the server side(Button1_Click) of the button after the javascript, if you need to do some server actions.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks 'mybirthname' . Used your suggestion and added below code to the page_load of aspx.cs file : Button1.Attributes.Add("onclick", "showFancy('NewForm.aspx?ordernumber2=' + $('#TxtContractNumber').val(), 'Send', '55%', '65%'); return true;"); , now it opens the newform.aspx page in fancybox format(which is like a pop-up) but disappears after some seconds and after that it's not working. What do you think the problem is ?
If you want the fancybox to stay open, at the end put return false, not return true. Return false will not execute the server side call of the button and the page will not make a postback.
Not Exactly. Good that it opens the newpage but it still disappers. should both 'Button1.Attributes.Add' and 'ClientScript.RegisterClientScriptBlock' return false. for page_load, I check if(!IsPostBack) then calling 'Button1.Attributes.Add'.
I answer you on your initial question, I don't know where is the problem in your script, debug it using console.log. Create new question and ask what is wrong somebody can help it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.