在Gridview的itemTemplate中找到控件,没有Gridview事件

本文关键字:Gridview 控件 事件 没有 itemTemplate | 更新日期: 2023-09-27 18:06:02

我在Gridview的ItemTemplate中有一个文本框。我需要找到文本框不使用RowDatabound或Gridview的任何其他事件

aspx

<asp:GridView ID="gv_Others" runat="server" AutoGenerateColumns="false">
                    <Columns>
                    <asp:TemplateField >                        
                        <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                        <ItemTemplate >                     
                            <asp:TextBox ID="txtEmp" runat="server" Width=100% Height=22px CssClass="input"></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    </Columns>
                </asp:GridView> 

aspx.cs

protected void Insert_OtherServices()
    {
        dsJobCardTableAdapters.Select_OtherServiceTableAdapter dsother = new dsJobCardTableAdapters.Select_OtherServiceTableAdapter();
        int count = 0;
        foreach (GridViewRow row in gv_Others.Rows)
        {
            //string b = (row.Cells[0].Controls[0] as DataBoundLiteralControl).Text;
            //TextBox a = gv_Others.Rows[count].Cells[0].FindControl("txtEmp") as TextBox;
           // string test = a.Text;
            //TextBox other = (TextBox)gv_Others.Rows[count].FindControl("txtEmp");
            TextBox other = (TextBox)gv_Others.TemplateControl.FindControl("txtEmp");
            dsother.Insert_OtherService(other.Text);
            count++;
        }
    } 

是否可以从Gridview的项目模板内的文本框中获取值

在Gridview的itemTemplate中找到控件,没有Gridview事件

您的意思是要查找文本框值而不通过GridView行循环吗?如果是这种情况,可以使用jQuery/javascript来实现这一点。

  1. 读取客户端的所有文本框值。你可以在这里找到更多关于如何做到这一点的信息
  2. 使用隐藏字段将这些值与回发请求一起传递。读取和使用方法中的隐藏字段值。