Displaying alert message from code-behind in ASP.NET

hi Experts
i am trying to execute an alert from code behind
i used the same example from
Displaying alert message from code-behind in ASP.NET

but istead of a button i want to use the same code from an if statement

public void Alert(Page page, string message)
        {
            string jsString = "alert('" + message + "');";
            ScriptManager.RegisterStartupScript(page, page.GetType(),
                    "MyApplication",
                    jsString,
                    true);
        }
protected void InsertCar(object sender, GridRecordEventArgs e)
        {
            ---some code----

            string variable = hdnfldVariable.Value;
            if (variable == null || variable == "")
            {                
                Alert(this.Page, "Hello");               
            }
            else
            {
                --some DB code--
            }

        }

Open in new window


but its not working , what i am doing wrong
any advice?
LVL 1
AZZA-KHAMEESAsked:
Who is Participating?
 
Lokesh B RDeveloperCommented:
Hi,

try Response.Write, it will write on the top left of the Page

protected void InsertCar(object sender, GridRecordEventArgs e)
        {

            string variable = hdnfldVariable.Value;
            if (!string.IsNullOrEmpty(variable))
            {
                Response.Write("Value Found..!!");
                 return;
            }
            else
            {
                Response.Write("No Value..!!");
                 return;
            }

        }

Open in new window

0
 
Lokesh B RDeveloperCommented:
Hi,

try this

 protected void InsertCar(object sender, GridRecordEventArgs e)
        {

            string variable = hdnfldVariable.Value;
            if (!string.IsNullOrEmpty(variable))
            {
                Alert("Value Found..!!");
            }
            else
            {
                Alert("No Value..!!");
            }

        }

Open in new window



public void Alert(string message)
        {
            string script = "alert('" + message + "');";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", script, true);
        }

Open in new window

0
 
AZZA-KHAMEESAuthor Commented:
thank you for the reply, i tried this code but still i am not getting the alert.
the function is being called but i am not getting the alert
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Lokesh B RDeveloperCommented:
Hi,

did you keep break point and see whether it is calling the function or not?
try that in a sample page.

can you post the full code?
0
 
AZZA-KHAMEESAuthor Commented:
i tried it in sample page and its working
but from my web application its not, note that hdnfldVariable.Value is HiddenField
0
 
Lokesh B RDeveloperCommented:
Yes, I got that as HiddenField.

Can you add the alert to else condition as well and try, as i had posted previously.

I think something on your page is stopping..!!
0
 
AZZA-KHAMEESAuthor Commented:
i added in else condition and still the same, i trace the code using break point and i found that the function is being called but the alert is not showing.
0
 
AZZA-KHAMEESAuthor Commented:
i even tried to display a label with error messages from code behind also its not appear

System.Web.UI.WebControls.Label lblError = new System.Web.UI.WebControls.Label();
                lblError.Text = "hello";
                this.Controls.Add(lblError);

Open in new window

0
 
AZZA-KHAMEESAuthor Commented:
it not working. the page get stuck
i think its something related to the component i am using, [Obout], the aler() is working when i am calling the procedure using asp buttons but from Obout grid insert command its not working.
0
 
AZZA-KHAMEESAuthor Commented:
also i used
MessageBox.Show("Please select the emplyee cprno");

Open in new window

and its working in if condition when i run my application from visual studio but when i publish the application to the localhost server the MessageBox is not working from the server, that why i changed the code to ScriptManager.RegisterStartupScript
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.