[英]How to get the value of an html textbox placed in a repeater, through ASP.NET code-behind?
I have a repeater, which contains a text box (html) in the ItemTemplate. 我有一个转发器,它包含ItemTemplate中的文本框(html)。
I cannot create an asp textbox, as the text box in question needs to be accessed through jQuery for user manipulation of the text box. 我无法创建一个asp文本框,因为有问题的文本框需要通过jQuery访问,以便用户操作文本框。
Once the user is done, an asp button is pressed in the respective repeater row, and I would like to get the value which the user has entered into the text box, through the code behind (c#). 一旦用户完成,在相应的转发器行中按下asp按钮,我想通过后面的代码(c#)获得用户输入文本框的值。
Could anyone tell me if this is at all possible? 谁能告诉我这是否可能? If so, could some code be suggested?
如果是这样,可以建议一些代码吗?
Otherwise, any non-AJAX alternatives? 否则,任何非AJAX替代品?
There is no issue with referencing asp.net components in jQuery. 在jQuery中引用asp.net组件没有问题。 A couple of things to know about this:
有几点需要了解:
<asp:TextBox ID="txtBox" runat="server" />
becomes <input id="...._txtBox" name="...$txtBox" />
in the HTML. <asp:TextBox ID="txtBox" runat="server" />
变为<input id="...._txtBox" name="...$txtBox" />
HTML中的<input id="...._txtBox" name="...$txtBox" />
。 A jQuery selector like: $(input[id$=txtBox])
will find it. $(input[id$=txtBox])
会找到它。 This should do it. 这应该做到这一点。 Let me know if you want/need any further explanation.
如果您需要/需要任何进一步的解释,请告诉我。
put this in your aspx page: 把它放在你的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>
put this in your code-behind: 把它放在你的代码隐藏中:
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.