Solved

Calling a Javascript function in the middle of a c# method and reacting of the reply of the user

Posted on 2008-10-16
3
962 Views
Last Modified: 2013-11-07
i have a C# function in a ASP.net program where i have a web page with text boxes and a option so the user can select new and then the text boxes clear themselves so the user can input the new data.

What i want is a function so if the user inputs a number in the primary key field which already exist in the database which i am working with a Javascript confirm box pops up and say 'do you wish to over write that record?' and the result of the user input get passed back to the C# class and the c# class responds effectively.

i have the Javascript function in the aspx.cs file and it is being called but after the method in the C# file has completely finished, this is the coding that i have at the moment

NpgsqlDataReader dr = db.cncSelect(sqlCID);
        if (dr.Read())
            CID2 = Convert.ToInt32(dr["couponid"]);
        dr.Close();

        bool bool1 = false;
        ClientScriptManager cs = Page.ClientScript;
       
        if (CID2 != null)
        {
            //string error = "function PopWindow() {confirm('Do you want to over write that coupon');}";
            //this.Save.Attributes.Add("onclick", "java script:return confirm('Do you want to over write that coupon')");
           
            //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "My Scrypt", error, true);
            //Response.Write("<script language='JavaScript>Confirm('Do you want to over write that coupon');</script>");
            Save.Attributes.Add("onclick", "ConfirmOverwrite();");
            //cs.RegisterStartupScript(this.GetType(), "ConfirmOverwrite", "ConfirmOverwrite()", true);
        }

        if(bool1 == true)
        {
         ......
        }

function ConfirmOverwrite()
{
    var cc = "";
    if(confirm('Want to continue'));
    {
        cc = document.getElementById("CouponConfirm");
        document.getElementById("MyHiddenField").value=cc.value+"(SENT BACK)";
    }
    else
    {
        return false;
    }
   
}

so if the user select yes the bool1 will be made true and the code in that if statement will be executed. if its false as the user as selected no it will pass that and do the else.
0
Comment
Question by:jjthorne
3 Comments
 
LVL 10

Expert Comment

by:jinn_hnnl
Comment Utility
As soon as the page is posted back to server and processing. You can not tell them to stop, render back to client side to ask for confirmation and come back to server to continue. They will finish what they are doing and as the server side done, they render to client. (by then it's too late).

Except you separate the 2 process, as soon as you found already exist, return the function hold there, render to client ask for confimation and post back to another method (and continue). This is not nice.

The best solution is:
Put that textbox in an update panel, set the OnTextChange Event for it, as soon as user done typing that part is process and postback. Check the existance of the record, if so, set client click for save button.

Hope this helps

JINN



<ajax:UpdatePanel ID = "up" runat="server">

    <ContentTemplate>	  

	   <asp:TextBox ID="tbInput" OnTextChanged="tbInput_TextChanged" AutoPostBack="True" runat="server" />

	   <asp:Button ID="Save"...>

    </ContentTemplate>

</ajax:UpdatePanel>
 
 

protected void tbInput_TextChanged(object sender, EventArgs e)

    {

       // ....

        string input = tbInput.Text

         ....
 

	  NpgsqlDataReader dr = db.cncSelect(sqlCID);

        if (dr.Read())

            CID2 = Convert.ToInt32(dr["couponid"]);

        dr.Close();
 

        bool bool1 = false;

        ClientScriptManager cs = Page.ClientScript;

       

        if (CID2 != null)

        {

            Register the client script for your save button now.

	    

	    .... the rest

    }

Open in new window

0
 
LVL 8

Accepted Solution

by:
tiagosalgado earned 500 total points
Comment Utility
You can do that forcing a postback after user click on confirmation dialog.
Inside a updatepanel, insert a button and put this code on click event

StringBuilder javaScript = new StringBuilder();

javaScript.Append("\n<script type=text/javascript>\n");

javaScript.Append("var success = window.confirm('Your confirm message goes here!!!');\n");

javaScript.Append("__doPostBack('ConfirmUserOption', success);\n");

javaScript.Append("</script>");
 

ScriptManager.RegisterStartupScript(btnYourButton, btnYourButton.GetType(), "confirm", javascript.ToString(), false);
 
 

###################################################
 

On Page_Load put this code

if (this.IsPostBack)

            {

                string eventTarget = (this.Request["__EVENTTARGET"] == null) ? string.Empty : this.Request["__EVENTTARGET"];

                string eventArgument = (this.Request["__EVENTARGUMENT"] == null) ? string.Empty : this.Request["__EVENTARGUMENT"];
 

                if (eventTarget == "ConfirmUserOption")

                {

                    if (eventArgument == "true")

                    {

                        // your C# Function to execute when user confirm

                    }

                }

            }

Open in new window

0
 

Author Closing Comment

by:jjthorne
Comment Utility
yeah, after adding the coding to a method and telling the code to run if the variable was true in a postback function that got it working.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now