?
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
?
994 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

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