how to add a C# string in javaScript as text popup - c#

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

Related

C# open browser with inserted words

I want to ask you guys if it is possible to do the following:
Type in textbox something like this "search pluto"
and then it must search for that last word.
This is how I did it but it doens't work because when I do that
my browser opens up twice.
One with "https://www.google.be/#q="
and the other tab that opens is the word that I wrote in
the textbox. Can somebody help me out of this please?
This is the code for this:
string url = "https://www.google.be/#
if (inputTBX.Text.Contains("search ") == true)
{
inputTBX.Text.Replace("search ", "");
string URL = url += inputTBX.Text;
Process.Start(#"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
URL);
inputTBX.Clear();
}
just as a test in a simple console app I tried the following and it worked launching my default browser
var t = "pluto";
Process.Start("http://google.com/search?q=" + t);
This also works
var t = "pluto";
Process.Start("https://www.google.be/search?q=" + t);
in your case you need to get your query string to be the following
https://www.google.be/#q=pluto
your first problem is that you are trying to use the Replace method but you need to assign it into something here is a working solution of your code just tested notice the differences in what I have done
inputTBX.Test = "search pluto";
string url = "https://www.google.be/search?q=";
if (inputTBX.Contains("search "))
{
inputTBX.Text = inputTBX.Replace("search ", "");
string URL = url += inputTBX;
Process.Start(URL); // this will launch your default web browser
inputTBX.Clear();
}
since your query string has the word search in it.. you really don't need this line if (inputTBX.Contains("search ")) but if you keep it it will work with if you pass search planet pluto for example in your textbox
This part
inputTBX.Text.Replace("search ", "");
Is seriously bad, because it will fail to do its job if you have input string like this
"search research on Beethoven's work"
If you want the key phrase to be "search ", you should do this instead
inputTBX.Text.Substring(("search ").Length); //this way it will skip the "search " phrase and use the rests of the phrase for searching
As for your process with the given URL, simply do
string url = "https://www.google.be/#q="; //notice the q= is missing in your code shown
Process.Start(url + inputTBX.Text.Substring(("search ").Length));

Jquery Newline doesnot display on message popup

I have code which displays a confirmation popup.
string message = "Do you want to set activity to Inactive? " ;
message += "The predefined settings will be reset for all the users using this Activity.";
SetToInactiveCheckBox.Attributes["onclick"] = "if($('input[id*=SetToInactiveCheckBox]:checkbox:checked').length > 0 ) return confirm('"+ message +"');";
I want the two lines to be printed in separate lines.
I tried in a following ways
string message = "Do you want to set activity to Inactive? \n" ;
string message = "Do you want to set activity to Inactive? '\r\n'" ;
string message = "Do you want to set activity to Inactive? '<br/>'" ;
How too display the messages in separate lines. I am using IE8.
You can use
\n or \r\n.
If that does not work then use
\\r\\n
You should use
<br />
instead of
'<br />'
This worked for me.

Alert show when extension of FileUpLoads are not allowed

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

message script does't show in asp.net webpage

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.

Client method called by ScriptManager.RegisterStartupScript not firing

Can somebody look at the below code and tell me what I am doing wrong.
for(i=0;i<=Request.Files.Count;i++)
{
int percentComplete = (int)Math.Ceiling((double)(i + 1) / (double)Request.Files.Count * 100);
string message = string.Format("{0} of {1} uploaded", i + 1, Request.Files.Count);
ScriptManager.RegisterStartupScript(this, this.GetType(), "progress", #"do_progress("+message+","+percentComplete+");", true);
}
I am trying to update the client with each pass of the loop. On the client (inside of the form tags) I have a function called "do_progress" that takes two parameters: message and percent. My intention is that the client-side method fires with each pass of the loop but nothing happens.
UPDATE
Thanks for your help. Ramiz, your code won't work in my case because it collects all the methods (and progress) inside the loop and then sends them to the client at the same time.
This won't show progress accurately (each loop represents the completion of an uploaded file). I need to be accessing the client function, do_progress, after each unique completion of the server-side loop.
Also, the page has already loaded and the code is fired when a (upload) button is clicked.
Having said that, I am still having problems. I can confirm that I am getting the results I want with the below code by looking at 'selection source':
int percentComplete = (int)Math.Ceiling((double)(i + 1) / (double)Request.Files.Count * 100);
string message = string.Format("{0} of {1} uploaded", i + 1, Request.Files.Count);
ScriptManager.RegisterStartupScript(this, this.GetType(), "progress" + i, #"do_progress('" + message + "','" + percentComplete + "');", true);
But, I am not seeing the results update in real-time on the client. The progress bar isn't moving and the counter (n of n files) isn't doing anything. But, when I look at the innerHTML, the values have updated. Very odd. It is almost like I need to be refreshing the page or something but that should't be necessary.
The client side function I am using, which is placed in the form tags at the end of the page, looks like this:
function do_progress(message,percent)
{
try {
$('progress_status').innerHTML = message;
$('progress_bar').attr("style", percent + "px");
}catch(e){alert(e.message)};
}
message is a string value it should be enclosed in single quote "do_progress('"+message+"',"+percentComplete+");"
percentComplete contains integer so it doesn't require to enclose in single quote as message does.
string methods = string.empty;
for(i=0;i<=Request.Files.Count;i++)
{
int percentComplete = (int)Math.Ceiling((double)(i + 1) / (double)Request.Files.Count * 100);
string message = string.Format("{0} of {1} uploaded", i + 1, Request.Files.Count);
methods += "do_progress('"+message+"','"+percentComplete+"');"
}
Secondly, here, I'm incrementing all methods in a string variable and calling it in window.onload event just make sure the DOM is ready before we call the do_progress function.
ScriptManager.RegisterStartupScript(this, this.GetType(), "progress", #"window.onload = function() {"+ methods +"}", true);
However, this should not require here, call in window.onload as ScriptManager.RegisterStartupScript will call them when DOM gets ready.
I'm not sure what exactly issue at your end but this is my instant review.
try to use this.RegisterStartupScript instead of ScriptManager.RegisterStartupScript
Your client side do_progress function appears to have an error in the jQuery selectors - you're currently looking for elements named progress_status and progress_bar when I suspect you intend to be looking for classes or IDs. If your selector doesn't match anything it doesn't raise any kind of error, it just doesn't do anything.

Categories

Resources