簡體   English   中英

在ASP.NET中在TextBoxes周圍加載內容

[英]Loading content around TextBoxes in ASP.NET

我正在嘗試在ASP.NET中創建一個表單,但是我想知道是否可以在TextBox周圍動態添加HTML內容。

這是代碼:

<%@ 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 --%>

正如您在代碼中看到的那樣,每個問題中<div>和<button>標記的內容都沒有改變,所以我想知道是否有某種方法可以從腳本中加載它們,以便在頁面出現時加載它們會自動環繞每個TextBox。 提前致謝。

如果只需要添加一個問題,可以使用<asp:label />標記,甚至是Html label標記,並將其設置為runat="server" 然后,您可以在后面的代碼中將文本動態添加到標簽中

每個頁面的總問題是否不同或相同? 如果不同,則可以嘗試使用asp:Repeater 您可以動態更改問題,並使每個問題的按鈕和div保持靜態。

例如 :

<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>

編輯:

要編輯標簽文本,您可以首先將所有問題添加到List<String>然后將其綁定到轉發器,這是示例:

在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事件上:

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;
            }
        }

您無需更改Label和TextBox的ID,因為對於中繼器中的每個項目,ID總是不同的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM