NoviceToDotNet
NoviceToDotNet

Reputation: 10805

How to call a java script function from code behind some button click condition?

i want to open a model pop up on some button click in code behind which has got a few if else condition. First of all i am unable to fix what would be best approach. What i options i think are followign. 1) calling a jquery model pop up 2) ajax model pop up

It is a button which fix on some button click condition to open the model pop up, if model pop says yes then, i want client to rediret to some payemnt page where he will pay to purchase the item.

Right now i am using the Jquery model pop up which i am calling like this

 protected void imgClientFreeEval_Click(object sender, System.Web.UI.ImageClickEventArgs e)
    {
-----
---some code not typed
 if (SurveyCount > 1)
            {
                Session["YourAssessment"] = true;
                Session["MyAssessment"] = false;
                ScriptManager.RegisterStartupScript(this, this.GetType(), "tmp", "<script>xyz()</script>", true);
                //Response.Redirect("~/yourAssessment.aspx");

            }
}

and i have model pop up like this

 function xyz() {
            //  alert('hi tpo all');
            // a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
            //            $("#dialog:ui-dialog").dialog("destroy");
            $(document).ready(function () {
                $("#dialog-confirm").dialog({
                    resizable: false,
                    height: 140,
                    modal: true,
                    autoOpen: false,
                    buttons: {
                        "OK": function () {
                            $(this).dialog("close");
                            window.location.replace("http://stackoverflow.com");

                        },
                        Cancel: function () {
                        window.location.replace("http://stackoverflow.com");
                            $(this).dialog("close");
                        }
                    }
                });
            });
        }

Now the problem is that this function not getting called at all. what is wrong with this, i am toiling for long, if possible please suggest me best approach how should i execute it. i am unable to call this javasccipt function from code behind button click.

Upvotes: 0

Views: 2940

Answers (4)

Imran Balouch
Imran Balouch

Reputation: 2170

One problem that I noted in your code is that in the line:

ScriptManager.RegisterStartupScript(this, this.GetType(), "tmp", "<script>xyz()</script>", true);

You are passing last parameter as true, which is addScriptTags, but you are already adding the script tag in the call, so might be it is creating problem over there.

Cheers

Upvotes: 0

Krishanu Dey
Krishanu Dey

Reputation: 6406

Try using this :

function xyz() {
            //  alert('hi tpo all');
            // a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
            //            $("#dialog:ui-dialog").dialog("destroy");
                $("#dialog-confirm").dialog({
                    resizable: false,
                    height: 140,
                    modal: true,
                    autoOpen: false,
                    buttons: {
                        "OK": function () {
                            $(this).dialog("close");
                            window.location.replace("http://stackoverflow.com");

                        },
                        Cancel: function () {
                        window.location.replace("http://stackoverflow.com");
                            $(this).dialog("close");
                        }
                    }
                });
            });
        }

Upvotes: 0

Pranav
Pranav

Reputation: 8871

if you want to get alert and then redirect your page when OK is clicked. you can try like :

ClientScript.RegisterStartupScript(this.GetType(), "My alert", "alert('" Your time has been finished "');", true);
    System.Text.StringBuilder sbs = new System.Text.StringBuilder();
    sbs.Append("<script language='javascript'>");
    sbs.Append("window.location = 'http://www.google.com/'");//or whatever you want:-
    sbs.Append("</script>");
    Type tr = this.GetType();
    ClientScript.RegisterClientScriptBlock(tr, "PopupScript2", sbs.ToString());

Upvotes: 0

inksmithy
inksmithy

Reputation: 546

The method I use to get javascript alerts up from my codebehind is like this:

public void AMessage(string message)
{
    ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "info only", "alert('" + message + "');", true);
}

If you aren't using the OnClientClick event in your button, then send the message through a similar handler. In your case, instead of calling "alert('" + message + "');" call the function you have written.

Upvotes: 1

Related Questions