none
问一个关于GridView中FindControl的问题. RRS feed

  • 问题

  •  

    我的GridView中的有2个TemplateField,其中一个放了一个textbox1,另一个放了button1,button1的commandname=click,我想单击button1获取textbox1 中的内容,

    Code Snippet

    protected void gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "click")
            {
               
                TextBox tb = gridview1.FindControl("textbox1") as TextBox;
                Response.Write(tb.Text);
            }

     

    总是找不到这个textbox

    我在网上搜了一下相关问题,有很多人都是在RowDataBound时Find不到,可以用if (e.Row.RowType == DataControlRowType.DataRow)
    解决,但是在RowCommand中怎么才能Find到呢?

    求高手指点

    2008年7月2日 11:19

答案

  • Code Snippet

    两种方法:

     

    <%@ Page Language="C#" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">
        // 计算数据,完全可以从数据看取得
        ICollection CreateDataSourceByXianhuiMeng()
        {
            System.Data.DataTable dt = new System.Data.DataTable();
            System.Data.DataRow dr;
            dt.Columns.Add(new System.Data.DataColumn("学生班级", typeof(System.String)));
            dt.Columns.Add(new System.Data.DataColumn("学生姓名", typeof(System.String)));
            dt.Columns.Add(new System.Data.DataColumn("语文", typeof(System.Decimal)));
            dt.Columns.Add(new System.Data.DataColumn("数学", typeof(System.Decimal)));
            dt.Columns.Add(new System.Data.DataColumn("英语", typeof(System.Decimal)));
            dt.Columns.Add(new System.Data.DataColumn("计算机", typeof(System.Decimal)));

            for (int i = 0; i < 8; i++)
            {
                System.Random rd = new System.Random(Environment.TickCount * i); ;
                dr = dt.NewRow();
                dr[0] = "班级" + i.ToString();
                dr[1] = "【孟子E章】" + i.ToString();
                dr[2] = System.Math.Round(rd.NextDouble() * 100, 2);
                dr[3] = System.Math.Round(rd.NextDouble() * 100, 2);
                dr[4] = System.Math.Round(rd.NextDouble() * 100, 2);
                dr[5] = System.Math.Round(rd.NextDouble() * 100, 2);
                dt.Rows.Add(dr);
            }
            System.Data.DataView dv = new System.Data.DataView(dt);
            return dv;
        }

        // 设置每页显示的行数 
        int TotalRowCount = 12;
        // 自动填充的行数
        int numCount = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GridView1.DataSource = CreateDataSourceByXianhuiMeng();
                GridView1.DataBind();
            }
        }

     

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("click"))
            {
                int index = Convert.ToInt32(e.CommandArgument);


                GridViewRow row = GridView1.Rows[index];

                Response.Write((row.FindControl("T") as TextBox).Text);
            }
        }
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" Font-Size="12px" CellPadding="3" OnRowCommand="GridView1_RowCommand"
            AutoGenerateColumns="false">
            <HeaderStyle BackColor="#EDEDED" />
            <Columns>
                <asp:TemplateField HeaderText="模版列">
                    <ItemTemplate>
                        <asp:TextBox ID="T" runat="server" Text=' <%#Eval("学生姓名") %>'></asp:TextBox>
                        <asp:Button ID="B" runat="server" CommandArgument="<%# Container.DataItemIndex %>" CommandName="click" Text="方法1" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:ButtonField ButtonType="Button" CommandName="click" Text="方法2" />
            </Columns>
        </asp:GridView>
        </form>
    </body>
    </html>


     

     

     

     

     

     

    2008年7月2日 14:01
    版主

全部回复

  • Code Snippet

    两种方法:

     

    <%@ Page Language="C#" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">
        // 计算数据,完全可以从数据看取得
        ICollection CreateDataSourceByXianhuiMeng()
        {
            System.Data.DataTable dt = new System.Data.DataTable();
            System.Data.DataRow dr;
            dt.Columns.Add(new System.Data.DataColumn("学生班级", typeof(System.String)));
            dt.Columns.Add(new System.Data.DataColumn("学生姓名", typeof(System.String)));
            dt.Columns.Add(new System.Data.DataColumn("语文", typeof(System.Decimal)));
            dt.Columns.Add(new System.Data.DataColumn("数学", typeof(System.Decimal)));
            dt.Columns.Add(new System.Data.DataColumn("英语", typeof(System.Decimal)));
            dt.Columns.Add(new System.Data.DataColumn("计算机", typeof(System.Decimal)));

            for (int i = 0; i < 8; i++)
            {
                System.Random rd = new System.Random(Environment.TickCount * i); ;
                dr = dt.NewRow();
                dr[0] = "班级" + i.ToString();
                dr[1] = "【孟子E章】" + i.ToString();
                dr[2] = System.Math.Round(rd.NextDouble() * 100, 2);
                dr[3] = System.Math.Round(rd.NextDouble() * 100, 2);
                dr[4] = System.Math.Round(rd.NextDouble() * 100, 2);
                dr[5] = System.Math.Round(rd.NextDouble() * 100, 2);
                dt.Rows.Add(dr);
            }
            System.Data.DataView dv = new System.Data.DataView(dt);
            return dv;
        }

        // 设置每页显示的行数 
        int TotalRowCount = 12;
        // 自动填充的行数
        int numCount = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GridView1.DataSource = CreateDataSourceByXianhuiMeng();
                GridView1.DataBind();
            }
        }

     

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("click"))
            {
                int index = Convert.ToInt32(e.CommandArgument);


                GridViewRow row = GridView1.Rows[index];

                Response.Write((row.FindControl("T") as TextBox).Text);
            }
        }
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" Font-Size="12px" CellPadding="3" OnRowCommand="GridView1_RowCommand"
            AutoGenerateColumns="false">
            <HeaderStyle BackColor="#EDEDED" />
            <Columns>
                <asp:TemplateField HeaderText="模版列">
                    <ItemTemplate>
                        <asp:TextBox ID="T" runat="server" Text=' <%#Eval("学生姓名") %>'></asp:TextBox>
                        <asp:Button ID="B" runat="server" CommandArgument="<%# Container.DataItemIndex %>" CommandName="click" Text="方法1" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:ButtonField ButtonType="Button" CommandName="click" Text="方法2" />
            </Columns>
        </asp:GridView>
        </form>
    </body>
    </html>


     

     

     

     

     

     

    2008年7月2日 14:01
    版主
  •  

    用e.item.findcountrol("textbox1")应该能找到
    2008年7月3日 1:36
  • if (e.CommandName == "click")
            {
           

              int index = Convert.ToInt32(e.CommandArgument);
             string a = ((textbox)GridView1.Rows[index].FindControl("textbox1")).Text.Trim();

    2008年8月1日 5:53