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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.