0

I'm fairly new to asp.net/c# and very new to jQuery so please forgive me if this is a newb question.

I have a jQuery fancybox installed which is defined by:

 function pageLoad(sender, args) {  
 $("#ctl00_wpm_ShowProduct_ctl04_TestAddToCart").fancybox({  
    'width' : 600,   
    'height' : 620,  
    'type' : 'iframe'   });      
}

When I click the asp:HyperLink that renders as ctl00_wpn_ShowProduct_ctl04_TestAddToCart everything works (I realize if I move things on the page this could break but I need to make it work before I make it work well).

I have another asp:Button on that page that I want to do some processing and then open the target URL (calendar) in a fancybox. In the original the click on the link brought fancybox into play and opened the calendar URL. Now if I remove that, how do I get the same results from the button after it does its half-dozen other tasks? I won't have a link to which I can tie the jQuery activation.

I know that codebehind is server and jQuery is client, but if it can work from a link it should work from a button.

3
  • 1
    just a suggestion, if possible, you should use "ClientIDMode=Static" on your page so that your are not dealing with those crazy asp.net control names. beyondrelational.com/blogs/hima/archive/2010/07/16/… Commented Feb 15, 2012 at 19:35
  • Thanks Zach, but our ecommerce store is set up to use 2.0 (ancient, isn't it!). I sure wish I could get away from the crazy names. Commented Feb 15, 2012 at 20:53
  • @3nigma, thanks for fixing my formatting. I'm learning from it! Commented Feb 15, 2012 at 21:46

3 Answers 3

2

I would recommend using ClientID on the client side.

If you can do it exclusively through the front end, you could do something like this.

<asp:Button OnClientClick="LaunchTheFancyBox()" ID=""... />

<script>
    function LaunchTheFancyBox(){ 
        $('#<%=myControl.ClientID%>').fancybox({  
            'width' : 600,   
            'height' : 620,  
            'type' : 'iframe'   
        });    
    }
</script>

Note: If you need to send it from the server, you need to inject it through an control.

Here's a post with the same sort of question
http://www.eggheadcafe.com/community/csharp/2/10346901/open-jquery-fancybox-from-codebehind.aspx

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

Comments

1

If you want to run javascript on postback and:

you are using AJAX you can do this:

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "ScriptKey", "myFunction();", true);

otherwise you can use this:

ClientScriptManager.RegisterClientScriptBlock(this.GetType(), "ButtonClickScript", "myFunction();", true);

References:

http://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.registerstartupscript.aspx

http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx

Comments

0

In your button, use (you'll need to set your parameter values):

OnClientClick="pageLoad(sender, args)"

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclientclick.aspx

Comments

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.