zoukankan      html  css  js  c++  java
  • [网络收集]在GridView、Datalist中使用FindControl

    1、在选择(SelectedIndexChanged)事件中使用

      //获得被选择行的TextBox1
      protected void gv1_SelectedIndexChanged(object sender, EventArgs e)
      {
        //Control c = this.gv1.Rows[this.gv1.SelectedIndex].FindControl("TextBox1");
        //TextBox tb = (TextBox)c;
        //tb.Text = "TextBox";

        TextBox tb = (TextBox)this.gv1.Rows[this.gv1.SelectedIndex].FindControl("TextBox1");
        tb.Text = "hello";
      }

    2、在编辑行(RowEditing)事件中使用

      //编辑行时,找到TextBox1
      protected void gv1_RowEditing(object sender, GridViewEditEventArgs e)
      {
        //设置要编辑行的索引
        gv1.EditIndex = e.NewEditIndex;
        GridViewBind();

        TextBox tb = (TextBox)this.gv1.Rows[e.NewEditIndex].FindControl("TextBox1");
        Response.Write(tb.Text);
      }

    3、在取消编辑行(RowCancelingEdit)事件中使用

      //取消编辑时,找到TextBox1
      protected void gv1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
      {
        TextBox tb = (TextBox)this.gv1.Rows[e.RowIndex].FindControl("TextBox1");
        Response.Write(tb.Text);

        gv1.EditIndex = -1;
        GridViewBind();
      }

    4、在行绑定(RowDataBound)事件中使用

      //获得行数据绑定中的TextBox1
      protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
      {
        // 对于在RowDataBound中Find,可以用if (e.Row.RowType == DataControlRowType.DataRow)来限制Find的范围,因为Find默认是在HeaderTemplate中找,如果不限定范围,在HeaderTemplate中找不到,自然就返回null,然后就出错了,DataControlRowType枚举中的DataRow确定是数据行.
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //  TextBox tb = (TextBox)e.Row.FindControl("TextBox1");
        //  tb.Text = "databind";
        //}

        //如果在DataGrid的页眉和页脚:

        //if (e.Row.RowType == DataControlRowType.Header)
        //{
        //  TextBox tbheader = (TextBox)e.Row.FindControl("txtHeader");
        //  tbheader.Text = "Head";
        //}
        ((TextBox)this.gv1.Controls[0].Controls[0].FindControl("txtHeader")).Text = "Head";

        if (e.Row.RowType == DataControlRowType.Footer)
        {
          TextBox tbfooter = (TextBox)e.Row.FindControl("txtFooter");
          tbfooter.Text = "Footer";
        }
      }

    5、在行命令(RowCommand)事件中使用

      //行命令时间中找到TextBox1
      //如果使用GridView默认的模式,e.CommandArgument自动棒定为该行的Index,这时候只要指定gridview1.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("xxx")就可以了,但是如果转化为Template,e.CommandArgument并不会自动绑定任何值,需要手动绑定,可以在<ItemTemplate></ItemTemplate>手动写CommandArgument="<%# ((GridViewRow) Container).RowIndex %>",把这个行的 Index绑定绑定到该e.CommandArgument就可以了.
      protected void gv1_RowCommand(object sender, GridViewCommandEventArgs e)
      {
        if (e.CommandName.ToLower() == "change")
        {
          TextBox tb = (TextBox)this.gv1.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("TextBox1");
          
          Response.Write(tb.Text);
        }
      }

    其他事件中的使用,和上面列举的类似

    附上,前台代码:UseTest3.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="UseTest3.aspx.cs" Inherits="UseTest3" %>

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

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:GridView ID="gv1" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="gv1_SelectedIndexChanged" OnRowEditing="gv1_RowEditing" OnRowDataBound="gv1_RowDataBound" ShowFooter="True" OnRowCommand="gv1_RowCommand" DataKeyNames="employeeid" OnRowCancelingEdit="gv1_RowCancelingEdit">
            <Columns>
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:TextBox ID="txtHeader" runat="Server"></asp:TextBox>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:TextBox ID="TextBox1" Text='<%# Bind("employeeid")%>' runat="server"></asp:TextBox>            
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtFooter" runat="server"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="lastname" HeaderText="LastName" />
            <asp:BoundField DataField="firstname" HeaderText="FirstName" />
            <asp:ButtonField CommandName="select" Text="选择" />    
            <asp:ButtonField CommandName="change" Text="change" />
                <asp:CommandField ShowEditButton="True" />
            </Columns>
            </asp:GridView>
            &nbsp;</div>
        </form>
    </body>
    </html>

    参考资料

    qdzx2008的专栏

    在GridView中使用FindControl
    http://blog.csdn.net/qdzx2008/archive/2009/09/16/4558388.aspx

    应用范例:

    aspx

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
            DataSourceID="SqlDataSource1" OnRowDataBound="GridView_OnRowDataBound">
      <Columns>
        <asp:TemplateField HeaderText="标题">
          <ItemTemplate>
          <asp:HiddenField ID="HiddenField_id" runat="server" Value='<%# Eval("id") %>' />
          <asp:Repeater ID="Repeater2" runat="server" DataSourceID="SqlDataSource2">
            <ItemTemplate>
              <%# Eval("title") %>
            </ItemTemplate>
          </asp:Repeater>
          <!-- 关联对应的一条数据,数据源应放在这里 -->
          <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:database_ConnectionString %>"
                                    SelectCommand="SELECT * FROM [db] WHERE ([id] = @id)">
            <SelectParameters>
              <asp:QueryStringParameter Name="id" QueryStringField="id" Type="Int32" />
            </SelectParameters>
          </asp:SqlDataSource>
        </ItemTemplate>
      </asp:TemplateField>
      </Columns>
    </asp:GridView>

    aspx.cs

    using System;
    using System.Web.UI.WebControls;

    public partial class item : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        protected void GridView_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                SqlDataSource SqlDataSource2= (SqlDataSource)e.Row.FindControl("SqlDataSource2");
                HiddenField HiddenField_id = (HiddenField)e.Row.FindControl("HiddenField_id");
                SqlDataSource2.SelectParameters["id"].DefaultValue = HiddenField_id.Value;
            }
        }

    }

    或者

    protected void DataList_OnItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item)
            {
                SqlDataSource SqlDataSource3 = (SqlDataSource)e.Item.FindControl("SqlDataSource3");
                HiddenField HiddenField3 = (HiddenField)e.Item.FindControl("HiddenField3");
                SqlDataSource3.SelectParameters["id"].DefaultValue = HiddenField3.Value;
            }
        }

  • 相关阅读:
    CF991D Bishwock
    CF1010D Mars rover
    NOIP 2011 计算系数
    SDOI 2008 仪仗队
    浅谈欧拉函数
    CF1249F Maximum Weight Subset
    NOIP 2011 铺地毯
    CF707D Persistent Bookcase
    C++ STL bitset 容器详解
    CF798D Mike and distribution
  • 原文地址:https://www.cnblogs.com/lushuicongsheng/p/1890540.html
Copyright © 2011-2022 走看看