围绕ASP.NET文本框内容加载 [英] Loading content around TextBoxes in ASP.NET
问题描述
我想在ASP.NET中创建一个形式,但我想知道我是否可以动态地添加围绕一个文本框HTML内容。
I'm trying to create a form in ASP.NET but I'd like to know if I can dynamically add HTML content around a TextBox.
下面是code:
<%@ Page Title="" Language="C#" MasterPageFile="~/MPpacientes.master" AutoEventWireup="true" CodeFile="CuestionarioGeneral.aspx.cs" Inherits="CuestionarioGeneral" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="navbar" Runat="Server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="headertitle" Runat="Server">
Cuestionario General
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="contentbox" Runat="Server">
<legend>Lorem ipsum dolor sia met etcetcetc</legend>
<%-- question1 --%>
<asp:Label ID="lbl1" runat="server" Text="1"></asp:Label>
<div class="input-control text" data-role="input-control">
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
<button class="btn-clear" tabindex="-1"></button>
</div>
<%-- question2 --%>
<asp:Label ID="lbl2" runat="server" Text="2"></asp:Label>
<div class="input-control text" data-role="input-control">
<asp:TextBox ID="txt2" runat="server"></asp:TextBox>
<button class="btn-clear" tabindex="-1"></button>
</div>
<%-- question3 --%>
<%-- etc --%>
正如你可以在code看到的内容在&lt; DIV>和&lt;按钮>标签不都在每个问题发生变化,所以我想知道是否有某种方式从脚本加载它们,这样在页面加载时会自动环绕每个文本框。
先谢谢了。
As you can see in the code, the content of the < div > and the < button > tags don't change at all in each question, so I wanna know if there's some way to load them from a script so that when the page loads they wrap around each TextBox automatically. Thanks in advance.
推荐答案
做的每一页的总问题不同或相同?
如果不同,你可以尝试使用 ASP:直放站
为。可以动态改变的问题,并保持按键和每一个问题div的静态的。
did the total question for each page different or the same?
if different, you can try to use asp:Repeater
for that. You can dynamically change the question, and keep the button and div static for every question.
例如:
<asp:Repeater ID="rptQuestion" runat="server" OnItemDataBound="rptQuestion_ItemDataBound">
<ItemTemplate>
<asp:Label ID="lblQuestion"></asp:Label>
<div class="input-control text" data-role="input-control">
<asp:TextBox ID="txtAnswer" runat="server"></asp:TextBox>
<asp:Button CssClass="btn-clear" TabIndex="-1" ID="btnClear" runat="server"></button>
</div>
</ItemTemplate>
</asp:Repeater>
编辑:
编辑标签文本,您可以将所有的问题首先添加到列表&LT;弦乐&GT;
然后将其绑定到中继器,这里的例子:
to edit the Label text, you can first add all your question to List<String>
then Bind it to the repeater, here's the example :
在pageLoad的:
on PageLoad :
protected void Page_Load(object sender, EventArgs e)
{
List<String> listQuestion = new List<String>();
foreach(string question in ...)
{
listQuestion.Add(question);
}
rptQuestion.DataSource = listQuestion;
rptQuestion.DataBind();
}
然后在ItemDataBound事件:
then on the ItemDataBound event :
protected void rptQuestion_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
String question = (String)e.Item.DataItem;
Label lblQuestion = (Label)e.Item.FindControl("lblQuestion");
lblQuestion.Text = question;
}
}
您不需要改变ID为标签和文本框,因为它总是会在中继器的每个项目不同的
you don't need to change the ID for the Label and TextBox, because it will always be different for each item in the repeater
这篇关于围绕ASP.NET文本框内容加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!