Create ASP.Net Page Programatically

M

Mythran

I have 2 projects, a Web project and a Class Library project. In the class
project, I have a class called Class1. Pseudo below:

Class Class1
...
Public Static Sub Generate()
If True
' Create Page and display to user.
End If
End Sub
End Class


The ASPX page follows:

Class MyASPXPage
...

Public Sub Page_Load(...)
MyClassProject.Class1.Generate()
End Sub
End Class


How can I make the Generate() method create a new aspx page with a web
control (Button) that, when clicked, will execute the event handler stored
in Class1? The new aspx page we want to be created does not exist on the
web server. We want to create it ourselves and possibly store it as a
resource in the Class Library project.

Basically, we want to include this class in any of our WebUI projects and if
certain criteria are met, this page would be loaded and prompt the user for
input. We do not want to create a new web application out of it (which is
what we are currently doing).

Any and all help appreciated.

Thanks,
Mythran
 
C

Cor Ligthert

Mythran,

There are 2 ways to use (create) ASPX pages scripting and with a DLL.
I post this from the language.VB newsgroup where the DLL way is mostly used.
In the ASPNET newsgroup you see often a lot of scripting solutions.

The Scripting format is where is said in the header to use VB or C# script
on the serverside and the DLL is where is told in the header use the class.

In my opinion can you to get your solution very good use the scripting
format.

Have just a look at gotdotnet how to make a scripting page.

That than dynamicly created page can you than in my idea place in the IIS
directory as start of your goal.

(Not that I tried this ever however this sound for me so obvious)

http://samples.gotdotnet.com/quickstart/

I hope this helps?

Cor



Cor
 
M

Mythran

Cor Ligthert said:
Mythran,

There are 2 ways to use (create) ASPX pages scripting and with a DLL.
I post this from the language.VB newsgroup where the DLL way is mostly
used.
In the ASPNET newsgroup you see often a lot of scripting solutions.

The Scripting format is where is said in the header to use VB or C# script
on the serverside and the DLL is where is told in the header use the
class.

In my opinion can you to get your solution very good use the scripting
format.

Have just a look at gotdotnet how to make a scripting page.

That than dynamicly created page can you than in my idea place in the IIS
directory as start of your goal.

(Not that I tried this ever however this sound for me so obvious)

http://samples.gotdotnet.com/quickstart/

I hope this helps?

Cor



Cor

Well, Cor, what we are trying to do is create a security system. We have
done quite a bit with security for asp.net and are trying this as a "try
only" test. What we want to do is create a .Net assembly which will be our
security library. This library will then contain a method, such as
Generate(). When the method is called, it will check all sorts of criteria,
just to make sure the user isn't already logged in. If they are not logged
in to this application, or another application, then we want the dll to show
a page. The page, though, should be output from the dll. The more I write
this, the more I think I can just spit out some generic html and then catch
the POST vars that are sent back from the security screen. Anywho, what I
was asking earlier was if there was a way to generate the ASPX page on the
fly. Or maybe store it into the assembly then spit it out when required. I
don't see how it could work, but I bet there is probably a way.

Not sure what I'm saying is clear enough for everyone to understand what we
are trying to do...

Thanks in advance and for help already given,

Mythran
 
C

Cor Ligthert

Mytrhan,

I am not interested what you exactly want to do, however for me your first
question was "how do I create a page on the fly" and on that I gave an
answer. I am not making an solution for you.

However to be sure if it was working, did I make this sample accoording to
my previous message, and it showed me a simple page made on the fly. This
will surely not fullfil al your needs quick reading your second page,
however is exact as my answer.

///Needs one button on a webpage and than paste in this code in the code
part
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim str As String = "<%@ Page Language='VB' %><html><head>" & _
"</head><body><center>" & _
"<form><p><%If Session.Item(""MyValue"") = ""Mythran"" then " &
vbCrLf & _
"Dim I As Integer" & vbCrLf & "For I = 0 to 7 %>" & vbCrLf & _
"<font size='<%=I%>'> Welcome to ASP.NET </font> <br>" & vbCrLf & _
"<% Next" & vbCrLf & _
"End if%>" & vbCrLf & _
"</form></center></body></html>"
Dim sw As New
IO.StreamWriter("C:\Inetpub\wwwroot\WebApplication1\mytest.aspx")
sw.Write(str)
sw.Close()
Session.Item("MyValue") = "Mythran"
Response.Redirect("mytest.aspx")
End Sub
///

However if this is not what you want, than I probably don't understand you.

I hope this helps?

Cor
 
M

Mythran

Cor Ligthert said:
Mytrhan,

I am not interested what you exactly want to do, however for me your first
question was "how do I create a page on the fly" and on that I gave an
answer. I am not making an solution for you.

However to be sure if it was working, did I make this sample accoording to
my previous message, and it showed me a simple page made on the fly. This
will surely not fullfil al your needs quick reading your second page,
however is exact as my answer.

///Needs one button on a webpage and than paste in this code in the code
part
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim str As String = "<%@ Page Language='VB' %><html><head>" & _
"</head><body><center>" & _
"<form><p><%If Session.Item(""MyValue"") = ""Mythran"" then " &
vbCrLf & _
"Dim I As Integer" & vbCrLf & "For I = 0 to 7 %>" & vbCrLf & _
"<font size='<%=I%>'> Welcome to ASP.NET </font> <br>" & vbCrLf & _
"<% Next" & vbCrLf & _
"End if%>" & vbCrLf & _
"</form></center></body></html>"
Dim sw As New
IO.StreamWriter("C:\Inetpub\wwwroot\WebApplication1\mytest.aspx")
sw.Write(str)
sw.Close()
Session.Item("MyValue") = "Mythran"
Response.Redirect("mytest.aspx")
End Sub
///

However if this is not what you want, than I probably don't understand
you.

I hope this helps?

Cor

Ok, I see what you are talking about here....now I'm understanding where we
are getting a little mis-understood. You are writing out an aspx page on
the file to the server's disk. What I was wondering was if there was a way
to create the page and send it to the client w/o writing it to the server's
disk first. From there, it would post back and fire events in the dll that
created the page...

See where I'm heading now?

Thanks :)

Mythran
 
C

Cor Ligthert

Mythran,
What I was wondering was if there was a way to create the page and send it
to the client w/o writing it to the server's disk first. From there, it
would post back and fire events in the dll that created the page...

That is not any problem, you can redirect that page as a string (Not in the
way I showed you however in clear HTML). However, than you are loosing all
the functionality from the server. That was exactly why I wrote as extra too
the dotnet page sample that session functionality in it. Please try to
understand the answers first before you start typing.

However I don't see the problem. This is a sample, probably I would in a
real situation give it as page name a Guid and deleted it as soon as it was
redirected. However that I did not try, that kind of functionality you would
have to do yourself in my opinion or ask for it. As I said, I am not making
your solution, only giving some hints. And in this case an even very
extended hint.

I hope this helps?

Cor
 
M

Mythran

Cor Ligthert said:
Mythran,


That is not any problem, you can redirect that page as a string (Not in
the way I showed you however in clear HTML). However, than you are loosing
all the functionality from the server. That was exactly why I wrote as
extra too the dotnet page sample that session functionality in it. Please
try to understand the answers first before you start typing.

However I don't see the problem. This is a sample, probably I would in a
real situation give it as page name a Guid and deleted it as soon as it
was redirected. However that I did not try, that kind of functionality you
would have to do yourself in my opinion or ask for it. As I said, I am not
making your solution, only giving some hints. And in this case an even
very extended hint.

I hope this helps?

Cor

Thanks for the help.

Mythran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top