1

I am inside an event and inside this event I want to have a javascript alert display a message to a user. But I cannot seem to get this to work.

protected void dgvStaff_Deleting(object sender, Infragistics.Web.UI.GridControls.RowDeletingEventArgs e)
        {
            // Code stub
            object test = e.Row.Items[0].Text;
            //ScriptManager.RegisterStartupScript(this, this.GetType(), "alertbox", "ShowPopup('Select a row to rate');", true);
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertbox", "ShowPopup('Select a row to rate');", true); 
            if (objGatewayFunctions.CheckStaffAssignment(e.Row.Items[0].Text.ToString(), ConfigurationManager.AppSettings.Get("Connection").ToString()) == true)
            {

            }
        }

Any idea what I am doing wrong here?

1
  • 8
    You do realize that all the C# code runs on the server, and the JavaScript runs on the client only after the server code has finished executing completely, and generated and pushed the resulting HTML code to the client's browser? You can't have live interaction between the server code and the client in the middle of methods with asp.net
    – mellamokb
    Commented Sep 27, 2012 at 16:58

3 Answers 3

6

You can't do that - C# and JavaScript run on different computers and you can't jump between them in the middle of a function.

Normally you delay errors/warnings till page is rendered. To improve user experience also try to do error checking before posting back to server (in addition to server side checks).

1
  • So if an error came up. Is my only option to show it in exposing the error message to a label? Is there no way to expose it to a javascript message? Basically the method checks to see if the user in the datagrid can be deleted, if they can't I need to display the reason why it can't be deleted. I can do it easily in a label, but wanted a pop up option. Commented Sep 27, 2012 at 17:30
0

You have to wait for the completion of Server execution, then you may able to see the Client side execution in action.

0

Use this

Response.Write("<script type='text/javascript'>alert('Hello World');</script>");
1
  • This did the same as the other two, no errors but no pop up either. =( Commented Sep 27, 2012 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.