日期:2014-05-17  浏览次数:20559 次

gridview中a控件控制b控件隐藏或者显示!
一个gridview控件中有控件a(button)和b(textbox)俩控件。当点击a控件时,就把该行的b控件显示出来!

------解决方案--------------------
第一种方法可以在后台动态的把b中的ClientID传人到a的onclick事件调用的函数参数里面
第二种方法可以在a点击的时候把this传过去,然后获取当前a的parentNode下面的子节点下面的b
------解决方案--------------------
给你个完整的 例子
HTML code
<%@ 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">

  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      System.Data.DataTable dataTable1 = new System.Data.DataTable("BlogUser");
      System.Data.DataRow dr;
      dataTable1.Columns.Add(new System.Data.DataColumn("UserId", typeof(System.Int32)));
      dataTable1.Columns.Add(new System.Data.DataColumn("UserName", typeof(System.String)));

      for (int i = 0; i < 8; i++)
      {
        dr = dataTable1.NewRow();
        dr[0] = i;
        dr[1] = "【孟子E章】" + i.ToString();
        dataTable1.Rows.Add(dr);
      }

      GridView1.DataSource = dataTable1;
      GridView1.DataBind();
    }
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <title></title>
  <script type="text/javascript">
    function xx(btn) {
      b = btn.parentNode;
      while (b.tagName != "TR") b = b.parentNode;
      b.cells[0].getElementsByTagName("div")[0].style.display = b.cells[0].getElementsByTagName("div")[0].style.display == "none" ? "" : "none";
    }
  </script>
</head>
<body>
  <form id="form1" runat="server">
  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="UserId">
    <Columns>
      <asp:TemplateField>
        <ItemTemplate>
          <asp:Panel ID="x" runat="server">
            <asp:TextBox ID="replay" runat="server"></asp:TextBox>
          </asp:Panel>
        </ItemTemplate>
      </asp:TemplateField>
      <asp:TemplateField>
        <ItemTemplate>
          <asp:Button ID="y" runat="server" OnClientClick="xx(this);return false;" Text="显示隐藏测试"></asp:Button>
        </ItemTemplate>
      </asp:TemplateField>
    </Columns>
  </asp:GridView>
  </form>
</body>
</html>