我有一个gridview,我有很少的记录,我在gridview顶部有一个表格单元格,带有一个文本框和一个按钮来过滤记录。如何找到该文本框的控件。
protected void GridView2_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
GridViewRow HeaderGridRow = new GridViewRow(0, 1, DataControlRowType.Header, DataControlRowState.Normal);
TableCell HeaderCell = new TableCell();
HeaderCell.Text = "";
TextBox abc = new TextBox();
HeaderCell.Controls.Add(abc);
HeaderCell.ColumnSpan = 3;
HeaderGridRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
HeaderCell.Text = "";
TextBox search = new TextBox();
search.ID = "search1";
HeaderCell.Controls.Add(search);
HeaderCell.ColumnSpan = 3;
HeaderGridRow.Cells.Add(HeaderCell);
Control ctrl = HeaderGridRow.Cells[1].Controls[0];
TextBox tc = ctrl as TextBox;
HeaderCell = new TableCell();
Button btn_chk = new Button();
btn_chk.Text = "go";
HeaderCell.Controls.Add(btn_chk);
HeaderCell.ColumnSpan = 1;
btn_chk.Click += new EventHandler(btnClick);
HeaderGridRow.Cells.Add(HeaderCell);
//btn_chk.CommandName = "btn_chk";
GridView2.Controls[0].Controls.AddAt(0, HeaderGridRow);
}
}
html part
<asp:GridView ID="GridView2" runat="server" OnRowCreated="GridView2_RowCreated">
<Columns>
<asp:TemplateField>
<%-- <HeaderTemplate>
Planning Id<br />
<asp:TextBox ID="txt_planningid" runat="server"></asp:TextBox>
</HeaderTemplate>--%>
<HeaderTemplate>
<asp:Label ID="Label9" runat="server" Text="S.No"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text="<%#Container.DataItemIndex+1 %> "></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
我在rowcreated事件中使用表格单元格添加了两个文本框和一个按钮。
答案 0 :(得分:0)
我认为这就是你所追求的,
将RowDataBound事件添加到Gridview
OnRowDataBound="GridView2_RowDataBound"
使用RowDataBound事件处理事件并找到Textbox:
protected void GridView2_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
// Find the textbox
TextBox abc = (TextBox)e.Row.FindControl("abc");
}
}