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

子控件获取父控件ID1
现在我有一个checkbox在datalist里,想再点击checkBox时在后台获得datalist 的ID。该怎么弄?以checkbox chk =(checkbox)sender,为前提找
求大神指导

------解决方案--------------------
CheckBox chk =(CheckBox )sender;
chk.Parent或者chk.Parent.Parent去找,
HTML code
<%@ Page Language="C#" EnableViewState="true" AutoEventWireup="true" %>

<!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();
      dataTable1.Columns.Add(new System.Data.DataColumn("ID", typeof(System.Int32)));
      dataTable1.Columns.Add(new System.Data.DataColumn("Title", typeof(System.String)));

      dataTable1.Rows.Add(new Object[] { 1, 22, });
      dataTable1.Rows.Add(new Object[] { 2, 2222 });
      dataTable1.Rows.Add(new Object[] { 3, 22222 });
      DataList1.DataSource = dataTable1;
      DataList1.DataBind();
    }
  }


  protected void chk_CheckedChanged(object sender, EventArgs e)
  {
    CheckBox chk = sender as CheckBox;
    DataList d = chk.NamingContainer.Parent as DataList;
    Response.Write(d.ID);
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title></title>
</head>
<body>
  <form id="form1" runat="server">
  <asp:DataList ID="DataList1" runat="server">
    <ItemTemplate>
      <asp:CheckBox ID="chk" runat="server" AutoPostBack="true" OnCheckedChanged="chk_CheckedChanged" /><%#Eval("Title")%>
    </ItemTemplate>
  </asp:DataList>
  </form>
</body>
</html>

------解决方案--------------------
或者
C# code
protected void chk_CheckedChanged(object sender, EventArgs e)
{
  CheckBox chk = sender as CheckBox;
  DataList d = chk.Parent.Parent as DataList;
  Response.Write(d.ID);
}

------解决方案--------------------
支持2楼!!!