Asp.net 在集合树中查找控件

Asp.net 在集合树中查找控件,asp.net,gridview,Asp.net,Gridview,我正在添加动态控件(textbox),并将属性设置为visible=false,但我在树集合中找不到该控件,我想将其隐藏起来,以防用户看到并读取该值 protected void gv_RowCreated(object sender, GridViewRowEventArgs e) { Students org = (namespace.Students )(e.Row.DataItem); foreach (Registration reg in org

我正在添加动态控件(textbox),并将属性设置为
visible=false
,但我在树集合中找不到该控件,我想将其隐藏起来,以防用户看到并读取该值

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 = new TextBox();
             txtBox.ID = "_registration" + e.Row.RowIndex + "_" + rowId;
             txtBox.Text = reg.Name;
             txtBox.Visible = true;
             e.Row.Cells[7].Controls.Add(txtBox);
         }
       }
    }
protectedvoid gv_RowCreated(对象发送方,GridViewRowEventArgs e)
{
学生组织=(namespace.Students)(e.Row.DataItem);
foreach(在org.Registrations中注册)
{
int_count=org.Registrations.count;
对于(int rowId=0;rowId<\u count;rowId++)
{
TextBox txtBox=新建TextBox();
txtBox.ID=“_注册”+e.Row.RowIndex+“x”+rowId;
txtBox.Text=reg.Name;
txtBox.Visible=true;
e、 Row.Cells[7].Controls.Add(txtBox);
}
}
}

由于您是基于org.Registrations创建文本框,因此可以在GirdVIew的ItemTemplate中使用Repeater,然后将org.Registrations作为数据源绑定到RowCreated事件中的Repeater。 即: 在您的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>
根据OP的评论忽略以下文本

不要动态创建文本框,而是在ASPX的Grid ItemTemplate中添加文本框,然后使用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);          
        }        
    }     
} 
protectedvoid gv_RowCreated(对象发送方,GridViewRowEventArgs e)
{        
学生组织=(namespace.Students)(e.Row.DataItem);
foreach(在org.Registrations中注册)
{           
int_count=org.Registrations.count;
对于(int rowId=0;rowId<\u count;rowId++)
{              
TextBox txtBox=e.Row.Cells[7]。FindControl(“txtRegistration”)作为TextBox;
//txtBox.ID=“_注册”+e.Row.RowIndex+“x”+rowId;
txtBox.Text=reg.Name;
txtBox.Visible=true;
//e、 Row.Cells[7].Controls.Add(txtBox);
}        
}     
} 

由于您是基于org.Registrations创建文本框,因此可以在GirdVIew的ItemTemplate中使用Repeater,然后将org.Registrations作为数据源绑定到RowCreated事件中的Repeater。 即: 在您的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>
根据OP的评论忽略以下文本

不要动态创建文本框,而是在ASPX的Grid ItemTemplate中添加文本框,然后使用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);          
        }        
    }     
} 
protectedvoid gv_RowCreated(对象发送方,GridViewRowEventArgs e)
{        
学生组织=(namespace.Students)(e.Row.DataItem);
foreach(在org.Registrations中注册)
{           
int_count=org.Registrations.count;
对于(int rowId=0;rowId<\u count;rowId++)
{              
TextBox txtBox=e.Row.Cells[7]。FindControl(“txtRegistration”)作为TextBox;
//txtBox.ID=“_注册”+e.Row.RowIndex+“x”+rowId;
txtBox.Text=reg.Name;
txtBox.Visible=true;
//e、 Row.Cells[7].Controls.Add(txtBox);
}        
}     
} 

如果visible为false,则不会呈现到页面上。根据您的要求,还可以使用HTMLInputHidden控件。

如果visible为false,则不会呈现到页面。也可以根据您的要求使用HTMLInputHidden控件。

我认为将Visible=“false”放在控件上,并停止将其呈现到浏览器,这样您就找不到它了

相反,您可以尝试使用HtmlInputHidden控件,该控件将生成一个隐藏的输入字段


隐藏字段并不是一种很好的处理方式,因为它们可以在客户端使用各种截取工具相当容易地进行修改。

我认为将Visible=“false”放在控件上,然后停止渲染到浏览器,这样您就找不到它了

相反,您可以尝试使用HtmlInputHidden控件,该控件将生成一个隐藏的输入字段



隐藏字段不是一种很好的方法,因为它们可以在客户端使用各种截取工具轻松地修改。

您不应该将Row.RowIndex添加到文本框的ID中。GridViewRow中每个控件的命名容器就是GridViewRow本身。因此,您可以并且应该在下一行使用相同的ID。我已经在使用e.row。rowindex@Time,我在这里贴了为什么我最终使用了,也许你能给我一些启示?如果您使用的是HtmlInputHidden,则不必设置visible=False。您不应将Row.RowIndex添加到文本框的ID中。GridViewRow中每个控件的命名容器都是GridViewRow本身。因此,您可以并且应该在下一行使用相同的ID。我已经在使用e.row。rowindex@Time,我在这里贴了为什么我最终使用了,也许你能给我一些启示?如果你使用的是HtmlInputHidden,那么你不必设置visible=False,我希望我可以使用,但我有一种情况,我必须创建动态控件,我已经在这里发布了问题,更新了帖子。。您可能需要稍微调整发布的代码以满足您的需要..但这给了您一个想法..@Cybernate:如果您查看我的代码,我正在执行
for
loop语句,根据我从db获得的计数创建一个文本框,但查看您的代码,您在.aspx页面中有声明性文本框。。。。我想知道这对我的情况有什么帮助?我需要一些更具活力的东西。你正在将注册绑定到转发器,转发器根据注册中的项目呈现文本框。尝试代码,你会发现,一旦你看到它的工作…我希望我可以使用,但我有一个情况,我必须创建动态控制,我已经张贴在这里的问题更新后。。你可能需要