在寻找收集树控件 [英] finding control in collection tree

查看:106
本文介绍了在寻找收集树控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加动态控制(文本框)和i属性设置为的visible = false ,但我不觉得树集合中的​​控制,我想从隐藏用户看到它和阅读的价值。

 保护无效gv_RowCreated(对象发件人,GridViewRowEventArgs E)
    {
       学生组织=(namespace.Students)(e.Row.DataItem);       的foreach(在org.Registrations登记REG)
       {
          INT _count = org.Registrations.Count;
          对于(INT ROWID = 0;&ROWID LT; _count; ROWID ++)
          {
             文本框txtBox =新的TextBox();
             txtBox.ID =_registration+ e.Row.RowIndex +_+ ROWID;
             txtBox.Text = reg.Name;
             txtBox.Visible =真;
             e.Row.Cells [7] .Controls.Add(txtBox);
         }
       }
    }


解决方案

由于您使用的org.Registrations创建TextBoxesbased,您可以在GirdVIew的ItemTemplate中使用一个中继器,然后org.Registrations作为数据源绑定到中继器在RowCreated事件。
即:
在你的aspx:

 < ASP:GridView的...... OnRowDataBound =gv_RowDataBound>



    < ASP:模板列...>
        <&ItemTemplate中GT;
            < ASP:直放站ID =rptRegistrations=服务器>
                < ASP:文本框的id =txtRegistration=服务器文本='<%#的eval(姓名)%>'>< / ASP:文本框>< BR />
            < / ASP:直放站>
        < / ItemTemplate中>
    < / ASP:模板列...>
< / ASP:GridView的>

在你的背后code

 保护无效gv_RowDataBound(对象发件人,GridViewRowEventArgs E)
        {
         如果(e.Row.RowType == DataControlRowType.DataRow)
    {
             学生组织=(namespace.Students)(e.Row.DataItem);
             中继rptrRegistrations = e.Row.Cells [7] .FindControl(rptrRegistrations)作为中继器;
             rptrRegistrations.DataSource = org.Registrations;
             rptrRegistrations.DataBind();
         }
        } 公共无效gv_SelectedIndexChanged(对象发件人,EventArgs的发送)
  {    //获取使用SelectedRow属性当前所选行。
    GridViewRow排= gvOrg.SelectedRow;
        中继rptrRegistrations = row.Cells [7] .FindControl(rptrRegistrations)作为中继器;
        的foreach(在rptrRegistrations.Items的RepeaterItem项)
        {
            文本框txtRegistration = item.FindControl(txtRegistration)的文本框;
            //现在您可以访问该文本框。
        }
  }

忽略基于OP的评论下面的文本

而不是动态创建文本框的,添加文本框在网格中的ItemTemplate在ASPX,然后用e.Row.Cells.FindControl访问文本框。是这样的:

 保护无效gv_RowCreated(对象发件人,GridViewRowEventArgs E)
{
    学生组织=(namespace.Students)(e.Row.DataItem);
    的foreach(在org.Registrations登记REG)
    {
        INT _count = org.Registrations.Count;
        对于(INT ROWID = 0;&ROWID LT; _count; ROWID ++)
        {
            文本框txtBox = e.Row.Cells [7] .FindControl(txtRegistration)的文本框;
            //txtBox.ID =_registration+ e.Row.RowIndex +_+ ROWID;
            txtBox.Text = reg.Name;
            txtBox.Visible =真;
            //e.Row.Cells[7].Controls.Add(txtBox);
        }
    }
}

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 seeing it and read the value.

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);
         }
       }
    }

解决方案

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);          
        }        
    }     
} 

这篇关于在寻找收集树控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆