Поскольку вы создаете TextBox на основе org.Registrations, вы можете использовать Repeater в ItemTemplate GirdVIew, а затем связать org.Registrations как DataSource с повторителем в событии RowCreated.
то есть:
в вашем aspx:
<asp:GridView ...... OnRowDataBound="gv_RowDataBound">
.
.
.
<asp:TemplateField ...>
<ItemTemplate>
<asp:Repeater id="rptRegistrations" runat="server">
<asp:TextBox id="txtRegistration" runat="server" Text='<%#Eval("Name")%>'></asp:TextBox><br/>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField ...>
</asp:GridView>
в вашем коде позади
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
Students org = (namespace.Students )(e.Row.DataItem);
Repeater rptrRegistrations = e.Row.Cells[7].FindControl("rptrRegistrations") as Repeater ;
rptrRegistrations.DataSource = org.Registrations;
rptrRegistrations.DataBind();
}
}
public void gv_SelectedIndexChanged(Object sender, EventArgs e)
{
// Get the currently selected row using the SelectedRow property.
GridViewRow row = gvOrg.SelectedRow;
Repeater rptrRegistrations = row.Cells[7].FindControl("rptrRegistrations") as Repeater;
foreach(RepeaterItem item in rptrRegistrations.Items)
{
TextBox txtRegistration = item.FindControl("txtRegistration") as TextBox;
//Now you have access to the textbox.
}
}
Игнорировать следующий текст на основе комментария ОП
Вместо того, чтобы создавать TextBox динамически, добавьте Textbox в шаблон элемента Grid Item в ASPX и затем используйте e.Row.Cells.FindControl для доступа к текстовому полю. Что-то вроде:
protected void gv_RowCreated(object sender, GridViewRowEventArgs e)
{
Students org = (namespace.Students )(e.Row.DataItem);
foreach (Registration reg in org.Registrations)
{
int _count = org.Registrations.Count;
for (int rowId = 0; rowId < _count; rowId++)
{
TextBox txtBox = e.Row.Cells[7].FindControl("txtRegistration") as TextBox ;
//txtBox.ID = "_registration" + e.Row.RowIndex + "_" + rowId;
txtBox.Text = reg.Name;
txtBox.Visible = true;
//e.Row.Cells[7].Controls.Add(txtBox);
}
}
}