簡體   English   中英

如何通過ASP.NET代碼隱藏獲取放置在轉發器中的html文本框的值?

[英]How to get the value of an html textbox placed in a repeater, through ASP.NET code-behind?

我有一個轉發器,它包含ItemTemplate中的文本框(html)。

我無法創建一個asp文本框,因為有問題的文本框需要通過jQuery訪問,以便用戶操作文本框。

一旦用戶完成,在相應的轉發器行中按下asp按鈕,我想通過后面的代碼(c#)獲得用戶輸入文本框的值。

誰能告訴我這是否可能? 如果是這樣,可以建議一些代碼嗎?

否則,任何非AJAX替代品?

在jQuery中引用asp.net組件沒有問題。 有幾點需要了解:

  1. 該項目的ID將包含您最初為控件提供的ID值。 使用當前編碼方案(我無法使Microsoft成像顯着變化), <asp:TextBox ID="txtBox" runat="server" />變為<input id="...._txtBox" name="...$txtBox" /> HTML中的<input id="...._txtBox" name="...$txtBox" /> 一個jQuery選擇器,如: $(input[id$=txtBox])會找到它。
  2. 因為asp控件最終解析為古老的HTML表單元素,所以您將它們視為您直接創建的HTML元素。 在這種情況下,您可以使用禁用的偽類啟用/禁用它。
  3. 通過查看我的網頁生成的源並查看發生的情況,我了解了很多這方面的知識。 我喜歡FireFox和Web Developer的工具欄及其View Generated Source命令。

這應該做到這一點。 如果您需要/需要任何進一步的解釋,請告訴我。

把它放在你的aspx頁面中:

<form id="form1" runat="server">
<div>
<asp:Repeater ID="rptHtmlTag" runat="server">
        <ItemTemplate>
            <input id="htmlTextBox" runat="server" />
        </ItemTemplate>
</asp:Repeater>
</div>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
</form>

把它放在你的代碼隱藏中:

protected override void OnInit(EventArgs e)
{
    this.rptHtmlTag.ItemDataBound += new RepeaterItemEventHandler(rptHtmlTag_ItemDataBound);
    this.btnSubmit.Click += new EventHandler(btnSubmit_Click);
    base.OnInit(e);
}

void btnSubmit_Click(object sender, EventArgs e)
{
    foreach (RepeaterItem item in this.rptHtmlTag.Items)
    {
        HtmlInputText htmlTextBox = (HtmlInputText)item.FindControl("htmlTextBox");
        string THIS_IS_YOUR_VALUE = htmlTextBox.Value;
    }
}

void rptHtmlTag_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        HtmlInputText htmlTextBox = (HtmlInputText )e.Item.FindControl("htmlTextBox");
        htmlTextBox.Value = String.Concat("Some Value - Index", e.Item.ItemIndex);
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        List<int> repeaterPopulator = new List<int> { 1, 2, 3 };
        this.rptHtmlTag.DataSource = repeaterPopulator;
        this.rptHtmlTag.DataBind();
    }
}

暫無
暫無

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

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