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

I used the following code to display alert message but i am not getting any thing Please check this
C#
string strMsg = "Leave has been applied already between " + txtFromDate.Text.Trim() + " and " + txtToDate.Text.Trim() + "";
                        string script = "<script language=\"javascript\" type=\"text/javascript\">alert('" + strMsg + "');</script>";
                        Response.Write(script);
Posted
Updated 1-Feb-12 0:52am
v2
Comments
Vani Kulkarni 10-Jul-12 2:18am    
Shashwath, I suppose you have received many solutions, please mark them as answers and close this question.
Rahul Rajat Singh 10-Jul-12 5:33am    
I agree with Vani.

check This .

C#
Page.RegisterStartupScript("Key", "<script type='text/javascript'>window.onload = function(){alert('Please accept Terms and Conditions before submitting.');return false;}</script>");
 
Share this answer
 
Sort out your double quotes:
C#
string script = "<script language="\""javascript\"
Becomes
C#
string script = "<script language=\"javascript\"
 
Share this answer
 
v2
modify as

C#
string strMsg = "Leave has been applied already between " + txtFromDate.Text.Trim() + " and " + txtToDate.Text.Trim() + "";
                     string script = "<script language=\"javascript\" type=\"text/javascript\">alert('" + strMsg + "');</script>";
                     Response.Write(script);


Note: Try to show alert message from javascript without postback
 
Share this answer
 
although all the answers given here are correct, you might find this article interesting.

this gives you a reusable class to do the same thing.

A Windows Form like MessageBox for ASP.NET Website[^]
 
Share this answer
 
Comments
Vani Kulkarni 10-Jul-12 2:17am    
Good answer! 5!
Rahul Rajat Singh 10-Jul-12 2:22am    
thanks.
i hope, it will work

C#
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

 ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Data Insert Successfully...');", true);
 
Share this answer
 
v2
C#
string strMsg = "Leave has been applied already between " + txtFromDate.Text.Trim() + " and " + txtToDate.Text.Trim() + "";
        string script = "<script language="\"javascript\"" type="\"text/javascript\"">alert('" + strMsg + "');</script>";
        Response.Write(script);
 
Share this answer
 
As OriginalGriff suggested you have missed out the string formatting. Below is working.

C#
txtFromDate.Text = DateTime.Now.AddDays(-2).ToString("MM/dd/yyyy");
            txtToDate.Text = DateTime.Now.AddDays(2).ToString("MM/dd/yyyy");
            string strMsg = "Leave has been applied already between " + txtFromDate.Text.Trim() + " and " + txtToDate.Text.Trim() + "";
            string script = "<script language=\"javascript\" type=\"text/javascript\">alert('" + strMsg + "');</script>";
            Response.Write(script);
 
Share this answer
 
C#
ClientScript.RegisterStartupScript(this.GetType(), "key", "<script>alert('user created')</script>");



Hope this may help you..
 
Share this answer
 
Comments
Mithun Patra 14-May-12 4:46am    
how to change the Title of this alert windows/message box???
XML
ss="100";
ss = ss.Insert(0, "Succesfully added the voucher entry! Voucher No ");

             ScriptManager.RegisterStartupScript(this, this.GetType(), "", "<script>alert('" + ss.ToString() + "');</script>", false);



Hope this may help you..
 
Share this answer
 
v2
C#
string strMsg = "Leave has been applied already between " + txtFromDate.Text.Trim() + " and " + txtToDate.Text.Trim() + "";
Response.Write("<script>alert('"+strMsg +"')</script>");


Hope this may help you..
 
Share this answer
 
v2
Comments
Rajasri Kannan 28-Sep-15 14:00pm    
Response.Write("<script>alert('"+strMsg +"')</script>");

this works perfect for displaying an output result in alert box.

Thanks.
 
Share this answer
 
To make your web application efficient you can add one class named MessageBox in your AppCode folder.
please cheak this link

www.codeproject.com/KB/webforms/AspNetMsgBox.aspx[^]
Sorry to say but before posting questions you must check the prev posts as there are many posts asking same question
 
Share this answer
 
XML
string strMsg = "Leave has been applied already between " + txtFromDate.Text.Trim() + " and " + txtToDate.Text.Trim() + "";
                        string script = "<script language="\""javascript\" type=\"text/javascript\">alert('" + strMsg + "');</script>";
                        Response.Write(script);


problem in wrong used of "" please sort out it.
 
Share this answer
 
XML
string strMsg = "Leave has been applied already between " + txtFromDate.Text.Trim() + " and " + txtToDate.Text.Trim() + "";
                        string script = "<script language="\""javascript\" type=\"text/javascript\">alert('" + strMsg + "');</script>";
                        Response.Write(script);


problem in wrong used of "" please sort out it. and either you can used messageBox to display your message in alert .
 
Share this answer
 
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Write Your Message');", true);
 
Share this answer
 
Comments
ZurdoDev 20-Mar-15 10:13am    
Please do not answer questions that are very old and are very answered already. Your account will get flagged as abusive and closed. You did not add any value to this question.

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



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