Reputation: 4897
I have a repeater, which contains a text box (html) in the ItemTemplate.
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.
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#).
Could anyone tell me if this is at all possible? If so, could some code be suggested?
Otherwise, any non-AJAX alternatives?
Upvotes: 0
Views: 8555
Reputation: 3951
There is no issue with referencing asp.net components in jQuery. A couple of things to know about this:
<asp:TextBox ID="txtBox" runat="server" />
becomes <input id="...._txtBox" name="...$txtBox" />
in the HTML. A jQuery selector like: $(input[id$=txtBox])
will find it.Upvotes: 1
Reputation: 6249
This should do it. Let me know if you want/need any further explanation.
put this in your aspx page:
<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();
}
}
Upvotes: 1