0

I have a jquery function called MyFunc() in a seperate .js file. I wanted to call this function in c# code behind button click event. (i.e. if data gets added successfully, call MyFunc() ). Originally this function looked like this

$(".next").click(function () {

where it will directly be called during when the asp: button is clicked.

<asp:button id="btnNext" runat="server" CssClass="next action-button" Text="Next" OnClientClick="return false"/>

So I changed the function to

function MyFunc(){}

and the button to

<asp:Button ID="btnNext" runat="server" Text="Next" CssClass="action-button" OnClick="btnNext_Click1"/>

and in code behind button click event

Page.ClientScript.RegisterClientScriptInclude(GetType(), "MyScript", "Easying.js");
Page.ClientScript.RegisterClientScriptInclude(GetType(), "MyScript", "Reg.js");
Page.ClientScript.RegisterStartupScript(GetType(), "MyScript", "MyFunc()", true);

it gives me no errors. But it doesnt work. below is the function.

var current_fs, next_fs, previous_fs; //fieldsets
var left, opacity, scale; //fieldset properties which we will animate
var animating; //flag to prevent quick multi-click glitches

$(".next").click(function() {

    if (animating) return false;
    animating = true;

    current_fs = $(this).parent();
    next_fs = $(this).parent().next();

    //activate next step on progressbar using the index of next_fs
    $("#progressbar li").eq($("fieldset").index(next_fs)).addClass("active");

    //show the next fieldset
    next_fs.show();
    //hide the current fieldset with style
    current_fs.animate({ opacity: 0 }, {
        step: function (now, mx) {
            //as the opacity of current_fs reduces to 0 - stored in "now"
            //1. scale current_fs down to 80%
            scale = 1 - (1 - now) * 0.2;
            //2. bring next_fs from the right(50%)
            left = (now * 50) + "%";
            //3. increase opacity of next_fs to 1 as it moves in
            opacity = 1 - now;
            current_fs.css({ 'transform': 'scale(' + scale + ')' });
            next_fs.css({ 'left': left, 'opacity': opacity });
        },
        duration: 800,
        complete: function () {
            current_fs.hide();
            animating = false;
        },
        //this comes from the custom easing plugin
        easing: 'easeInOutBack'
    });
});

also, as you can see, MyFunc() is used to change from one fieldset to another. my aspx page has 3 fieldsets. and a progress bar (1--2--3). The progress bar should move from one to the other. and the fieldset also shud change. the current situation is I see a the progress bar move from 1 to directly 3. and no change in the fieldset. this is where I took the code for it. As Ive mentioned above, it works perfectly when I call it directly frm the aspx page. http://codepen.io/atakan/pen/gqbIz

heres the MyFunc() !!

function MyFunc() {
var current_fs, next_fs, previous_fs; //fieldsets
var left, opacity, scale; //fieldset properties which we will animate
var animating; //flag to prevent quick multi-click glitches

if (animating) return false;
animating = true;

current_fs = $(this).parent();
next_fs = $(this).parent().next();

//activate next step on progressbar using the index of next_fs
$("#progressbar li").eq($("fieldset").index(next_fs)).addClass("active");

//show the next fieldset
next_fs.show();
//hide the current fieldset with style
current_fs.animate({ opacity: 0 }, {
    step: function (now, mx) {
        //as the opacity of current_fs reduces to 0 - stored in "now"
        //1. scale current_fs down to 80%
        scale = 1 - (1 - now) * 0.2;
        //2. bring next_fs from the right(50%)
        left = (now * 50) + "%";
        //3. increase opacity of next_fs to 1 as it moves in
        opacity = 1 - now;
        current_fs.css({ 'transform': 'scale(' + scale + ')' });
        next_fs.css({ 'left': left, 'opacity': opacity });
    },
    duration: 800,
    complete: function () {
        current_fs.hide();
        animating = false;
    },
    //this comes from the custom easing plugin
    easing: 'easeInOutBack'
});};
  • 1
    Mind to post your MyFunc() definition? – Hatjhie Sep 18 '14 at 02:13
  • $(".next").click(function() this was function I changed to MyFunc(). its posted above !! – Raveen Mobile Sep 18 '14 at 02:14
  • did you have any element with class `next`? you omit the class `next` on your new `button` – Kyojimaru Sep 18 '14 at 02:16
  • yes, I omitted the class next bcz I changed the function. from $(".next").click(function() to function MyFunc() , because I wanted the function to be called in the codebehind button click event. – Raveen Mobile Sep 18 '14 at 02:20
  • if you only put the function definition of $(".next").click to MyFunc(), then what happened with the declaration? var current_fs, next_fs, previous_fs; //fieldsets var left, opacity, scale; //fieldset properties which we will animate var animating; //flag to prevent quick multi-click glitches It should throw an error for variable is undefined? – Hatjhie Sep 18 '14 at 02:20
  • no it doesnt throw any errors !! but I tried, putting those declarations inside the function definition also. still no change !! – Raveen Mobile Sep 18 '14 at 02:22
  • interesting. But I couldn't see how your variable is being initialized here. For example, if (animating) return false; animating = true; How this animating variable being assigned? – Hatjhie Sep 18 '14 at 02:24
  • yes sir, even I was wondering abt dt. is it because of this ?? Page.ClientScript.RegisterClientScriptInclude(GetType(), "MyScript", "Easying.js"); Page.ClientScript.RegisterClientScriptInclude(GetType(), "MyScript", "Reg.js"); Page.ClientScript.RegisterStartupScript(GetType(), "MyScript", "MyFunc()", true); – Raveen Mobile Sep 18 '14 at 02:31
  • perhaps it's because it's missing the `semicolon` in the `javascript` statement. Try this `Page.ClientScript.RegisterStartupScript(GetType(), "MyScript", "MyFunc();", true);` here's the [reference](http://stackoverflow.com/questions/4994040/scriptmanager-registerstartupscript-code-not-working-why/4994129#4994129) – Kyojimaru Sep 18 '14 at 02:53
  • Ok, let's do experiment. Remove all your current MyFunc definition and put only alert("Test"); If it's registered correctly, the alert box would show up and that means nothing wrong with the RegisterClientScript function. – Hatjhie Sep 18 '14 at 03:09
  • ok !! (is this correct ?? no I dont get the alert box) Page.ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Test')", true); – Raveen Mobile Sep 18 '14 at 03:19
  • also, as you can see, MyFunc() is used to change from one fieldset to another. my aspx page has 3 fieldsets. and a progress bar (1--2--3). The progress bar should move from one to the other. and the fieldset also shud change. the current situation is I see a the progress bar move from 1 to directly 3. and no change in the fieldset. – Raveen Mobile Sep 18 '14 at 03:22
  • @Raveen did you write in the code like this **"alert('Test')"** or **"alert('Test');"**, I don't see any semicolon `;` in your code that call the `javascript`, and you can mention the other commenter to notify them, or else they won't know if you're replying to them – Kyojimaru Sep 18 '14 at 05:27
  • @Kyojimaru yes!! sorry !! yes this works !! kindly see my original post I have added an update. Page.ClientScript.RegisterStartupScript(GetType(), "MyScript", "alert('test');", true); – Raveen Mobile Sep 18 '14 at 06:19
  • @Hatjhie . yes the alert box shows up. – Raveen Mobile Sep 18 '14 at 06:21
  • That means, you need to focus on your MyFunc() logic. The RegisterStartUpScript is working fine. If possible, please post your MyFunc() logic after the modification. – Hatjhie Sep 18 '14 at 07:11
  • And also the result `HTML` with your markup if possible, and check the result of `current_fs` and `next_fs` before you `show` the `next_fs` – Kyojimaru Sep 18 '14 at 07:24
  • @Hatjhie, Ive updated my question!! u'll see the edited MyFunc() at the bottom. I hope you saw this also, this is where I got the code from. http://codepen.io/atakan/pen/gqbIz – Raveen Mobile Sep 18 '14 at 08:01
  • @Kyojimaru, am sorry. am not really sure how to do that !! kindly explain pls. thank u ! – Raveen Mobile Sep 18 '14 at 08:03
  • try adding this line `console.log(current_fs);` and `console.log(next_fs);` after `next_fs = $(this).parent().next();`, then see in the console in the `developer tools`, press F12 to open it, and see what's logged there, what I see is it should be `undefined` though – Kyojimaru Sep 18 '14 at 09:22
  • @Kyojimaru. yes u r correct. Object { length: 0, prevObject: Object, context: undefined } Reg.js:20 Object { length: 0, prevObject: Object, context: undefined } Reg.js:20 – Raveen Mobile Sep 18 '14 at 10:15

2 Answers2

0

it won't work because of your code here

current_fs = $(this).parent();
next_fs = $(this).parent().next();

$(this) in MyFunc() isn't the button that's being clicked like when you use $(".next").click(function() { ... }, so your code isn't working as expected like when you use the click function using jQuery, for now you can overcome it by sending the class for the current and next item using the parameter in the MyFunc(), so you can write it like this

function MyFunc(curClass, nxtClass) {
    // your code ...

    current_fs = $(curClass);
    next_fs = $(nxtClass);

    // your code...
}

it's not tested yet, but you should be able to do it with this logic.

Kyojimaru
  • 2,694
  • 1
  • 14
  • 22
  • thank you very much for finding the problem. But am not really sure wt parameters to be passed for curClass n nxtClass from codebehind. !! – Raveen Mobile Sep 18 '14 at 11:01
  • ah ! I gave my aspx fieldsets id's and make the runat=server. then I passed the id's of the fieldsets for MyFunc(f1,f2) in the code behind. !! it works !! thank you very much !! – Raveen Mobile Sep 18 '14 at 11:09
  • oh !! it works fine in firefox. But internet explorer gives me a runtime error. Unhandled exception at line 128, column 1 in http://localhost:36678/RegistrationSeeker.aspx 0x800a1391 - JavaScript runtime error: 'f1' is undefined – Raveen Mobile Sep 18 '14 at 11:29
0

Your issue is that you are mixing up your variable scopes. You're function MyFunc() is trying to set current_fs and next_fs as such:

current_fs = $(this).parent();
next_fs = $(this).parent().next();

But the object which the 'this' keyword refers to will change from when it is used in the context of when the function is called from a click event, where it will refer to the button that has been clicked, versus when it is called from your code behind method via the Page.ClientScript.RegisterStartupScript() method, where it will refer to some other object which probably has no use to you.

You will have to therefore make the function correctly identify the current fieldset and next fieldset using a different method.

My suggestion however is to change your implementation (if possible) altogether so that the final submit button is the only postback action, as it is a simpler solution that will not only result in smoother user experience but also result in less traffic between the client and server.

Adrian Sanguineti
  • 2,455
  • 1
  • 27
  • 29