473,509 Members | 12,711 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ASP.NET adding controls at run time

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
Nov 15 '05 #1
1 6963
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.

"Jax" <an*******@discussions.microsoft.com> wrote in message
news:1f****************************@phx.gbl...
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

Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3705
by: avivgur | last post by:
Hello, I am writing a program in Visual C# and I have encountered a problem. In my program I want to dynamically create a multitude of controls (thousands) on a form. The problem is that calling...
3
4857
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
2
2088
by: Tim Marsden | last post by:
Hi, This is what I am doing, please comment if this is the correct way. I need to add controls to a form dynamically. Within the Page_Load event (is not Postback) I run a routine to create the...
4
2489
by: Bas Groeneveld | last post by:
I am developing an ASP.NET application part of which consists of a data entry wizard defined by entries in a data table - ie the controls on each page of the wizard are determined by definitions in...
0
1710
by: Sinisa Ruzin | last post by:
Hi all, I had problem with dynamically adding/removing controls;ascx, Controls.Add(Page.LoadControl... in the same page of the IBuySpy portal. ASP.NET, C#. I added buttons to the main ASCX loaded...
0
1863
by: Sileesh | last post by:
Hi I have html table and a Button in an Aspx page. I am adding one row with some textboxes to Html table each time i click on the Button thru Javascript. Now problem is when when i try to...
9
2752
by: Neo Geshel | last post by:
I have strip-mined, strip-searched, and completely exhausted the Internet (up to the 30th page on Google, with 100 results per page!!), all without finding an answer to my question AS TO WHY IT...
9
4467
by: Kadett | last post by:
Hi all, I have following problem: I'm creating a ListView (Details) control at run-time and filling it with some records (let's say 10 000). This operation seems to be quite fast, but when I call...
2
14971
by: _MC_ | last post by:
Hi, i want to structure two elements (each conists of 1 Label and 1 Textbox) in an Table. As I use an Content Place Holder, i thought it is possible to add the table via Controls.add(Literal)....
7
6179
by: =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?= | last post by:
I'm adding subheadings to a gridview. Each sub head has a few link buttons. I'm adding the controls in the rowdatabound event code follows: sorry about the length here. I have to be missing...
0
7137
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5659
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3219
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3210
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
445
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.