Link to home
Start Free TrialLog in
Avatar of AZZA-KHAMEES
AZZA-KHAMEESFlag for Bahrain

asked on

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?
Avatar of Lokesh B R
Lokesh B R
Flag of India image

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

Avatar of AZZA-KHAMEES

ASKER

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
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?
i tried it in sample page and its working
but from my web application its not, note that hdnfldVariable.Value is HiddenField
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..!!
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.
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

ASKER CERTIFIED SOLUTION
Avatar of Lokesh B R
Lokesh B R
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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