我正在添加动态控件(文本框),我将属性设置为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);
}
}
}
答案 0 :(得分:2)
由于您正在org.Registrations上创建TextBoxes,因此您可以在GirdVIew的ItemTemplate中使用Repeater,然后将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>
您的代码 中的
根据OP的评论忽略以下文字 不是动态创建TextBox,而是在ASPX中的Grid ItemTemplate中添加Textbox,然后使用e.Row.Cells.FindControl访问文本框。类似的东西: 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.
}
}
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);
}
}
}
答案 1 :(得分:1)
我相信将Visible =“false”放到控件上并停止渲染到浏览器,所以不会,你找不到它。
相反,您可以尝试使用HtmlInputHidden控件,它将生成一个隐藏的输入字段。
隐藏字段不是一种很好的处理方式,因为它们可以使用各种拦截工具在客户端轻松修改。
答案 2 :(得分:0)
如果可见为false,则不会将其呈现给页面。您也可以使用HTMLInputHidden控件。