In this article I will explain with an example, how to get TextBox value from Repeater on Button click in ASP.Net using C# and VB.Net.
The Repeater Item will be referenced using the NamingContainer property and then the TextBox will be referenced using the FindControl method in ASP.Net.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
HTML Markup
The following HTML Markup consists of an ASP.Net Repeater control with an HTML Table Layout.
The Repeater’s ItemTemplate consists of two Label controls and a TextBox control.
<asp:Repeater ID="rptCustomers" runat="server">
<HeaderTemplate>
<table cellspacing="0" rules="all" border="1">
<tr>
<th scope="col" style="width: 80px">
Customer Id
</th>
<th scope="col" style="width: 120px">
Name
</th>
<th scope="col" style="width: 100px">
Country
</th>
<th>
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblCustomerId" runat="server" Text='<%# Eval("CustomerId") %>' />
</td>
<td>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>' />
</td>
<td>
<asp:TextBox ID="txtCountry" runat="server" Text='<%# Eval("Country") %>' />
</td>
<td>
<asp:Button Text="Get Value" runat="server" OnClick="GetValue" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Populating Repeater control from database in ASP.Net
Inside the Page load event handler, the Repeater is populated with the records from the Customers table.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindRepeater();
}
}
private void BindRepeater()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
rptCustomers.DataSource = dt;
rptCustomers.DataBind();
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindRepeater()
End If
End Sub
Private Sub BindRepeater()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Customers", con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
rptCustomers.DataSource = dt
rptCustomers.DataBind()
End Using
End Using
End Using
End Sub
Get TextBox value from Repeater on Button click in ASP.Net
When the Button is clicked, first the Repeater Item is referenced using the NamingContainer property.
Then the Label and the TextBox controls are referenced from the Repeater Item using the FindControl method.
Finally the values of Label and TextBox controls are displayed using JavaScript Alert Message Box.
C#
protected void GetValue(object sender, EventArgs e)
{
//Reference the Repeater Item using Button.
RepeaterItem item = (sender as Button).NamingContainer as RepeaterItem;
//Reference the Label and TextBox.
string message = "Customer Id: " + (item.FindControl("lblCustomerId") as Label).Text;
message += "\\nName: " + (item.FindControl("lblName") as Label).Text;
message += "\\nCountry: " + (item.FindControl("txtCountry") as TextBox).Text;
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + message + "');", true);
}
VB.Net
Protected Sub GetValue(ByVal sender As Object, ByVal e As EventArgs)
'Reference the Repeater Item using Button.
Dim item As RepeaterItem = TryCast((TryCast(sender, Button)).NamingContainer, RepeaterItem)
'Reference the Label and TextBox.
Dim message As String = "Customer Id: " & (TryCast(item.FindControl("lblCustomerId"), Label)).Text
message += "\nName: " & (TryCast(item.FindControl("lblName"), Label)).Text
message += "\nCountry: " & (TryCast(item.FindControl("txtCountry"), TextBox)).Text
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('" & message & "');", True)
End Sub
Screenshot
Demo
Downloads