在 ASP.Net、C# 中以编程方式创建字段集、ol/ul 和 li 标签

发布于 2024-10-19 20:48:59 字数 1348 浏览 7 评论 0原文

我需要编写一个 ASP.Net 表单,它将生成以下 HTML:

<fieldset>   
<legend>Contact Details</legend>   
<ol>   
<li>   
<label for="name">Name:</label>   
<input id="name" name="name" class="text" type="text" />   
</li>   
<li>   
<label for="email">Email address:</label>   
<input id="email" name="email" class="text" type="text" />   
</li>   
<li>   
<label for="phone">Telephone:</label>   
<input id="phone" name="phone" class="text" type="text" />   
</li>   
</ol>   
</fieldset>  

但是,要添加到表单中的字段将在运行时确定,因此我需要在运行时创建字段集并添加有序列表和列表项适当地使用标签、文本框、复选框等。

我找不到将创建这些标签的标准 ASP.Net 对象。

例如,我想在 C# 中执行如下操作:

FieldSet myFieldSet = new FieldSet();
myFieldSet.Legend = “Contact Details”;
OrderedList myOrderedList = new OrderedList();

ListItem listItem1 = new ListItem();
ListItem listItem2 = new ListItem();
ListItem listItem3 = new ListItem();

// code here which would add labels and textboxes to the ListItems

myOrderedList.Controls.Add(listItem1);
myOrderedList.Controls.Add(listItem2);
myOrderedList.Controls.Add(listItem3);

myFieldSet.Controls.Add(myOrderedList);

Form1.Controls.Add(myFieldSet);

是否有任何标准 ASP.Net 对象可以生成此结果,或者是否有其他方法可以实现相同的结果?

马特

I need to write an ASP.Net form which will produce the following HTML:

<fieldset>   
<legend>Contact Details</legend>   
<ol>   
<li>   
<label for="name">Name:</label>   
<input id="name" name="name" class="text" type="text" />   
</li>   
<li>   
<label for="email">Email address:</label>   
<input id="email" name="email" class="text" type="text" />   
</li>   
<li>   
<label for="phone">Telephone:</label>   
<input id="phone" name="phone" class="text" type="text" />   
</li>   
</ol>   
</fieldset>  

However, the fields which are to be added to the form will be determined at runtime, so I need to create the fieldset at runtime and add an ordered list and listitems to it, with labels, textboxes, checkboxes etc as appropriate.

I can’t find standard ASP.Net objects which will create these tags.

For instance, I’d like to do something like the following in C#:

FieldSet myFieldSet = new FieldSet();
myFieldSet.Legend = “Contact Details”;
OrderedList myOrderedList = new OrderedList();

ListItem listItem1 = new ListItem();
ListItem listItem2 = new ListItem();
ListItem listItem3 = new ListItem();

// code here which would add labels and textboxes to the ListItems

myOrderedList.Controls.Add(listItem1);
myOrderedList.Controls.Add(listItem2);
myOrderedList.Controls.Add(listItem3);

myFieldSet.Controls.Add(myOrderedList);

Form1.Controls.Add(myFieldSet);

Are there any standard ASP.Net objects which can produce this, or is there some other way of achieving the same result?

Matt

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷弦 2024-10-26 20:48:59

你可以试试这个:

Panel myFieldSet = new Panel();
myFieldSet.GroupingText= “Contact Details”;
HtmlGenericControl myOrderedList = new HtmlGenericControl("ol");

HtmlGenericControl listItem1 = new HtmlGenericControl ("li");
HtmlGenericControl listItem2 = new HtmlGenericControl ("li");
HtmlGenericControl listItem3 = new HtmlGenericControl ("li");

// code here which would add labels and textboxes to the ListItems

myOrderedList.Controls.Add(listItem1);
myOrderedList.Controls.Add(listItem2);
myOrderedList.Controls.Add(listItem3);

myFieldSet.Controls.Add(myOrderedList);

Form1.Controls.Add(myFieldSet);

You can try this:

Panel myFieldSet = new Panel();
myFieldSet.GroupingText= “Contact Details”;
HtmlGenericControl myOrderedList = new HtmlGenericControl("ol");

HtmlGenericControl listItem1 = new HtmlGenericControl ("li");
HtmlGenericControl listItem2 = new HtmlGenericControl ("li");
HtmlGenericControl listItem3 = new HtmlGenericControl ("li");

// code here which would add labels and textboxes to the ListItems

myOrderedList.Controls.Add(listItem1);
myOrderedList.Controls.Add(listItem2);
myOrderedList.Controls.Add(listItem3);

myFieldSet.Controls.Add(myOrderedList);

Form1.Controls.Add(myFieldSet);
酷炫老祖宗 2024-10-26 20:48:59

根据上面的答案,我有一个实例,其中数组被证明是有用的,如下所示。注意:我在 aspx 代码中使用 id="ulNoTree" 创建了一个无序列表。

int NumFloorsCt = 10;
LinkButton[] rgBL;
HtmlGenericControl[] rgLI;

/// <summary>
/// set up an array of LinkButtons with "li" controls
/// - each LinkButton click is handled by the same event handler
/// </summary>
void SetUpLinkButtons(List<FLOOR> listFloorRecs)
{
  NumFloorsCt = 10;
  rgBL = new LinkButton[NumFloorsCt];
  rgLI = new HtmlGenericControl[NumFloorsCt];

  for (int i = 0; i < NumFloorsCt; i++)
  {
    rgBL[i] = new LinkButton();
    rgBL[i].ID = LB_ID_prefix + listFloorRecs[i].ID;
    rgBL[i].Click += new System.EventHandler(LB_fp_Click);
    rgBL[i].Text = listFloorRecs[i].DESCRIP;
    rgBL[i].ToolTip = "Click here to display floor info";

    rgLI[i] = new HtmlGenericControl("li");
    rgLI[i].Controls.Add(rgBL[i]);

    ulNoTree.Controls.Add(rgLI[i]);
  }
}

/// <summary>
/// event handler for any of the link buttons
/// </summary>
protected void LB_fp_Click(object sender, EventArgs e)
{
  LinkButton btn = (LinkButton)(sender);
  // do your action here
}

Per the answer above, I had an instance where an array proved to be useful as shown here. Note: I created an unordered list in the aspx code with id="ulNoTree".

int NumFloorsCt = 10;
LinkButton[] rgBL;
HtmlGenericControl[] rgLI;

/// <summary>
/// set up an array of LinkButtons with "li" controls
/// - each LinkButton click is handled by the same event handler
/// </summary>
void SetUpLinkButtons(List<FLOOR> listFloorRecs)
{
  NumFloorsCt = 10;
  rgBL = new LinkButton[NumFloorsCt];
  rgLI = new HtmlGenericControl[NumFloorsCt];

  for (int i = 0; i < NumFloorsCt; i++)
  {
    rgBL[i] = new LinkButton();
    rgBL[i].ID = LB_ID_prefix + listFloorRecs[i].ID;
    rgBL[i].Click += new System.EventHandler(LB_fp_Click);
    rgBL[i].Text = listFloorRecs[i].DESCRIP;
    rgBL[i].ToolTip = "Click here to display floor info";

    rgLI[i] = new HtmlGenericControl("li");
    rgLI[i].Controls.Add(rgBL[i]);

    ulNoTree.Controls.Add(rgLI[i]);
  }
}

/// <summary>
/// event handler for any of the link buttons
/// </summary>
protected void LB_fp_Click(object sender, EventArgs e)
{
  LinkButton btn = (LinkButton)(sender);
  // do your action here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文