[英]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組件沒有問題。 有幾點需要了解:
<asp:TextBox ID="txtBox" runat="server" />
變為<input id="...._txtBox" name="...$txtBox" />
HTML中的<input id="...._txtBox" name="...$txtBox" />
。 一個jQuery選擇器,如: $(input[id$=txtBox])
會找到它。 這應該做到這一點。 如果您需要/需要任何進一步的解釋,請告訴我。
把它放在你的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.