none
ajax 显示回复问题: RRS feed

  • 问题

  • 就像一个贴子的标题:点一下左边一个按钮的时候下面出现这个标题的所有的回复:问题是我想让 点按钮的时候再加载数据:



    而不是页面初始化时候就加载:怎么实现呢?显示回复信息的是一个DIV 我是你的DIV 里面 放一个 Repeater 控件 :

    怎么让 获取的内容绑定到 Repeater 呢?就像本论坛的 一样:急呀:有这个思想:但不知代码怎么写:


    希望给个例子参考: 谢谢:

    小吴
    2009年6月13日 3:23

答案

  • 完整的例子
    Default10.aspx
    <%@ 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">
      System.Data.DataView CreateDataSourceByXianhuiMeng()
      {
        System.Data.DataTable dt = new System.Data.DataTable();
        System.Data.DataRow dr;
        dt.Columns.Add(new System.Data.DataColumn("id", typeof(System.Int32)));
        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 = 1; i < 8; i++)
        {
          System.Random rd = new System.Random(Environment.TickCount * i); ;
          dr = dt.NewRow();
          dr[0] = i;
          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;
      }
    
      protected void Page_Load(object sender, EventArgs e)
      {   
          r.DataSource = CreateDataSourceByXianhuiMeng();
          r.DataBind();
          StringBuilder SB = new StringBuilder();
          System.IO.StringWriter SW = new System.IO.StringWriter(SB);
          HtmlTextWriter htmlTW = new HtmlTextWriter(SW);
    
          r.RenderControl(htmlTW);
    
          string dataGridHTML = SB.ToString();
          Response.ClearContent();
          Response.Write(dataGridHTML);
          Response.End();
      }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
      <title></title>
    </head>
    <body>
      <form id="form1" runat="server">
      <div>
        <asp:Repeater ID="r" runat="server">
          <ItemTemplate>
            <div style='color: red'>
              语文:<%#Eval("语文")%> 数学:<%#Eval("数学")%></div>
          </ItemTemplate>
        </asp:Repeater>
      </div>
      </form>
    </body>
    </html>
    
    testPage.aspx
    =====
    <%@ Page Language="C#" Debug="true" EnableViewState="true" %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
    
    <script runat="server">
    
     
      System.Data.DataView CreateDataSourceByXianhuiMeng()
      {
        System.Data.DataTable dt = new System.Data.DataTable();
        System.Data.DataRow dr;
        dt.Columns.Add(new System.Data.DataColumn("id", typeof(System.Int32)));
        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;
          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;
      }
    
    
      protected void Page_Load(object sender, EventArgs e)
      {
        if (!Page.IsPostBack)
        { 
         
            GridView1.DataSource = CreateDataSourceByXianhuiMeng();
            GridView1.DataBind();
    
        }    
      }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
      <title></title>
      <script type="text/javascript">
        var req;
        function getReply(rid) {
          req = new XMLHttpRequest();
          req.open("GET", "Default10.aspx?rid=" + rid + "&tmp=" + Date.parse(new Date()), true)
          req.onreadystatechange = function() {
          showData(rid)
          }
          req.send(null)
        }
    
        function showData(rid) {
          if (req.readyState == 4) {
            if (req.status == 200) {
              document.getElementById("reply"+rid).innerHTML = (req.responseText);
            }
          }
        }    
      </script>
    </head>
    <body>
      <form id="form1" runat="server">
      <asp:ScriptManager runat="server" id="ScriptManager1" />
      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>
          <asp:TemplateField>
            <ItemTemplate>
              <div>
                <div onclick='getReply(<%#Eval("id") %>)'>
                  <%#Eval("学生姓名") %></div>
                <div id='reply<%#Eval("id") %>' style="padding-left:20px;">
                </div>
              </div>
            </ItemTemplate>
          </asp:TemplateField>
        </Columns>
      </asp:GridView>
      </form>
    </body>
    </html>
    



    【孟子E章】
    2009年6月14日 7:04
    版主

全部回复

  • 可以使用 iframe ,在 iframe 的页面中显示你的回复
    知识改变命运,奋斗成就人生!
    2009年6月13日 3:50
    版主
  • 可以使用 iframe ,在 iframe 的页面中显示你的回复
    知识改变命运,奋斗成就人生!

    点按钮    怎么动态加载控制   iframe 的显示:
    小吴
    2009年6月13日 4:46
  • 你好!通过 javascript 改变 iframe 的 src 来达到你的目的,网上例子很多

    知识改变命运,奋斗成就人生!
    2009年6月13日 14:20
    版主
  • 完整的例子
    Default10.aspx
    <%@ 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">
      System.Data.DataView CreateDataSourceByXianhuiMeng()
      {
        System.Data.DataTable dt = new System.Data.DataTable();
        System.Data.DataRow dr;
        dt.Columns.Add(new System.Data.DataColumn("id", typeof(System.Int32)));
        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 = 1; i < 8; i++)
        {
          System.Random rd = new System.Random(Environment.TickCount * i); ;
          dr = dt.NewRow();
          dr[0] = i;
          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;
      }
    
      protected void Page_Load(object sender, EventArgs e)
      {   
          r.DataSource = CreateDataSourceByXianhuiMeng();
          r.DataBind();
          StringBuilder SB = new StringBuilder();
          System.IO.StringWriter SW = new System.IO.StringWriter(SB);
          HtmlTextWriter htmlTW = new HtmlTextWriter(SW);
    
          r.RenderControl(htmlTW);
    
          string dataGridHTML = SB.ToString();
          Response.ClearContent();
          Response.Write(dataGridHTML);
          Response.End();
      }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
      <title></title>
    </head>
    <body>
      <form id="form1" runat="server">
      <div>
        <asp:Repeater ID="r" runat="server">
          <ItemTemplate>
            <div style='color: red'>
              语文:<%#Eval("语文")%> 数学:<%#Eval("数学")%></div>
          </ItemTemplate>
        </asp:Repeater>
      </div>
      </form>
    </body>
    </html>
    
    testPage.aspx
    =====
    <%@ Page Language="C#" Debug="true" EnableViewState="true" %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
    
    <script runat="server">
    
     
      System.Data.DataView CreateDataSourceByXianhuiMeng()
      {
        System.Data.DataTable dt = new System.Data.DataTable();
        System.Data.DataRow dr;
        dt.Columns.Add(new System.Data.DataColumn("id", typeof(System.Int32)));
        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;
          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;
      }
    
    
      protected void Page_Load(object sender, EventArgs e)
      {
        if (!Page.IsPostBack)
        { 
         
            GridView1.DataSource = CreateDataSourceByXianhuiMeng();
            GridView1.DataBind();
    
        }    
      }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
      <title></title>
      <script type="text/javascript">
        var req;
        function getReply(rid) {
          req = new XMLHttpRequest();
          req.open("GET", "Default10.aspx?rid=" + rid + "&tmp=" + Date.parse(new Date()), true)
          req.onreadystatechange = function() {
          showData(rid)
          }
          req.send(null)
        }
    
        function showData(rid) {
          if (req.readyState == 4) {
            if (req.status == 200) {
              document.getElementById("reply"+rid).innerHTML = (req.responseText);
            }
          }
        }    
      </script>
    </head>
    <body>
      <form id="form1" runat="server">
      <asp:ScriptManager runat="server" id="ScriptManager1" />
      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>
          <asp:TemplateField>
            <ItemTemplate>
              <div>
                <div onclick='getReply(<%#Eval("id") %>)'>
                  <%#Eval("学生姓名") %></div>
                <div id='reply<%#Eval("id") %>' style="padding-left:20px;">
                </div>
              </div>
            </ItemTemplate>
          </asp:TemplateField>
        </Columns>
      </asp:GridView>
      </form>
    </body>
    </html>
    



    【孟子E章】
    2009年6月14日 7:04
    版主