[英]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.