Here's a quick example.
This is in C# as I don't do VB.NET unfortunately. But this should be straight forward for you to follow and convert. I've updated the code and added a VB.NET equivalent.
ASPX (removed the styles for simplicity):
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Field1" HeaderText="Field 1" />
<asp:TemplateField>
<ItemTemplate>
<tr>
<td colspan="3">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebFormDemo
{
public partial class Repeater : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
BindGridView();
}
}
private void BindGridView() {
var data = GetSampleData();
GridView1.DataSource = data;
GridView1.DataBind();
}
private List<Student> GetSampleData() {
List<Student> students = new List<Student>();
students.Add(new Student() { Id = 1, Field1 = "SomeText 1", Field2 = "SomeText 1" });
students.Add(new Student() { Id = 2, Field1 = "SomeText 2", Field2 = "SomeText 2" });
students.Add(new Student() { Id = 3, Field1 = "SomeText 3", Field2 = "SomeText 3" });
return students;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
int id = Convert.ToInt32(e.Row.Cells[0].Text);
TextBox tb = (TextBox)e.Row.FindControl("TextBox1");
tb.Text = "Assign whatever value here";
}
}
}
public class Student
{
public int Id { get; set; }
public string Field1 { get; set; }
public string Field2 { get; set; }
}
}
The key there is the
RowDataBound
event. That event is fired when a grid is bounded with data, that's why it's a perfect place to access your
TextBox
and assign a value to it.
Here's the VB.NET equivalent using a
convert tool:
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace WebFormDemo
Public Partial Class Repeater
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
BindGridView()
End If
End Sub
Private Sub BindGridView()
Dim data = GetSampleData()
GridView1.DataSource = data
GridView1.DataBind()
End Sub
Private Function GetSampleData() As List(Of Student)
Dim students As List(Of Student) = New List(Of Student)()
students.Add(New Student() With {
.Id = 1,
.Field1 = "SomeText 1",
.Field2 = "SomeText 1"
})
students.Add(New Student() With {
.Id = 2,
.Field1 = "SomeText 2",
.Field2 = "SomeText 2"
})
students.Add(New Student() With {
.Id = 3,
.Field1 = "SomeText 3",
.Field2 = "SomeText 3"
})
Return students
End Function
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim id As Integer = Convert.ToInt32(e.Row.Cells(0).Text)
Dim tb As TextBox = CType(e.Row.FindControl("TextBox1"), TextBox)
tb.Text = "Assign whatever value here"
End If
End Sub
End Class
Public Class Student
Public Property Id As Integer
Public Property Field1 As String
Public Property Field2 As String
End Class
End Namespace