Click here to Skip to main content
16,020,673 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have a javascript for confirm that assigns a value to an asp hidden field
XML
function ConfirmBox(msg) {
    var x;
    if (confirm(msg))
    {
        document.getElementById('<% =inpHide.ClientID %>').value = "1";
        //return true;
    }
    else
    {
        document.getElementById('<% =inpHide.ClientID %>').value = "0";
        //return false;
    }

}


it will be called to from codebehind using this:

C#
ClientScriptManager script = Page.ClientScript;

                            script.RegisterStartupScript(this.GetType(), "Error", "ConfirmBox('ADS Keymetrics General report already exists for this account for Q" + ADSImport.Upload.QTo + " " + ADSImport.Upload.YrTo + ". Do you want to overwrite the record?');", true);
                            
                            int DiagResult = int.Parse(inpHide.Value);
                            if (DiagResult == 1)
                            { ClientScript.RegisterClientScriptBlock(this.GetType(), "Error", "MessageBox('OK');", true); }



the confirm box won't pop-up and when i trace the code, it just passes by the RegisterStartupScript code and goes directly to
C#
int DiagResult = int.Parse(inpHide.Value);
that's why it gives me an error: "Input string was not in a correct format." since the variable inpHide has a null value.

any help is greatly appreciated.

Thanks!
Posted
Comments
bbirajdar 1-Oct-12 11:57am    
You cannot CALL javascript from server side.. However, you can INITIALIZE javascript from server-side using the above procedure..

1 solution

Well, you inject a JavaScript code and then instantly try to access (on server side) value of one of the controls involved. How will it work?

Injecting JavaScript will not set the value. A call to ConfirmBox() will set the value and post that whenever you try to access that control value on server side, you will get back the set value.

Either, move your logic to a proper place or set a default value of inpHide such that there is no error and it can go on smooth.
 
Share this answer
 
Comments
Franco Cipriano 1-Oct-12 11:36am    
The line before the variable declaration of DiagResult calls the ConfirmBox() where I can assign the value of the inpHide through the javascript function. But in this case, the ConfirmBox() does not execute..but when I put the ConfirmBox() function on the onClientClick event of the button it pops-up..any idea??

thanks,
Sandeep Mewara 1-Oct-12 11:46am    
Page is not yet complete. Once the code is executed, page is constructed. Your code would not work as you are trying to. Going with onClientclick or so would be correct way.
Franco Cipriano 1-Oct-12 12:44pm    
Yes, that's why I put it on the codebehind since I need to have a dialog box that will pop-up and let the user know if they want to continue of not.

Thanks!
Franco Cipriano 1-Oct-12 13:24pm    
i used ClientScriptManager.RegisterClientScriptBlock (Type typeofscript, String key, String script, true)..to initialize a javascript alert dialog box and it worked..im not sure why it does not let the confirm box pop-up..
Franco Cipriano 3-Oct-12 11:56am    
When doing some more debugging i found out something on the the code..instead of popping up a dialog box when the code passes on the clientscript line..what it does is passes on the clientscript block, and continue until the function is done and pops up a dialog box..am I doing something wrong in the code?

Thanks,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900