Am using the below code to display the message details. Its work in all pages and it doesn't work in some places. The below script does't work when am using calendar render event.Please help me to fix this error..
My partial code is here:
finally
{
if (message != "")
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "<script language='javascript'>alert('List : \\n" + message + "');</script>", false);
}
}
i think its may be because of no value in you 'message' variable. please try once by commenting the 'if' block/condition.
Related
I tried searching the internet for ways to display a javascript alert box in asp.net, but none of them worked.
My code:
string message;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
if ((int) Application["taken"] == 1)
{
message = "Username and email already in use!";
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Username and email already in use!')", true);
}
else if ((int) Application["taken"] == 2)
{
message = "Username already in use!";
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Username already in use!')", true);
}
else if ((int) Application["taken"] == 3)
{
message = "Email already in use!";
sb.Append("<script type = 'text/javascript'> window.onload=function(){alert('" + message + "')} </script>");
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
}
what do I need to do?
You cannot call script twice, instead write code for alert twice in same script.
You can write your javascript method at your aspx page or can write in a javascript file and include it. Then register it at cs page as
ScriptManager.RegisterStartupScript(this, this.GetType(), “ShowStatus”, “javascript:ShowMessages(‘First Message’,’Second Message’);”, true);
function ShowMessages(msg1, msg2) {
alert(msg1);
alert(msg2);
}
For details please click on this link
Could you try RegisterStartupScript ?
ClientScript.RegisterStartupScript(this.GetType(), "alertMessage", "alert('Username already in use!')", true);
I have a asp FileUpload control in my aspx page.
And the code behind to check file's extension and then insert them into database:
private string Write(HttpPostedFile file, string table)
{
string fileNameMain = Path.GetFileName(file.FileName);
// check for the valid file extension
string fileExtension = Path.GetExtension(fileNameMain).ToLower();
if (fileExtension.Equals(".pdf") || fileExtension.Equals(".doc"))
{
...insert fileupload into database
}
else
{
LabelError.Text = "Extension of files are not allowed.";
return null;
}
}
With the code above, it checks the extension and then show the message "Extension of files are not allowed." in a LabelError if the fileupload is not allowed.
Now, I'm required to do the "check-extension" in the other way: at the moment the client click and choose FileUpload, an alert show "Extension of files are not allowed.".
I need a way to make an alert show at the momment the FileUpload choosed in browser. Help!!!!
Hi but if you want an Alert (JavaScript) but first you need an postback action to execute code vb and the alert for example you could use a button to execute this method. You can show alert with this code
string script = "alert('Extension of files are not allowed');";
ScriptManager.RegisterStartupScript(this, typeof(Page), "alert", script , true);
Good day so here is my code
Page.ClientScript.RegisterStartupScript(this.GetType(), "messagebox", "<script>$(document).ready( function() { csscody.alert('<br/><h1> Exception</h1><br/>The file that you have selected has Invalid/No matching Branch Code in our Database'"+Message+"',{onComplete: function(e){if(e){process();__doPostBack('ctl00$ContentPlaceHolder1$btndelete','');}}});return false;});</script>", false);
The problem is when i put the Variable Message inside the pop up doens't show (maybe its a syntax error) but when i remove it it shows as usall, so how would i add a text/String from C# to the code above? the Message variable contains this text
String Message = 123123 <br/> 22222 <br/> 1233 <br/> 33123 <br/>
You are closing your single quotes before you append the Message string. (Actually I think it's a stray single quote.) Try:
...anch Code in our Database" + Message + "'...
try
Page.ClientScript.RegisterStartupScript(this.GetType(), "messagebox", "<script>$(document).ready( function() { csscody.alert('<br/><h1> Exception</h1><br/>The file that you have selected has Invalid/No matching Branch Code in our Database\\''"+Message+"\\',{onComplete: function(e){if(e){process();__doPostBack('ctl00$ContentPlaceHolder1$btndelete','');}}});return false;});</script>", false);
I am having a web page and i applied script manager on it. On the page load i am checking some condition and showing a Javascript ShowModalDialog from code and it is working fine.
int isAnyNonProdTaskActive = _nonProduction.IsAnyTaskActive(UserIDFromDB);
if (isAnyNonProdTaskActive > 0)
{
//Displays and Logs Message
_loggerDetails.LogMessage = "EmployeeQuotient.Production.Page_Load() One NonProduction incomplete task found, NonProductionTimeEntryID : " + isAnyNonProdTaskActive.ToString();
_writeLog.LogDetails(_loggerDetails.LogLevel_Info, _loggerDetails.LogMessage);
Session["TaskActiveNonProd"] = isAnyNonProdTaskActive;
Page page = HttpContext.Current.CurrentHandler as Page;
//Displays and Logs Message
_loggerDetails.LogMessage = "EmployeeQuotient.Production.Page_Load() Opening ElapsedClockNonProd.aspx to complete the incomplete task id :" + isAnyNonProdTaskActive.ToString();
_writeLog.LogDetails(_loggerDetails.LogLevel_Info, _loggerDetails.LogMessage);
ScriptManager.RegisterStartupScript(page, page.GetType(), "OpenModalDialog", "<script type=text/javascript>window.showModalDialog('ElapsedClockNonProd.aspx?code=" + isAnyNonProdTaskActive.ToString() + "', null, 'unadorned:yes ;resizable:0 ;dialogWidth:300px ;dialogHeight:300px ;status:no ;scroll:no ;status=no;'); </script>", false);
}
The same code i am applying on a Button on the same page then i am getting error.The error message is :- Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: The script tag registered for type 'ASP.associates_production_aspx' and key 'OpenModalDialog' has invalid characters outside of the script tags: window.showModalDialog('ElapsedClockNonProd.aspx?code=464', null, 'unadorned:yes ;resizable:0 ;dialogWidth:300px ;dialogHeight:300px ;status:no ;scroll:no ;status=no;'); . Only properly formatted script tags can be registered.
Guide me that how i fix this error.I am not getting that why on the button click it is not working but while page load the same script is working without any issues..
ScriptManager.RegisterStartupScript(page, page.GetType(), "OpenModalDialog", "<script type='text/javascript'>window.showModalDialog('ElapsedClockNonProd.aspx?code=" + isAnyNonProdTaskActive.ToString() + "', null, 'unadorned:yes ;resizable:0 ;dialogWidth:300px ;dialogHeight:300px ;status:no ;scroll:no ;status=no;'); </script>", false);
I have the following code, but the alert box is not displaying.
try
{
do something..
}
catch(Exception ex)
{
Response.Write("<script>alert('"+ex+"')</script>");
}
If I use this code, the alert box appears.
try
{
do some thing
}
catch (Exception ex)
{
Response.Write("<script>alert(\"an error occur\")</script>");
}
How can I display the exception variable in an alert box?
If you want to show the stacktrace:
Response.Write("<script>alert('"+ Server.HtmlEncode(ex.ToString()) + "')</script>");
or if you want only the message
Response.Write("<script>alert('"+ Server.HtmlEncode(ex.Message) + "')</script>");
Try something like
Response.Write("<script>alert('"+ex.Message+"')</script>");
Have a look at the class Exception Class
Dim message = New JavaScriptSerializer().Serialize(rs)
Dim script = String.Format("alert({0});", message)
ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "", Script, True)
Please check whthr you r using update panel in that page.It may sometimes work if the update panel is there.
You need to be careful and properly escape the Javascript string you are generating ... Imagine there are single quotes in the Exception's message ...
Single-quotes (') need to be escaped (\')
Response.Write("<script>alert('"+ Server.HtmlEncode(ex.Message).Replace("'","\\'" ) + "')</script>");
This solved my problem:
string jscriptCustInfo = "<script type='text/javascript' language='javascript'>";
jscriptCustInfo = jscriptCustInfo + "alert('Dividend Posting Done, Batch No: "+lblBatch.Text+"');";
jscriptCustInfo = jscriptCustInfo + "</script>";
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", jscriptCustInfo, false);