opening multiple browser tabs but only one will open - c#

I have a scenario where I loop through a data set (max of 6 records) and then open a new browser tab for each record - each tab shows an invoice for one of the records - not the best design but it's what was requested.
I'm using the code below within a foreach to build a url and open a new browser tab, the problem is it loops through ok but only ever opens 1 new tab.
Every other thing happening in the loop works so the problem seems to be with the code. It opens the first tab for the first record then no more after that.
Can anyone comment on what's wrong?
string pageurl = "Label.aspx?booking=" + v.booking + "&pallet=" + v.palletId;
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "popup", "window.open('" + pageurl + "','_blank')", true);

You can only have one startup script. Try putting all of your window.open calls in a single script;
//This code inside loop
string pageurl = "Label.aspx?booking=" + v.booking + "&pallet=" + v.palletId;
string script += "window.open('" + pageurl + "','_blank'); "
//This code outside loop
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "popup", script, true);

Related

Pop window not opening at code behind

After open pop window, i need to reload my page. here page gets reloaded but pop window not opened. How to solve it.
string url = "Popup.aspx";
string s = "window.open('" + url + "', 'popup_window', 'width=300,height=100,left=100,top=100,resizable=yes');";
ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);
Response.Redirect("~/mysamepage.aspx", false);
Have you tried a Reload in the javascript by appending it to the s string:
window.location.reload(false);
// If you need to fetch the webpage from the web-server again (where the page
//contents change dynamically) pass the argument as 'true' instead of 'false'.
eg
string s = "window.open('" + url + "', 'popup_window', 'width=300,height=100,left=100,top=100,resizable=yes');window.location.reload(false);";
or try:
window.opener.href('url').reload(true);

How to create a Log with listBox

I'm creating a webapp in c# using visual stdio 2008.
In the page there's a button that starts a quite long (about 1 min) processing.
During this i would like to show what the program is doing in a listbox in the same page but I don't know how to do it beacuse when the process start the page is continuously refreshing so the listbox is always empty even if in the code there are many ListBox.Items.Add("..").
Hope you can help me.
(p.s. sorry for my bad english,I'm italian)
May be your page load is taking time for other reasons, but not sure.
public static void WriteLog(string Error)
{
using (StreamWriter logfile = new StreamWriter(filePath + "Log.txt", true))
{
logfile.WriteLine(DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString() + " -#: " + Error);
logfile.Close();
}
}
Use this function to log.
Call this function in Page load
WriteLog("Comments");

Passing long querystrings over urls shows page not found error(404)

I have a "error.aspx" page which is there to mail me if any exception is caught. When I open the page manually, mysite.com/error.aspx, the page opens fine but when it is redirected by a catch block with the exception.message and exception.stackTrace as querystrings, I get an error "page not found". Are the querystrings directing the browser to open a different url? It works fine when run on localhost, though.
public void send_error(Exception ex)
{
Response.Redirect("error.aspx?time=" + DateTime.Now.ToString() + "&ex=" + ex.Message + "&st=" + ex.StackTrace.Replace("\n", " "), false);
}
If you check this Article, you will see that the max query length of url string is 2048 symbols for Internet explorer. Probably the url is bigger and because of that you have this problem. One solution is to save the desire message in the session as string and after that retrieve it on other pages.
string errorMessage = DateTime.Now.ToString() + " " + ex.Message + " " + ex.StackTrace.Replace("\n", " ");
Session["__ErrMessage"] = errorMessage;
When you are in other pages you can access this string like this:
string errMessage = "";
if(Session["__ErrMessage"] != null)
errMessage = Session["ErrMessage"].ToString();

Opening a PDF with search parameters just opens the PDF

I was requested to add some searching functionality to an existing system for the collection of PDFs that we have. I know about searching PDFs and opening them with search parameters and in a test application I wrote, it works like a dream. When trying to convert it over to our existing application the PDF opens but without the search terms or the advanced find of Acrobat Reader popping up. Any help would be greatly appreciated!
Here is a snippet of the cs code :
case "PDF":
string searchTerms = SearchWordsTB.Text;
searchTerms = searchTerms.Replace(',', ' ');
launchStr = "OpenPDF('" + e.Row.Cells[9].Text.Replace("\\", "/") + "','" + HttpUtility.UrlEncode(e.Row.Cells[2].Text) + "','" + e.Row.Cells[0].Text + "','" + searchTerms + "')";
break;
We are creating the list of documents on the fly and PDF is one of the options. Assuming I am understanding this correctly, A DataGrid is created with all these clickable rows that will execute a Javascript function when clicked. The Javascript function OpenPDF is shown below:
function OpenPDF(url, filename, ID, searchTerms) {
if (searchTerms.length > 0) {
window.open('FileViewer.aspx?name=' + filename + '&ID=' + ID + '&url=' + url + '#search="' + searchTerms + '"', 'mywindow' + windowCnt, 'width=800,height=600,location=no,resizable=yes');
}
else {
window.open('FileViewer.aspx?name=' + filename + '&ID=' + ID + '&url=' + url, 'mywindow' + windowCnt, 'width=800,height=600,location=no,resizable=yes');
}
windowCnt++;
}
From following the debugging in the CS code, I know that I am properly stripping out the commas in the search terms so that shouldn't be the problem. What currently happens is the PDF file will open up just fine, but the search terms are not being used. I have tried following the debugger through the Javascript (which for me has always been spotty at best) but the breakpoint is never hit. It should also probably be noted that the Javascript function is kept in a separate Javascript File and is not inline in the aspx page. And yes, we are correctly referencing the Javascript file. I will be more than happy to update this post with any extra info that is requested. Thanks in advance for any help!
I was able to achieve the desired results by using the http encode on the launch string as shown below.
launchStr = "OpenFile('" + HttpUtility.UrlEncode(e.Row.Cells[9].Text.Replace("\\", "/") + "#search=\"" + searchTerms + "\"") + "','" + HttpUtility.UrlEncode(e.Row.Cells[2].Text) + "','" + e.Row.Cells[0].Text + "','" + e.Row.Cells[1].Text + "')";
I then used the function to just open the window with the PDF in it. The problem I had was that without the HTTP Encode, the URL was just cutting off the search parameters. I believe this is because the #search="blah" isn't normally recognized as part of a URL and was therefore truncated. If anyone has a better reason, I would love to hear it.

Why I can't register a client script block of string (url)?

Code :
string url = "http://www.google.com";
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "UrlSocialDefinitivo", "var UrlSocialDefinitivo=" + Server.UrlEncode(url) + ";", true);
but I get (on browser console) : SyntaxError: identifier starts immediately after numeric literal.
In fact it registers UrlSocialDefinitivo=http%3a%2f%2fwww.google.it;
Where am I wrong? How can I resolve this trouble?
It miss the quotes around the string :
string url = "http://www.google.com";
ScriptManager.RegisterClientScriptBlock(
this,
this.GetType(),
"UrlSocialDefinitivo",
"var UrlSocialDefinitivo=\"" + Server.UrlEncode(url) + "\";",
true
);
That will produces :
UrlSocialDefinitivo="http%3a%2f%2fwww.google.it";
Note that I'm not sure you have to keep the Server.UrlEncore call. It depends on the format you are expecting actually.
You need to surround the url string in quotes or the javascript interpreter will assume it is javascript to be executed (and fail miserably).
ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
"UrlSocialDefinitivo",
"var UrlSocialDefinitivo='" + Server.UrlEncode(url) + "';", true);
// quotes here ^ and here ^

Categories