finding control in collection tree

前端 未结 3 1294
眼角桃花
眼角桃花 2021-01-26 00:30

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

相关标签:
3条回答
  • 2021-01-26 00:52

    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.

    0 讨论(0)
  • 2021-01-26 00:52

    If visible is false, it wont be rendered to the page. Also for you requirement HTMLInputHidden control can be used.

    0 讨论(0)
  • 2021-01-26 01:03

    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);          
            }        
        }     
    } 
    
    0 讨论(0)
提交回复
热议问题