zoukankan      html  css  js  c++  java
  • repeater 总结

     
    公司某项目中用到了repeater嵌套,当初用SqlDataSource绑定repeater数据源总是绑定不对
    使用当r1绑定一列后,触发了r1的ItemDataBound事件,r2绑定数据,但是最后的结果全是依据r1最后一列所对应的数据,原因是因为使用了共同的数据源
    所以在这个地方建议不要用SqlDataSource,改用dataset或datatable等来绑定数据

    如下总结,留以后用
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="NoteRepeater.aspx.cs" Inherits="NoteRepeater"%>

    <!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:Repeater ID="Repeater1" runat="server"
    onitemcommand
    ="Repeater1_ItemCommand"
    onitemdatabound
    ="Repeater1_ItemDataBound"
    onitemcreated
    ="Repeater1_ItemCreated">
    <ItemTemplate>

    <asp:LinkButton ID="LinkButton1" CommandName="r1_linkbtn" runat="server"><%#(Container.DataItem asstring).ToString() %></asp:LinkButton>
    <asp:HiddenField ID="HiddenField1" runat="server"/>

    <table>
    <tr>
    <td>
    <asp:Repeater ID="Repeater2" runat="server" onitemcommand="rep_ItemCommand">
    <ItemTemplate>
    <asp:LinkButton ID="LinkButton2" CommandName="r2_linkbtn" runat="server" Text='<%#(Container.DataItem as string).ToString() %>'></asp:LinkButton>
    </ItemTemplate>
    </asp:Repeater>
    </td>
    </tr>
    </table>
    </ItemTemplate>
    </asp:Repeater>
    </div>
    </form>
    </body>
    </html>
     后台代码:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
    using System.Collections;

    publicpartialclass NoteRepeater : System.Web.UI.Page
    {
    protectedvoid Page_Load(object sender, EventArgs e)
    {
    List
    <string> list =new List<string>();
    list.Add(
    "北京");
    list.Add(
    "上海");
    list.Add(
    "山东");

    this.Repeater1.DataSource = list;
    Repeater1.DataBind();
    //不要忘记绑定
    }

    //这个事件是当repeater绑定一列后执行一次,绑定一列执行一次
    protectedvoid Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
    List
    <string> list2 =new List<string>();
    list2.Add(
    "C#");
    list2.Add(
    "ASP.NET");
    list2.Add(
    "AJAX");
    //获得repeater2
    Repeater rep = e.Item.FindControl("Repeater2") as Repeater;
    rep.DataSource
    = list2;
    rep.DataBind();
    }
    protectedvoid Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
    if (e.CommandName=="r1_linkbtn")
    {
    string linkbtn = ((LinkButton)(e.Item.FindControl("LinkButton1") as LinkButton)).Text;
    ClientScript.RegisterStartupScript(
    this.GetType(), "", "<script>alert('您触发了Repeater1并选择了:"+ linkbtn +"(因为linkbutton中没有写text属性,所以没有值,但是已经触发了r1事件)')</script>");
    }
    }
    protectedvoid rep_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
    //CommandName为linkbutton的一个属性,前台页面不要忘记添加
    if (e.CommandName=="r2_linkbtn")
    {
    //获取linkbutton的text值,注意:要在linkbutton里写text属性否则获取不到
    string linkbtn = ((LinkButton)(e.Item.FindControl("LinkButton2") as LinkButton)).Text;
    ClientScript.RegisterStartupScript(
    this.GetType(), "", "<script>alert('您出发了Repeater2并选择了:"+ linkbtn +"')</script>");
    }
    }
    protectedvoid Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
    {
    Repeater rep
    = e.Item.FindControl("Repeater2") as Repeater; //在r1的ItemCreated事件中注册一下r2
    }

    }
     
     
     (本人愚见,望指教!)
     
  • 相关阅读:
    实时获取管道信息的一个小框架
    multiprocessing还是threading?
    QThread的一些使用心得
    super超类继承特点小结
    打靶总结
    简析Colorspace
    第一个Unity3D脚本
    一个新的计划,写在年末
    lambda函数的特性
    Nuke Python module的使用
  • 原文地址:https://www.cnblogs.com/itstone/p/2046194.html
Copyright © 2011-2022 走看看