adding controls to asp .net form from the .cs file during runtime
I have a site (build with asp .net) and I want to add to it some text and pictures during runtime (I don't know the amount before).
After the user chooses the parameters, he clicks a button which invokes the following code (in the .cs file):foreach (var car in cars)
{
Label lbl = new Label ();
form1.Controls.Add(lbl);
lbl.Text = car.name;
Image myImage = new Image ();
form1.Controls.Add(myImage);
myImage.ImageUrl = car.imageURL;
}
but when I run the cod开发者_JAVA百科e, I don't see the controls on the page. am I missing something?
Thanks!You have to assign the control to the form BEFORE setting it's properties.
For example:
foreach (var car in cars){
Label lbl = new Label ();
form1.Controls.Add(lbl); // Create and immediately assign
lbl.Text = car.name; // now you can set some properties
Image myImage = new Image ();
form1.Controls.Add(myImage); // Ditto
myImage.ImageUrl = car.imageURL;
}
UPDATE:
I just created a brand new web application project with a page that only had a button on it; it worked as expected. I suggest you have something else going on. See below:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>
and the code behind:
namespace WebApplication1 {
public partial class _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
}
protected void Button1_Click(object sender, EventArgs e) {
for (Int32 i = 0; i < 10; i++) {
Label lbl = new Label();
form1.Controls.Add(lbl);
lbl.Text = "HAHAHAHAH";
}
}
}
}
Try adding a placeholder control onto your form. Then instead of calling Form1.Controls.Add(....) use PlaceHolder1.Controls.Add(.....)
When you create dynamic controls on a Web Form, the controls must be created and added to the controls collection either in the OnInit or in the Page_Load events.
Please refer this question in order to solve your issue.
You can try what AndyPeacock says but looking at your code I think what you need to research into is actually Repeaters and other databound controls like DataListView.
Here's a quick example of how you could implement this with a Repeater:
In your ASPX file:
<asp:Repeater ID="RepCars" runat="server">
<ItemTemplate>
<%# Eval("name") %>
<img src='<% Eval("imageURL") %>' alt="" />
</ItemTemplate>
</asp:Repeater>
And on your .CS part, probably in Page_Load:
RepCarImages.DataSource = cars;
RepCarImages.DataBind();
Have a look at http://www.w3schools.com/ASPNET/aspnet_repeater.asp
Not an answer, but the following stripped-down sample works for me:
<%@ Page Language="C#" AutoEventWireup="true" %>
<html>
<head runat="server"></head>
<body>
<form id="form1" runat="server">
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
var lit = new Literal();
Controls.Add(lit);
Controls.Add(new Literal() { Text = "aaaa" });
lit.Text = "some text -- ";
}
</script>
<asp:Button ID="Button1" runat="server" Text="Button"
onclick="Button1_Click" />
</form>
</body>
</html>
So it seems you have not posted enough information to answer your question?
精彩评论