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

datalist取值问题
datalist直接绑定数据库数据,没有控件,请问怎么获取datalist里一个单元格里的数据? 
ps:单元格<td>我都设置了id
如<td id="id2"> 要获取里面的值怎么弄?
请大虾们赐教,还是像网上说的,根本就取不到???

------解决方案--------------------
HTML code
<%@ Page Language="C#" 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">
 
  public System.Data.DataTable GetData()
  {
    System.Data.DataTable dataTable1 = new System.Data.DataTable("BlogUser");
    dataTable1.Columns.Add(new System.Data.DataColumn("UserId", typeof(System.Int32)));
    dataTable1.Columns.Add(new System.Data.DataColumn("UserName", typeof(System.String)));

    dataTable1.Rows.Add(new Object[] { 11, "a" });
    dataTable1.Rows.Add(new Object[] { 111, "aa" });
    dataTable1.Rows.Add(new Object[] { 11111, "aaa" });
    return dataTable1;
  }
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      DataList1.DataSource = GetData();
      DataList1.DataBind();
    }
  }

  protected void Button1_Click(object sender, EventArgs e)
  {
    foreach (DataListItem x in DataList1.Items)
    {
      HtmlTableCell td = x.FindControl("td2") as HtmlTableCell;
      Response.Write("<li>" + td.InnerText);
    }
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <title>无标题页</title>
</head>
<body>
  <form id="form1" runat="server">
  <asp:DataList ID="DataList1" runat="server">
    <ItemTemplate>
      <table border="1">
        <tr>
          <td id="td2" runat="server">
            <%#Eval("UserName") %>
          </td>
        </tr>
      </table>
    </ItemTemplate>
  </asp:DataList>
  <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="得到td2的内容" />
  </form>
</body>
</html>