[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
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
Medium Priority
?
1,008 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
ID: 22729056
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 2000 total points
ID: 22730286
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
ID: 31506635
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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

834 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