Server.MapPath in window open in asp.net - asp.net

i want to open a new pop up window when user click the button.But nw i facing a problem, how can i open a new pop up window based on server.mapPath?
Here is my coding
StringBuilder sb = new StringBuilder();
sb.Append("<script>");
sb.Append("window.open(" + Server.MapPath("~/reportPreview.aspx") + ", '', '');");
sb.Append("</script>");
ClientScript.RegisterStartupScript(this.GetType(),"test", sb.ToString());
But i cant open a new window . Please help :(

window.open expects a URL like "../reportPreview.aspx", but Server.MapPath returns a physical path like "C:\YourApp\reportPreview.aspx". You should call ResolveClientUrl instead. Also, you need to add quotes around the URL:
sb.Append("window.open('" + ResolveClientUrl("~/reportPreview.aspx") + "', '', '');");

Related

Creating a report from label values in asp.net

I have a website that I wrote in ASP.net and I have a page called Statistics.aspx
that I displayed few useful numbers that admin can view in labels .
I want to make a button " Download Report " that can put these numbers into a word/pdf document .
Can anyone help ?
You need to give a bit more detail.Well I believe this is useful
string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
StringBuilder sb = new StringBuilder();
sb.AppendLine(Label1.text); // and so on
using (StreamWriter outfile = new StreamWriter(mydocpath + #"\AllTxtFiles.txt"))
{
outfile.Write(sb.ToString());
}

Set Hyperlink to be open in new window / new tab server side

I wonder if I can get some help, I'm creating hyperlink on the server side for each row in the my grid.
The issue is how can I set it up to open in a new window or new tab when clicking on the hyperlink.
Please help me to modify my code so it can be open in new window/new tab.
Protected Function GenerateReportLink(ByVal inputVal)
Dim output As String = ""
Try
Dim svcs As New SystemServices
' Check for null values
If Not inputVal Is Nothing And Not String.IsNullOrEmpty(inputVal) Then
Dim URL As String = Nothing
URL = (String.Format("https://www.test.com/cgis/{0}/Reports/ShortReport.asp?/SessionID={1}&IncidentID={2}", m_User.CompanyCode, m_SessionID, m_IncidentCaseID.ToString()))
output = "<a href='" + URL + "'>Report</a>"
End If
Catch
End Try
Return output
End Function
Change:
output = "<a href='" + URL + "'>Report</a>"
to:
output = "<a href='" + URL + "' target='_blank'>Report</a>"
Source: W3 Schools
It looks like you might want to set the 'target' attribute of the hyperlink, although going from what you've written so far, I'm not 100% sure where this hyperlink is going to eventually be clicked, most modern browsers, unless the user has explicitly set the behavior as otherwise would open in a new tab I think.
Either way, this might help you http://www.w3schools.com/html/html_links.asp
just add the target='_blank' property to anchor <a> tag under your output varriable.
"<a href='" + URL + "' target='_blank'>Report</a>"

opening small size window from link send in body of email generated using asp.net

here is my code
System.Text.StringBuilder sb = new System.Text.StringBuilder();
MailMessage message = new MailMessage("abc#gmail.com", txtEmailId.Text.Trim());
message.Subject = "Auto email Test";
message.IsBodyHtml = true;
string str="http://localhost:55243/WebSiteTest/Accept.aspx?id=" + txtEmailId.Text.Trim();
string url = #"<a href="""+str+#""" target='_blank'";
string str1 = "http://localhost:55243/WebSiteTest/Reject.aspx?id=" + txtEmailId.Text.Trim();
string url1 = #"<a href=""" + str1 + #""" target='_blank'";
message.Body = #"<html><body>Thanks For Showing interest in our site. please press "+ url + #">Accept</a>Or "+ url1+">Reject</a></body></html>";
SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential("abc#gmail.com", "password");
client.Send(message);
it redirects to the aspx page when link is clicked from mail received but as a new tab in browser.
but i need to open the pages in small window that will open on the screen.
any help!!!!!
Thanks in Advance.
You can't do that. You would need javascript to open a window at a certain size, and e-mail cients - reasonably so - do not run javascript in e-mails.
I believe what you're seeing is a combination of markup and browser behavior. In particular, the anchor tags you're generating in the message body both contain the target="_blank" attribute. This tells the browser to open the content in a new window, but doesn't specify the size of the window, so it seems the browser is just opening a new tab in an already opened browser (if no browser is opened a new browser instance altogether is opened).
The behavior you're seeing is not unexpected. As Menno van den Heuvel mentioned, you do need JavaScript to give browsers explicit instructions on the window size. This w3schools.com link has some details on that if you're interested. But, again, as Menno van den Heuvel points out most email clients aren't well equipped to handle JavaScript.

how to run response.redirect on for loop

I Have process that should run atau go to url , but it should run on for loop :
For i = 0 To ds.Tables(0).Rows.Count - 1
idCustomer = ds.Tables(0).Rows(i)("House").ToString()
amt = ds.Tables(0).Rows(i)("OPR_BALANCE").ToString()
lang = "0"
aid = "000000"
Dim result As String = "http://soap.Services.com:2121/WS.aspx/?c=1&id=" + idCustomer + "&lang=" + lang + ""
Response.Redirect(result)
Next
but right now, based on my code, redirect to the url and , the progress is automaticly stop because its redirected.
is it possible to run process by redirectt to URL on for loop?
Redirecting inside a loop wont work. You need to call the service in other way.
Try using DownloadString method of WebClient. Extract result by this:
Dim client As New WebClient()
Dim result As String = client.DownloadString("http://soap.Services.com:2121/WS.aspx/?c=1&id=" + idCustomer + "&lang=" + lang )
you cannot do it with using response.redirect(). instead, you can use clientside code(JS) to do this logic. do your logic in JS and open up new pages with JS.
you can use jquery to call a webmethod. that webmethod can do the database stuff for you and return the data as json object. then you can do your logic in jquery to open new pages
I hope this helps
As soon as you call the Redirect you are done.... you can't redirect to mutiple pages....its illogical.....
Look this discussion : Redirect loop

viewing the images problem

Using ASP.Net and VB.Net
Code
Image1.ImageUrl = "c:\Blue hills.jpg"
The above code is not viewing the image when i clicked the button.
How to solve this problem.
Need Code Help
You shouldn't be referencing the local file path - you should set the ImageUrl to be the URL on the webserver - for example, if you had the file in the root of your website, you'd reference it as:
Image1.ImageUrl = "/Blue%20hills.jpg"
The value you are supplying to the ImageUrl property is a file path and not a Url.
Ideally you should have a folder in your ASP.NET application that holds your image files which you can then reference with either a relative or absolute Url path i.e:
Image1.ImageUrl = "/Content/Images/Blue hills.jpg"
If you want to show pictures from the server disk that are outside of the website root, you have to build "proxy" page. For example call it ShowImage.aspx and it will get the picture name on the URL.
Then you'll have such code
Image1.ImageUrl = "ShowImage.aspx?image=Blue hills.jpg"
And the code for ShowImage.aspx code behind:
string imageName = Request.QueryString["image"] + "";
if (imageName.Length == 0 || imageName.Contains("/") || imageName.Contains("\\") || !imageName.ToLower().EndsWith(".jpg"))
{
throw new Exception("Invalid image requested");
}
else
{
string strFullPath = "C:\\" + imageName;
Response.ContentType = "image/jpeg";
Response.Clear();
Response.WriteFile(strFullPath);
}
Response.End();
This is C# but should be simple enough to translate into VB.NET as well.
Edit: added basic validation that verify that user does not try to fetch files from different directory and allows only .jpg files.
Image1.ImageUrl = System.Web.HttpContext.Current.Server.MapPath("~") + "/"+ "Images" + "/" + "Blue hills.jpg";
but as said before you can't reference a local file like this , except u use HttpHandler.
http://www.15seconds.com/issue/020417.htm

Resources