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

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.
jjthorneAsked:
Who is Participating?
 
tiagosalgadoConnect With a Mentor Commented:
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
 
jinn_hnnlCommented:
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
 
jjthorneAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.