我正在做一个关于航班预订系统的项目。我的职责是输入旅行乘客的详细信息。这些乘客可能包括成人和儿童。所以我需要为所有旅行的乘客动态生成单独的标签和文本框,以便输入所有乘客的详细信息。
我怎样才能做到这一点?
我正在做一个关于航班预订系统的项目。我的职责是输入旅行乘客的详细信息。这些乘客可能包括成人和儿童。所以我需要为所有旅行的乘客动态生成单独的标签和文本框,以便输入所有乘客的详细信息。
我怎样才能做到这一点?
您可能想查看 GridViews / DetailsView / Repeaters 等。然后您可以编写一些数据绑定代码来根据需要显示每条记录的不同标签。
正如其他人所说,需要更多信息才能正确回答问题。也许您可以发布一些您已经拥有的代码,以便我们可以为您指明下一个方向。
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 ;)
只是为了补充一些其他人所说的:如果您采用这种方法,那么我的猜测是您真的不了解 ASP.NET 在基础级别上是如何工作的。
如果你想显示一个乘客列表,那么你要做的最后一件事就是为每个乘客动态生成一个标签控件——在很多层面上都是错误的。
我能想到两种解决方案。首先,您可以在适当的位置放置一个标签,然后使用呈现乘客所需的 HTML 填充此标签(包括任何 href)。其次,您可以使用 GridView 或其同类控件之一来呈现此类信息。这是罗宾的建议,这确实是最好的方法。
不要太苛刻,而是从http://www.asp.net/learn/开始。如果您有更具体的问题,请编辑您原来的、非常模糊的问题。
在后面的代码中添加控件非常容易 - 看看controls.Add()方法。
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.