Dot NET
Dot NET

Reputation: 4897

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.

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

Answers (2)

drdwilcox
drdwilcox

Reputation: 3951

There is no issue with referencing asp.net components in jQuery. A couple of things to know about this:

  1. The item will an have id that includes the ID value that you originally gave the control. Using the current encoding scheme (which I cannot imaging Microsoft changing significantly), <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.
  2. Because the asp controls eventually resolve to good-old HTML form elements, you treat them exactly like an HTML element you created directly. In this case, you enable/disable it using the pseudo-class disabled.
  3. I learned much of this by viewing the generated source for my web pages and seeing what was happening. I like FireFox with the Web Developer's Toolbar and its View Generated Source command.

Upvotes: 1

Shai Cohen
Shai Cohen

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

Related Questions