bootbox.alert not working from code behind - javascript

I am importing data to DB from excel using bootstrap file uploader. After data import I need to show a message to user. So I have below code
string strScript = "bootbox.alert('" + Resources.Resource.Success_SaveUserInfo + "');";
Page.ClientScript.RegisterStartupScript(this.GetType(), "UserSave", strScript, true);
But it is not working. Page get freeze instead of alert. But if I use below code it work.
string strScript = "alert('" + Resources.Resource.Success_SaveUserInfo + "');";
Page.ClientScript.RegisterStartupScript(this.GetType(), "UserSave", strScript, true);
I have already added bootbox.min.js. So it is not issue.

You can do like this
string strScript = "<script type = 'text/javascript'>bootbox.alert('" + Resources.Resource.Success_SaveUserInfo + "');</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "UserSave", strScript, true);
OR
Call javascript function it's highly recommended and reusable as well
Page.ClientScript.RegisterStartupScript(this.GetType(), "UserSave", CustomalertFunction('" + Resources.Resource.Success_SaveUserInfo + "'), true);
and write a javascript function
function CustomalertFunction(message)
{
bootbox.alert(message);
}

Related

Enabling Javascript Scripts in my C# code on IIS

I have some Javascript scripts in my ASP code that isn't working when I host it on Information. I tried enabling static content and setting Authentication to Anonymous Authentication. I don't have any separate JS files that I need to run, but .js is also enabled on MIME.
protected void GetDateCal_SelectionChanged(object sender, EventArgs e)
{
if (Request.QueryString["textbox"] != "")
{
string strScript = "<script>window.opener.document.forms[0].ctl00_ContentPlaceHolder1_" + Request.QueryString["textbox"].ToString() + ".value = '";
strScript += GetDateCal.SelectedDate.ToString();
strScript += "';window.opener.document.forms[0].submit();self.close();";
strScript += "</" + "script>";
Page.ClientScript.RegisterClientScriptBlock( this.GetType(), "Calendar_ChangeDate", strScript);
}
}
I would either use the "true" tag to automatic surround your js code with script tags.
And, any "" character in a c# string will be escaped, so use # for your strings.
eg:
string strScript = "<script>window.opener.document.forms[0].ctl00_ContentPlaceHolder1_" + Request.QueryString["textbox"].ToString() + ".value = '";
strScript += GetDateCal.SelectedDate.ToString();
strScript += "';window.opener.document.forms[0].submit();self.close();";
strScript += #"</" + "script>";
Page.ClientScript.RegisterClientScriptBlock( this.GetType(), "Calendar_ChangeDate", strScript);
or, consider dropping the script tags, and use this:
string strScript = "window.opener.document.forms[0].ctl00_ContentPlaceHolder1_" + Request.QueryString["textbox"].ToString() + ".value = '";
strScript += GetDateCal.SelectedDate.ToString();
strScript += "';window.opener.document.forms[0].submit();self.close();";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Calendar_ChangeDate", strScript,true);
So, "" in any string tends to be ignored, or escaped.
Use # for such strings, but probably better is to simple include the true tage in the Register script block, and it will surround the js code with script tags for you.

Why isn't my Javascript Ajax call posting values to my PHP script?

This is a very strange issue. The Javascript IS calling the PHP script, and the PHP script IS returning "something" (it returns array ()). The issue is, when I try to get the value of the posted data via $_POST['ID'] it basically says that there is no posted value, even though sendData does contain a value for ID. The full string of sendData (obtained through alert(sendData)) is as follows:
ID='1'&Invoice=''&FirstName=''&LastName=''&Description=''&Testimonial=''&ScreenRoom='false'&GlassWindow='true'
Javascript File: admin.js
function saveChanges() {
var sendData='ID=\'' + document.getElementById("ID").value + '\'';
sendData+='&Invoice=\'' + document.getElementById("Invoice").value + '\'';
sendData+='&FirstName=\'' + document.getElementById("FirstName").value + '\'';
sendData+='&LastName=\'' + document.getElementById("LastName").value + '\'';
sendData+='&Description=\'' + document.getElementById("Description").value + '\'';
sendData+='&Testimonial=\'' + document.getElementById("Testimonial").value + '\'';
sendData+='&ScreenRoom=\'' + document.getElementById("ScreenRoom").checked + '\'';
sendData+='&GlassWindow=\'' + document.getElementById("GlassWindow").checked + '\'';
var req = new XMLHttpRequest();
req.open("POST","scripts/saveChanges.php",true); //true indicates ASYNCHRONOUS
req.send(sendData);
req.onreadystatechange = function() {
//Is request finished? Does the requested page exist?
if(req.readyState==4 && req.status==200) {
//Your HTML arrives here
alert(sendData);
alert(req.responseText);
}
}
}
PHP File (that is being posted to): saveChanges.php (located in /scripts/)
<?php
session_start();
if (!isset($_SESSION['group']) || $_SESSION['group'] != 'admin') {
die ('You do not have permission to access this page!');
}
print_r($_POST);
$ID=$_POST['ID'];
$Invoice=$_POST['Invoice'];
$FirstName=$_POST['FirstName'];
$LastName=$_POST['LastName'];
$Description=$_POST['Description'];
$Testimonial=$_POST['Testimonial'];
$Date=$_POST['Date'];
$GlassWindow=$_POST['GlassWindow'];
$ScreenRoom=$_POST['ScreenRoom'];
?>
I normally only come here in a state of desperation, and being I've spent about 3 hours now working on trying to figure this out, I consider myself fairly desperate. Any help would be greatly appreciated and please ask if you need more information.
You have to set the content type for php to read the variables
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
Remove the quotes from the posted data and properly encode it
var sendData='ID=' + encodeURIComponent(document.getElementById("ID").value);
sendData+='&Invoice=' + encodeURIComponent(document.getElementById("Invoice").value);
sendData+='&FirstName=' + encodeURIComponent(document.getElementById("FirstName").value);
sendData+='&LastName=' + encodeURIComponent(document.getElementById("LastName").value);
sendData+='&Description=' + encodeURIComponent(document.getElementById("Description").value);
sendData+='&Testimonial=' + encodeURIComponent(document.getElementById("Testimonial").value);
sendData+='&ScreenRoom=' + document.getElementById("ScreenRoom").checked;
sendData+='&GlassWindow=' + document.getElementById("GlassWindow").checked;
Set the ready state listener before you send the request
req.onreadystatechange = function() {
//Is request finished? Does the requested page exist?
if(req.readyState==4 && req.status==200) {
//Your HTML arrives here
alert(sendData);
alert(req.responseText);
}
}
req.send(sendData);
Try to set HTTP header:
req.setRequestHeader("Content-type","application/x-www-form-urlencoded");

I want to run a new page on my website after the registration is finish

Wen I finish (successfully) the new user registration I want to change the current page in a new notification page from my code behind.
For this purpose I use a javascript:
<script type="text/javascript">
var Tag;
function notifyscript(TagName) {
tag = TagName;
var myTag = document.getElementById(tag);
myTag.click();
}
in order to call:
<a id="notify" href="http://localhost/Pages/Account/Notification.aspx" target="_parent"/>
But it trow me an error when I run this locally.
500 (HTTP Error 500.0 - Internal Server Error
The call fails LoadLibraryEx στο φίλτρο ISAPI "C:\Windows\Microsoft.NET\Framework\v4.0.30319\\aspnet_filter.dll"
When I run this in the web doing nothing.
Is there someone who knows how to do this action?
I mean to open a new page when the code behind ends the registration.
Please try this code on Code Behind:
string url="www.google.com";
string msg = "<script type='text/javascript'>" +
" $(document).ready(function(){ " +
" window.location.href='" + url + "';" +
" });" +
"</script>";
page.ClientScript.RegisterStartupScript(page.GetType(), "myURL", msg);

Passing javascript parameter from codebehind error

I use this script to call a javascript from codebehind.
ClientScript.RegisterStartupScript(this.GetType(), "Exist", "<script language='javascript'>ConfirmRedirect('" + url + "','" + msg + "');</script>", true);
and my javascript code is
function ConfirmRedirect(url,msg)
{
alert(msg);
window.location.href = url;
}
Am getting ')' expected error.
What am missing here? If am calling javascript without paramters then it is working.
Remove the Script tag and it will work, this worked for me:
ClientScript.RegisterStartupScript(this.GetType(), "Exist", "ConfirmRedirect('" + url + "','" + msg + "');", true);
View the HTML source and include it in your question/post.
You have probably just not escaped the msg enough. It can for example not contain single quotes ' or line breaks.
UPDATE
A simple solution would be to escape all single quotes using Replace().
ClientScript.RegisterStartupScript(this.GetType(), "Exist", "<script language='javascript'>ConfirmRedirect('" + url + "','" + msg.Replace("'", "\'") + "');</script>", true);
Probably the msg parameter contain symbols like the '
Then the code will be something like
<script language='javascript'>
ConfirmRedirect('/myurl/somthing.aspx','Here's my message');
</script>
And there wait for the close parenthesis.
Replace your msg varriable. Use this one---
ClientScript.RegisterStartupScript(this.GetType(), "Exist", "javascript:ConfirmRedirect('" + url + "','" + msg.Replace("'","") + "');", true);

Sharepoint Custom WebPart Startup Script

Okay, I've gotten a unique issue I've been trying to solve for two days.
I have System.Web.UI.WebControls.WebParts.WebPart control I am building a custom Sharepoint View with. Almost everything I want done is working except one little issue. I need to use Javascript to Format Date and Currency fields. The Client wants DateTime fields to be mm/dd/yyyy and currency have $ and commas where appropriate.
This is easy enough in javascript on a regular aspx page. I just wrote the functions and on page load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridFieldDAO dao = new GridFieldDAO();
myGrid.DataSource = dao.getItems();
myGrid.DataBind();
}
GetBuildFormattingScript();
}
private void GetBuildFormattingScript()
{
string script = "<script type=\"text/javascript\">";
script += " FormatByRows(\"" + myGrid.ClientID + "\",2);";
script += " FormatByRowsDate(\"" + myGrid.ClientID + "\",1);";
script += "</script>";
if(!ClientScript.IsClientScriptBlockRegistered("DoFormatting"))
ClientScript.RegisterStartupScript(typeof(string), "DoFormatting", script);
string script2 = " <script type=\"text/javascript\">"+
"var prm = Sys.WebForms.PageRequestManager.getInstance(); "+
"prm.add_beginRequest(BeginRequestHandler); "+
"prm.add_endRequest(EndRequestHandler); "+
"function BeginRequestHandler(sender, args) "+
"{ }"+
"function EndRequestHandler(sender, args) "+
"{ FormatByRows(\"" + myGrid.ClientID + "\",2); "+
" FormatByRowsDate(\""+myGrid.ClientID+"\",1);}</script> ";
if (!ClientScript.IsClientScriptBlockRegistered("DoUpdateFormatting"))
ClientScript.RegisterStartupScript(typeof(string), "DoUpdateFormatting", script2);
}
My issue in that on the OnLoad of the WebPart the grid I am wanting to update doesn't exists... so I have to add code to the OnPreRender.
Well, the WebPArt loads and the Javascript doesn't fire... so I click refresh and it does fire. Can anyone help me get the code working on the inital WebPart Load? Has anyone been able to get server side script to work this way in SharePoint?
Thanks,
Mike V
For this, you can take advantage of _spBodyOnLoadFunctionNames:
string script = "<script type=\"text/javascript\">";
script += " function FormatDataGridRows() {";
script += " FormatByRows(\"" + myGrid.ClientID + "\",2);";
script += " FormatByRowsDate(\"" + myGrid.ClientID + "\",1);";
script += " }";
script += " _spBodyOnLoadFunctionNames.push('FormatDataGridRows');";
script += "</script>";
Edit
To test, put the following code in a Content Editor web part on your page:
<script type="text/javascript">
function SayHello() {
alert('hello world!');
}
_spBodyOnLoadFunctionNames.push("SayHello");
</script>

Categories