Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

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,001 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

Industry Leaders: 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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

636 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