问题
I am doing a project on Flight Booking system. My part is to enter the details of the passengers travelling. These passengers may include Adults as well as children. So I need to dynamically generate separate labels and text boxes for all the passengers travelling so that details of all them can be entered.
How can I do that?
回答1:
You may want to look at GridViews / DetailsView / Repeaters etc. You can then write some databinding code to show the different labels for each record as required.
As others have said though, a bit more information is needed to answer the question properly. Maybe you could post some of the code you have already so we can point you in the next direction.
回答2:
You can generate a whole page by using Response.Write()
回答3:
Not to be too harsh, but start with http://www.asp.net/learn/. If you have a more specific question, then edit your original, very vague question.
回答4:
It is pretty easy to add controls in the code behind - take a look at the controls.Add() method.
回答5:
Just to augment what a few others have said: if you are taking this approach then my guess is that you really don't understand how ASP.NET works at a fundamental level.
If you want to show a list of passengers then the last thing you would do is to dynamically generate a label control for each passenger - it is just wrong on so many levels.
There are two solutions that I can think of. First, you might place a single label in an appropriate position and then fill this label with the HTML necessary to render your passengers (including any hrefs). Second, you could use a GridView or one of its kindred controls to render this kind of information. This was Robin's suggestion and it is really the best approach.
回答6:
You page contains a Controls Collection that you can use to append new Controls, as Labels and Textboxes, i.e.
You can access this Controls Collection using code-behind, accessing the Page.Controls property and appending the controls you want to display in the page, that will then be rendered.
You can check this out: http://msdn.microsoft.com/en-us/library/system.web.ui.control.createchildcontrols.aspx
Here is a simple example you may try out:
Codefront (aspx webform)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 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>Test Website</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<!-- here you can place the static text and other elements -->
<h1>TESTING</h1>
blah blah blah blah blah
</div>
<div id="placeholder" runat="server">
<!-- here is where the dinamically created elements will be placed -->
</div>
</form>
</body>
</html>
Code-behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page {
protected override void CreateChildControls(){
// Add a Label to the current ControlCollection.
Label lbl = new Label();
lbl.Text = "Label text";
placeholder.Controls.Add(lbl);
// Create a text box control, set the default Text property, and add it to the ControlCollection.
TextBox box = new TextBox();
box.Text = "Textbox text";
placeholder.Controls.Add(box);
}
}
Hope this help ;)
回答7:
Sometimes its much easier just to create your own server control. that way you can control the HTML which is outputted a lot easier and have greater control over different browsers and languages with less code.
来源:https://stackoverflow.com/questions/733831/asp-net-how-to-dynamically-generate-labels