ASP.NET adding controls at run time

  • Thread starter Thread starter Jax
  • Start date Start date
J

Jax

I am looking to populate web page with controls as a
result of a database query.

eg (pseudocode)
<%
foreach(Garment g in GetStuffFromDB())
{
//add controls
Response.Write("<asp:Button id = 'Button'"+ctr.ToString()
+"/>")
}
%>

This doesn't work at all, I get nothing, not even one
button in the corner, i'm not sure why, probably because
i'm using the wrong object to write or something.
It does work with standard HTML controls although
sometimes those controls sporadically decide not to show
up.
A more complete example of my code is below with the style
tag being assigned to.

<%
int startLeft = 200;
int startTop = 200;
int ctr = 101;
int placeCtr = 20;
ArrayList al = GetGarmentsFromDB();
foreach(Garment g in al)
{
Response.Write("<SELECT style = 'Z-INDEX: "+ctr.ToString()
+"; LEFT: 220px; POSITION: absolute;
TOP: "+placeCtr.ToString()+"px; WIDTH: 100px; HEIGHT:
50px' />");
Response.Write("<br></br>");
ctr++;
placeCtr += 50;
}
%>

Unfortunately if this code runs only two of the four
results show up. I get only two comboBoxes rather then
four. Strangely enough if I add:

Response.Write("<input style = 'Z-INDEX: "+ctr.ToString()
+"; LEFT: 220px; POSITION: absolute;
TOP: "+placeCtr.ToString()+"px; WIDTH: 100px; HEIGHT:
50px' />");

in the line after I add the html select I get 4 comboBoxes
and 4 inputs.

So to recap, WebControls don't work at all and html
elements work sporadically, what am I doing wrong?
Any help would be greatly appreciated.

jax
 
Jax: First of all, for the web controls:

the <asp:Button runat=server> is a server side-tag. (simplified)What
happens when a user requests a .aspx page with this tag in it is that the
ASP.NET framework compiles the .aspx page into a class, then runs the class.
When compiling it, the tag <asp:Button...> gets translated into an actual
Button object in that class. What you're trying to do here is take that
already compiled class, and send a server side tag to the client
(Response.Write sends what you provide directly to the client.) The client
won't have any idea what the html tag <asp:Button ... /> represents, and so
will not render it. What you're looking for is a Repeater control, which
repeats a template for each item of a data source:
(simplified - look up the Repeater control and play with it)
<asp:Repeater id="Repeater1" runat="server">
<ItemTemplate>
<asp:Button Runat=server Text='<%# DataBinder.Eval(Container.DataItem,
"SomeProp") %>'/>
</ItemTemplate>
</asp:Repeater>
<%
Repeater1.DataSource = myDataSource;
Repeater1.DataBind();
%>


For your second question, while I don't have access to a testbed right now
(which is why the above code is "rough"), I'd suspect that your tags aren't
being written correctly. Look at your output HTML source and verify that
only two items are being written out. Perhaps they all are, but only two are
displaying? Although again, you'd better off with a Repeater.

ASP.NET isn't just ASP with a c# syntax -- it's a whole new beast and will
require completely different approaches.
 
Back
Top