How to get the value of a TextBox in an ASP.NET Repeater Control

By Peter Bromberg

The Repeater Control is a relatively simple ASP.NET control, but it can be used to display all kinds of tabular data via ItemTemplates and inserted markup. This explains how to click a button in a Repeater row and be able to get the value of a TextBox in that row. You can copy the code shown into an ASPX page and run it.

<%@ Page Language="C#" AutoEventWireup="true"  %>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<script runat="server">
protected void Page_Load (object sender, EventArgs e)
    if (!IsPostBack)
       DataTable dt = new DataTable();
        for (int i = 0; i < 10; i++)
           DataRow row = dt.NewRow();
            row.ItemArray = new object[] {i,"Joe","Blow" +i.ToString( )};
            Repeater1.DataSource = dt;

    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
         int rowid = (e.Item.ItemIndex);
       TextBox tb = (TextBox)Repeater1.Items[rowid].FindControl("txtOne");
        Label2.Text = tb.Text;
<html xmlns="">
<head runat="server">
    <title>Repeater Demo</title>
    <form id="form1" runat="server">
<asp:Repeater ID="Repeater1" runat="server" onitemcommand="Repeater1_ItemCommand">
<table border="1" width="50%">

  <td><asp:Button ID="btnOne" runat=server Text="SELECT"   /></td>
  <td> <asp:TextBox ID=txtOne runat=server Text='<%# Eval("ID") %>' /></td>
<td><%# Eval("Firstname") %></td>
<td><%# Eval("LastName") %></td>


    <asp:Label ID=Label2 runat=server></asp:Label>

How to get the value of a TextBox in an ASP.NET Repeater Control  (5196 Views)