Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using server controls on html pages 1

Status
Not open for further replies.

dragonwell

Programmer
Oct 21, 2002
863
US
Does anyone know how to use the output of a ASP.NET page to write html into a non-.NET page, such as a static HTML page?

I knwo you can do something like:
Code:
<img src="[URL unfurl="true"]http://myserver.com/MakeImage.aspx">[/URL]
If the MakeImage.aspx page outputs a dynamic image, you can use that tag on an html page to get a dynamic image.

What I want to do is more complicated, though - ultimately I want to get a block of HTML text from the server based on values read in from cookies. I have the aspx page that will render the output I need, using querystring values. Now I just need to figure out the javascript (probably using document.write()) that will "call" or "execute" the asp.net page.

Anyone have any background in this?

Thanks!

-d
 
An include directive MAY work, but it would have to be an .shtml page for server side includes to work.

<!-- #include file="/myweb/mypage.aspx" -->

I'm not really sure if it will work... just an idea. Is renaming the page to .shtml an option?

-paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
>>Is renaming the page to .shtml an option?

unfortunately not - but if it's clasic asp I CAN use the include directive. That might solve it...


Thanks,

-d
 
I did it!

If anyone's interested here is my solution.

First I put this code into the html page:
Code:
<script language=[blue]"javascript"[/blue]>
[green]//set a variable that the server control needs[/green]
[blue][b]var[/b][/blue] pageID=1;
</script>
<script src=[blue]"[URL unfurl="true"]http://myserver.com/ServerControl.js"[/URL][/blue]></script>

ServerControl.js sends the request to the server by writing another javascript src request with the varibales in the querystring. Contents of ServerControl.js:
Code:
[blue]if[/blue](!(pageID==null)){
    [blue]var[/blue] output = '<script src="[URL unfurl="true"]http://myserver.com/ServerControl.aspx?pageID=';[/URL]
    output+=pageViewID;
    output+='"></script>';

    document.write(output);
}

So when that runs, it writes the following string into the html:
<script src="
When that happens the following asp.net page is executed:
Code:
[blue]Public Class[/blue] ServerControl
    [blue]Inherits[/blue] System.Web.UI.Page

    [blue]Private[/blue] _pageID [blue]As[/blue] Int32
   
    [blue]Private Sub[/blue] Page_Load([blue]ByVal[/blue] sender [blue]As[/blue] System.Object, [blue]ByVal[/blue] e [blue]As[/blue] System.EventArgs) [blue]Handles[/blue] MyBase.Load

        [green]'get the variable (pageID) from the querystring[/green]
        [blue]Me[/blue]._pageID = Int32.Parse(Request.QueryString("pageID").ToString())

    [blue]End Sub[/blue]

    [blue]Protected Overrides Sub[/blue] Render([blue]ByVal[/blue] writer [blue]As[/blue] HtmlTextWriter)

        [green]'make sure the variable got set[/green]
        [blue]If[/blue] ([blue]Me[/blue]._pageViewID = 0) [blue]Then
            Return
        End If[/blue]

        [green]'get a new server control to be rendered[/green]
        [blue]Dim[/blue] ctrl [blue]As[/blue] MyServerControl = [blue]New[/blue] MyServerControl()
        [green]'set the property of the control[/green]
        ctrl.PageViewID = [blue]Me[/blue]._pageViewID

        [green]'start the javascript method that will write it to page[/green]
        writer.Write("document.write('")

        [green]'render the Control into the stream[/green]
        ctrl.RenderControl(writer)

        [green]'close the javascript method[/green]
        writer.Write("');")

   [blue]End Sub[/blue]
[blue]End Class[/blue]

So when THAT's done, we end up with a javascript call to document.write() with the HTML of the server control sent as the parameter.

This opens up a lot of possibilities for letting people put your .NET server controls on any type of page (html, asp, php, jsp, etc.)

I am using it for traffic monitoring and dynamic banner ad placement on legacy sites using a system I wrote in .NET.

Hope that helps someone!

-d
 
oops - wherever you see pageViewID I meant pageID ;)

(trying to make what I pasted more generalized, but i missed a couple)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top