modify alert message of script manager - javascript

Is there any way I can modify "alert('error !') part of the script manager to include message in the label below? I don't want to use the label for that, just display database error in the pop up window. I tried adding it with + but either it doesn't work or the pop up isnt displayed at all. Thanks
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('error !') ", true);
lblError.Text = "A database error has occured. <br /> <br />" +
"Message: " + ex.Message;
It doesnt like this:
<!-- all the links for datetime picker -->
<link rel="stylesheet" type="text/css" href="../style/jquery.datetimepicker.css"/ >
<script src="../Scripts/jquery.js"></script>
<script src="../Scripts/jquery.datetimepicker.js"></script>
<script>
$(function() {
$( "#datetimepicker" ).datetimepicker();
});
</script>

You can create a javascript function and call it to change the text of any label you want.
My suggestion is:
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", " changeLabel('" + ex.Message + "', '" + lblError.ClientID +"'); alert('error !'); ", true);
And the javascript function :
function changeLabel(text, id){
$("#"+id).val(text);
}

try
{
..
}
catch(Exception ex)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Database error ocurred : "+ ex.Message.ToString()+"')", true);
}

Unfortunately you cannot add HTML tag in javascript alert message.
Simple Alert
Make sure you strip out the single quote ' in ex.Message.
try
{
throw new Exception("test");
}
catch (Exception ex)
{
string message = string.Format("alert('A database error has occured. {0}');",
ex.Message.Replace("'", ""));
ScriptManager.RegisterStartupScript(this, this.GetType(),
"alert" + UniqueID, message, true);
}
jQuery Dialog
However you can use jQuery Dialog if you want to insert HTML.
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<div id="dialog" title="Basic dialog">
<div id="dialog-text"></div>
</div>
try
{
throw new Exception("This is an error message.");
}
catch (Exception ex)
{
string message = string.Concat("$(function () {$('#dialog').dialog(); " +
"$('#dialog-text').html('A database error has occured. " +
"<br /> <br />", ex.Message, "');});");
ScriptManager.RegisterStartupScript(this, this.GetType(),
"alert" + UniqueID, message, true);
}

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "<script language='javascript'>alert('A database error has occured.\n\n"+ ex.Message +"' );</script>", true);

Related

Redirect to popup window after alert message show using asp.net?

I want to show alert message after alert message confirmed i want to display another page .That page url is url="var params = ['height='+screen.height,'width='+screen.width,'fullscreen=yes'].join(',');var popup = window.open('ReportViewer.aspx?Option=DeliveryOrder1&DONO=HQ1700000149&Report=SalesInvoice&CustomerName=PUAN+YATI&Address1=NO 1179 PERSIARAN PUTERI 3/2 DSN,&Address2=PUTERI 3/2 TAMAN CHANDAN PUTERI, KUALA KANSAR PERA&Country=MALAYSIA&Phone= &ZipCode=33000 &subtotal=14.00&totalvalue=14.00&gst=0.00&TotalDiscount=0.00&NetTotal=14.00', '_blank', params);popup.moveTo(0, 0);"
my code:
protected void btn_CreatePo_Click(object sender, EventArgs e)
{
if (result == "True")
{
string url = btn_poprint();//edit23/06
ClientScript.RegisterStartupScript(this.GetType(), "Alert", "Alert_CodeBehind(' Delivery Order " + txt_PrintDO.Text + " is Successfully Converted to PO ');window.location='"+ url +"' ", true);
return;
}
else
{
// ScriptManager.RegisterStartupScript(this, this.GetType(), "Hide", "<script> document.getElementById('li_ListView').style.display = 'block';</script>", true);
ScriptManager.RegisterStartupScript(this.Up_Detail, typeof(string), "Alert", "Alert_CodeBehind('Error in Converting ')", true);
ClientScript.RegisterStartupScript(GetType(), "test", "<script>document.getElementById('a14').className = 'active_tab'</script>");
return;
}
}
I want to display alert message after alert message confirmed the given url page will be show.

Call alert after response.end

Here is my code where I am trying to show alert after response. But no os alert is showing
string filepath = ConfigurationManager.AppSettings["USPPath"].ToString() + urlPDF;
FileInfo file = new FileInfo(Server.MapPath(filepath));
if (file.Exists)
{
ClientScript.RegisterStartupScript(this.GetType(), "somekey", "alert('Some data missing!');", true);
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/pdf";
Response.TransmitFile(file.FullName);
try
{
Response.Flush();
ClientScript.RegisterStartupScript(this.GetType(), "somekeyqw","alert('Some data missing!'); ", true);
// DisplaySucessAlert(true, "Shipping Label downloaded successfully.");
// ScriptManager.RegisterStartupScript(this, this.GetType(), "Popalertxs", "normalalert();", true);
}
finally
{
// DisplaySucessAlert(true, "Shipping Label downloaded successfully.");
// ScriptManager.RegisterStartupScript(this, this.GetType(), "Popalert", "normalalert();", true);
}
}
I have used update panel and the html code look likes
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="btnuspsgenerate" class="btn-def" runat="server" Text="Download USPS label" OnClick="btnuspsgenerate_Click" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnuspsgenerate" />
</Triggers>
</asp:UpdatePanel>
My pdf file gets download but not showing alert.
Here I had used many ways but not able to show alert.
Some of the code I have commented as they where not working
When you use update panel then you can not call javascript like this ..
Try Following Code,
string CloseWindow;
CloseWindow = "alert('Hello World')";
ScriptManager.RegisterStartupScript(UpdatePanelID,UpdatePanelID.GetType(), "CloseWindow", CloseWindow, true);

Run javascript on button press ASP.NET

I would like to call javascript function in case something in my code behind would happen.
If it is like the code below it works fine after postback alert windows shows and says it correctly. But in case I remove the comment from else block none of those two scripts in else block will happen?
Is there any limit on how many of these action could I make from codebehind?
if (condition) {
if (condition2) {
var message = "It happened !";
Page.ClientScript.RegisterStartupScript(this.GetType(), "yep1", "alert('" + message + "')", true);
}
} else {
var msg = "It does not work like that";
Page.ClientScript.RegisterStartupScript(this.GetType(), "nope1", "alert('" + msg + "!')", true);
//Page.ClientScript.RegisterStartupScript(this.GetType(), "nope2", "alert('" + msg + "')", true);
}
This way it will work. As the name of the function says, it register the startup script so you are changing it instead of inserting 2. In this way it will do both ^^
if (condition)
{
if (condition2)
{
var message = "It happened !";
Page.ClientScript.RegisterStartupScript(this.GetType(), "yep1", "alert('"+message+"');", true);
}
}
else
{
var msg = "It does not work like that";
Page.ClientScript.RegisterStartupScript(this.GetType(), "nope1", "alert('"+msg+"!'); alert('" + msg + "');", true);
//Page.ClientScript.RegisterStartupScript(this.GetType(), "nope1", "alert('"+msg+"!')", true);
//Page.ClientScript.RegisterStartupScript(this.GetType(), "nope2", "alert('" + msg + "')", true);
}

Alert before redirect showing value with message in asp

I am trying to display a string along with the message.But i get an error.
str1 is a value retrieved from a table
the code is
ScriptManager.RegisterStartupScript(this, this.GetType(), "redirect", "alert('Welcome'); window.location='" +
Request.ApplicationPath + "#';", true);
but i want to display it as follows
ScriptManager.RegisterStartupScript(this, this.GetType(), "redirect", "alert('Welcome '" + tbEmail.Text + "''); window.location='" +
Request.ApplicationPath + "#';", true);
I'm just a beginner,any help is appreciated ,thank you

alert not showing exception variable in asp.net

I want to show exception in alert box in asp.net 4. But it is not showing any alert.
I tried these solutions -
try{// my code}
catch (Exception ex)
{
//Response.Write("<script>alert('" + Server.HtmlEncode(ex.Message) + "')</script>"); // Not Working
//Response.Write("<script>alert('" + ex.Message + "');</script>"); // Not working
// Response.Write("<script>alert('" + Server.HtmlEncode(ex.ToString()) + "')</script>"); // Not working
//ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "done", "alert('Error Occured.');", true); // Only this is working.
}
Please suggest any solution to this where I am going wrong.
Thanks and regards,
Rizwan Gazi.
Normally Response.Write(); always works. But as you said its not working than
Try this :
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "done", "alert('" + ex.Message.Replace("'", "") + "');", true);
Let me know if it solves your issue.

Categories

Resources