Subject: Using javascript for dynamic asp: control
Posted By: VictorVictor Post Date: 8/28/2006 3:12:29 PM
Hello All:

Below appears the source for a trivial ASP.NET 2. micro project for testing javascript's ability to build window pages dynamically.  The program below works.

However, what does not work is substituting an <asp:Image> control for the <img> html control.  I've tried for a full day to make the substitution work but have failed.

Anyone tell me how to make it work?

BETTER YET, can anyone tell me the name and ISBN number of a book that would show me how to dynamically create windows pages with lots of different kinds of <asp:XXcontrolXX>s?

VictorVictor


Reply By: VictorVictor Reply Date: 8/28/2006 3:15:50 PM
Forgot to attach the source code.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Dynamic Pop-up Example</title>
</head>
<body>
    <form id="form1" runat="server" >
    </form>
</body>
</html>
--------------------------------------------------------------------------------
Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        Me.ClientScript.RegisterClientScriptInclude("DefaultPage_javascript_Key", "Default.js")
    End Sub
End Class
--------------------------------------------------------------------------------
// JScript File
 var popwindow = window.open("","","top=40,left=30,width=500,height=250");
    popwindow.document.open();
    popwindow.document.write("<title>Pop-up</title>");
    popwindow.document.write("Dynamic Pop-up Page<br/>");
    popwindow.document.write("<img src='HikeFT.gif' alt=''>");
    popwindow.document.close();


Reply By: Imar Reply Date: 8/28/2006 4:26:30 PM
ASP.NET is a server side technology, while JavaScript is a client side technology.

You can't create server side pages with a client side language and expect things to work. For a server side <asp:Image> to become a client side <img> it has to be compiled and be part of the whole ASP.NET processing pipe-line after which it ends up as a client side <img /> tag.

What is your intention with this? Why do you think it's useful to create "server side pages" at the client and why do you want to do this?

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
Reply By: VictorVictor Reply Date: 8/29/2006 12:10:51 PM
Imar:

Thanks for your response.  I am interested in creating asp: controls by using javascript, for two reasons.

1.  I am interested in learning how much I can do using javascript.  Obviously I can it to create html controls on a page and manipulate any of the page's DOM objects.  I had hoped that such creation would include an asp: object.  Guess not.
  
2.  The particular app I am working on will offer users, who are not programmers, but rather semi-skilled computer users, the ability to build asp pages for sending to the server.  I picture something like a wizard facility where the user specifies enough detail for me to build the asp page dynamically.  I had hoped to use javascript for this.  For example, in my app, a user needs to be able to build a page containing a schedule of future monthly activities for a hiking club.  The particular line items (hikes) are not generally known until club meetings have occurred wherein the details of hikes are decided upon.  Once the details have been established, when, where, description, etc. I want the user to enter the details into textboxes, etc, and click a Build button.  The functionality under the Build button builds the asp page, sends it to the server, and integrates it into the overall set of server pages.  It's important that I remove myself from the app, and not build pages myself.  I'm trying to find a way for the unsophisticated user to do that.

Instead of using javascript to do all this, my only other hope would be to get into the meta code and/or MicroSoft Intermediate Language .  And, I am new to both javascript and ASP.NET 2.0.  So I don't think I have the skill to do that (much less the time to learn how to write MSIL).  Finally, I am one developer working alone and time is a definite scarce resource.  If including the 'wizard' capability isn't simple or readily available for implementation via some simple mechanism,then I will have to abandon that app feature.

Thanks for your help.

VV



Reply By: Imar Reply Date: 8/29/2006 12:34:05 PM
Right, I see. But since JavaScript runs at the client, this plan will never work.

However, MSIL is on the other side of your possibilities. With MSIL, you are probably not going to solve this as well.

IMO, you have a few options:

1. Use an ASPX form to generate controls at run-time. E.g. in Page_Load, you can do this:

Button myButton = new Button();
// Do something with the button
myPlaceHolder.Controls.Add(myButton);

This injects the button control into the Controls collection of the PlaceHolder control. This is a trivial example, but much more is possible.

2. Use a database for dynamic stuff. Depending on your needs and requirements you may not need all this complicated stuff. Instead, design a smart database schema that can hold variable data. Presenting the data may be difficult though, but it all depends on what you are doing.

3. Use a client side UI (with ASP.NET forms) and server side code to create new ASPX pages on the fly. You could simp,y build up a complete string with the final code, and then write it to disk.

4. Alternatively, you can take a look at the System.CodeDom namespace that allows you to create and compile code on the fly. I haven't done much with it, other than play with it from an academic perspective, but it's an interesting technology.

But, personally, I don't think you need to go this complex route. I think with a smart design and UI, you can build  flexible user interface that suits your needs, possible using option 1 to create controls on the fly.

Did you ever overcome the shock of hacking from this thread: http://p2p.wrox.com/topic.asp?TOPIC_ID=48504

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.

Go to topic 48162

Return to index page 191
Return to index page 190
Return to index page 189
Return to index page 188
Return to index page 187
Return to index page 186
Return to index page 185
Return to index page 184
Return to index page 183
Return to index page 182