i am adding dynamic control (textbox) and i set the property to visible = false
but i dont find the control in the tree collection, i want to hide it from user se
I believe that putting Visible="false" onto a control with stop it rendering to the browser, so no, you won't find it.
Instead, you can try using an HtmlInputHidden control, which will generate a hidden input field.
Hidden fields aren't a great way of doing things since they're able to be modified fairly easily on the client side using various intercept tools.
If visible is false, it wont be rendered to the page. Also for you requirement HTMLInputHidden control can be used.
Since you are creating the TextBoxesbased on org.Registrations, you can use a Repeater in the ItemTemplate of GirdVIew and then bind the org.Registrations as DataSource to the repeater in the RowCreated event. i.e: in your 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>
in your code behind
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.
}
}
Ignore the following text based on OP's comment
Instead of creating the TextBox dynamically, add the Textbox in the Grid ItemTemplate in ASPX and then use e.Row.Cells.FindControl to access the textbox. Something like:
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);
}
}
}