how to make html page by link name - asp.net

I am building a page for produce name "product.aspx"
I have display some products in this page with hyperlink.
Now I want, when I click my product (such as pen) then it automatically makes an html page name pen.html with some information.
When I click clock then it automatically makes an html page name clock.html with some information.
How can I do this?
I am using asp.net C# 3.5.

You can use a Literal to crete your html page
in frontend ypu can add Literal
so in backend code you can create a html page by adding text to the literal
eg.
var productdata = datasource;
ltrProduct.text ="<head><title>"+productdata.title+"</title></head><body></body></html>";

Rather than actually creating a physical file for these products, its best to have a Web Form called something like "Product.aspx" which you pass a querystring to like:
/Product.aspx?product=Pen
This can then be URL Rewritten to something like:
/Product/Pen.aspx
You can then use this query string to return data like:
Dim cmd as new sqlcommand("SELECT Name, Price FROM Products WHERE FileName=#FileName", Conn)
cmd.CommandType = CommandType.StoredProcedure
cmd.parameters.AddWithValue("#FileName", Request.QueryString("product"))

Related

Classic ASP render a template and send variables

I have a classic ASP page, and I need to create a loop for each row on a table and then create an html document and save it to the hard drive, but I want to create a template so I just send the two variables to the template so I don't have to write the HTML document each time on the loop.
This is what I have so far:
SQL = "select Title, Article from [ASPTest].[dbo].[articles]"
set rs = conn.execute(SQL)
arrRecs = rs.GetRows
For row = 0 To UBound(arrRecs, 2) 'Rows
For col = 0 To UBound(arrRecs, 1) 'Columns
Response.Write rs.Fields(col).Name & " = " & arrRecs(col, row) & " "
dim fs,f
set fs=Server.CreateObject("Scripting.FileSystemObject")
set f=fs.CreateTextFile("C:\Users\User\Documents\ASP Pages\"+arrRecs(col, row)+".html",true)
f.write("<html><body><div>It kinda works</div></body></html>")
f.close
set f=nothing
set fs=nothing
Next
Response.Write "<br />"
Next
Is there a way to use a template that has 2 variable holders and send the article name and title to the template and then save it to the disk?
Thank you.
I think you could probably achieve what you want using a template stored as a text file, and the Replace function.
Your template should be a fully-formed html page, but with placeholder values for the title and article. The placeholders need to be unique, so something like [[[~~~Title~~~]]] or a similar sequence that will not occur in your actual titles, articles, or the template itself.
<html>
<head><title>[[[~~~Title~~~]]]</title></head>
<body>
<h1>[[[~~~Title~~~]]]</h1>
<div id="article">[[[~~~Article~~~]]]</div>
</body>
</html>
In your code, read the template from the file and store it in a variable. (So technically, you could just write it to a variable in the first place, but VBScript is bad at string concatenation... anyway.) Get your array of titles & articles and loop through it (though only once: I'm not sure why you're looping through both rows and columns in your attempt). For each row, make a copy of the template, replace the title placeholder with the current row's title, replace the article placeholder with the current row's article, and write the result to a file.
Dim template, t
Dim fso, file
Dim rs, conn, SQL
Dim records, row
SQL = "SELECT ID, Title, Article FROM [ASPTest].[dbo].[articles]"
'[...database stuff...]
records = rs.GetRows
'[...close database...]
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("path/to/template.txt",1) '- 1 = For reading
template = file.ReadAll
file.Close
Set file = Nothing
For row = 0 to UBound(records,2)
t = template
t = Replace(t,"[[[~~~Title~~~]]]",records(1,row))
t = Replace(t,"[[[~~~Article~~~]]]",records(2,row))
Set file = fso.CreateTextFile("path/to/html/" & records(0,row) & ".html")
file.Write(t)
file.Close
Set file = Nothing
Next
Set fso = Nothing
Back in the day I created the KudzuASP template engine to solve this rather complex deficiency in Classic ASP. In KudzuASP you can have ASP code pages that have absolutely NO HTML in them.
KudzuASP is as small include file roughly under 1000 lines of code that turns your hosting ASP page into an event driven object used by the template engine.
In short you create an instance of the template engine, set some variables, install custom code objects, and invoke it after which the template engine reads your template and make callbacks to your ASP page when and where appropriate. It has a library system so you can load libraries of custom tags handlers/components via code or by tags placed in your HTML template.
One of the best features is that for those still under the Classic ASP umbrella it makes 100% separation of application code and logic from presentation possible. Coding Classic ASP pages using KudzuASP is much easier than without and because of the way ASP compiles pages the callbacks are "native" and very fast.
You can find it here KudzuASP where the project is still maintained.

Comparing URLs in ASP.NET

I have an application that crawls a web site for unique link urls (i.e. hrefs) and then saves the urls to a database. I will ensure that there is url for each page in the site. Below is the code for getting the string that is saved to the database.
'url is the url obtained from the link's href
Dim uriReturn As Uri = New Uri(url, UriKind.RelativeOrAbsolute)
'Make it absolute if it's relative
If Not uriReturn.IsAbsoluteUri Then
Dim baseUri As New Uri(BaseUrl)
uriReturn = New Uri(baseUri, uriReturn)
End If
Return LCase(uriReturn.ToString)
In another part of the application I have section that queries the database with the url of the current page. Below is the code for getting the current page url.
Dim CurrentURL As String = lcase(HttpContext.Current.Request.Url.AbsoluteUri
My question is can I be sure that I will find a match in the database using the current page url? That is could there be differences in the string obtained from the href and the string returned from the current page even through they point to the same page? Is there a way to convert the urls to ensure they will always match?
Since BaseURl is not defined, can't tell if you got it correct. But BaseUrl should be = Request.Url.
And your
Dim CurrentURL As String = lcase(HttpContext.Current.Request.Url.AbsoluteUri
Since you are doing store/retrieve, I suggests you standardize your methods. In store section, you use uriReturn.ToString(), so in the retrieve section, you should also use ToString() instead of AbsoluteUri.

How to add a hyperlink in a text file in ASP.NET using VB

I have tow pages, first one for displaying the data in the text file and the other one is to store the data. I want to save a site in the text file but when i display the data will be just text and I can't click it as a link. The code I post is when I write to a file in display page.
Try
Dim fs As String
fs = Server.MapPath("Footer.txt")
lblsplittext.Text = ""
Dim filestream As StreamReader
filestream = New IO.StreamReader(fs)
Dim readcontents As String = filestream.ReadToEnd()
Dim textdelimiter As String = "#"
Dim splitout = Split(readcontents, textdelimiter)
Dim i As Integer
For i = 0 To UBound(splitout)
lblsplittext.Text &= splitout(i) & "<br>"
Next
filestream.Close()
Catch ex As Exception
Dim str As String
str = ex.Message
End Try
If you have a different suggestion about how to read from a file or database (it doesnt matter from where for now) just keep in mine when i display them i just need to have hyperlinks among my text... Or can I write to html file instead of text file if so what is the difference i need to make to write to html. I really really need help here and i have done many searches but i found nothing.
Thanks in advance.
There are a number of ways you could do this. One would be to use a series of dynamically-added Label controls. Your Hyperlink control could simply be inserted between one Label control and the next.
Do you intend to retrieve information from your series of labels on postback? (It would be redundant to do that, since you already know what the information is, but just in case.) Using multiple controls would make that more complicated. You could try one or more Literal controls, created dynamically and added as child controls to a Panel control. Again, the Hyperlink control would be added at whatever time you need it.

Access the controls from a web page

I want to access the content(controls) on an external web page
http://nccptrai.gov.in/nccpregistry/search.misc
There are a few controls there like the text box, get the value of it,
redirect to http://nccptrai.gov.in/nccpregistry/saveSearchSub.misc
supplying necessary input
You can use this code based on WebClient
System.Net.WebClient wc = new System.Net.WebClient();
byte[] raw = wc.DownloadData("http://nccptrai.gov.in/nccpregistry/saveSearchSub.misc");
//it's also possible to use DownloadString
string webData = System.Text.Encoding.UTF8.GetString(raw);
And so you can parse webData in order to get your wished value

ASP.NET - Current name of page from web user control?

I need to find out name of location where web user control is. Somethnig like HttpContext.Current.Request.Url.ToString(), but I get only page for this web user control.
Request.Url.Segments will give you a string array. The last item is the page
You should try the Request.Url.LocalPath property
string fileNameFromLocalPath = Path.GetFileName(Request.Url.LocalPath);
This code helps:
string filename = Path.GetFileName(Request.Url.AbsolutePath);
If you ask for Page.getType.name, you will get the name of the master, the aspx page.
if you want the name of the ascx control you are working on, use
me.GetType.Name.ToString
if your control is in a directory MyDir and the name of your ascx is test.ascx then the result will be
"ASP.MyDir_test_ascx"
You can also use (VB.Net):
Dim pageName as String = Page.GetType().Name
which replaces the .extension with an underscore
So from Default.aspx you would be returned Default_aspx
You can also use:
Dim pageName as String = CType(HttpContext.Current.CurrentHandler, Page).GetType().Name
Which will produce the same results as described above.

Resources