查了好几天的资料,我现在可以用
Repeater1.ItemTemplate = Page.LoadTemplate("list.ascx");
这种方法把用户控里的东西加到Repeater的模版里面,但是我想把
LinkButton btn = new LinkButton 这里面的 btn加到Repeater的模版里面,应该怎么写呢?Repeater1.ItemTemplate =btn;
Repeater1.ItemTemplate =(System.Web.UI.ITemplate)btn;这两种写法都是不对的。
应该怎么写呢?大家帮帮忙了!!我想在服务器控件里面用Repeater,服务器控件里面没有前台代码,只能在后台里面加。我想实现的功能是一个翻页控件,其他的功能都实现了,我现在想用Repeater来实现页号的效果
... 10 11 12 13 14 15 16 ...
我感觉用Repeater是最好的方法。直接用LinkButton的话,数量上不太灵活。另外我要在点击页号的时候能触发事件,而不是 aa.aspx?pageindex=10 这样的效果,这样做的话,无法保持页面的状态,限制了翻页控件的适用范围。另附赠我的翻页算法:ALTER PROCEDURE Test --测试用  简单的不带优化的
(
@pageSize int , --一页的记录数
@runKind int=null, --1:第一页;2:最后一页;           @CurPage int --新页号(要显示第几页的数据)。
)AS
declare @id int
if @runKind = 2
begin
--最后一页
set rowcount @pageSize
select @id=newsID from newsTemp   order by newsID
goto start
end--获取 @CurPage  页的第一条记录的ID
set @TopCount = @pageSize * (@CurPage -1) + 1
set rowcount @TopCount
select @id=ID from Temp  order by ID descstart:
--返回记录
set rowcount @pageSize
select * from Temp where ID <=@id order by ID descset rowcount 0
优点:就一个字——简单
缺点:数据量大的时候(二十万以上吧),会有点慢。我用十五万条记录做过测试,很快的。SQL占用内存在65M左右。
还有就是不太通用,不过思路都有了,你可以随意修改呀!另外说明一点,需要给ID字段设置为主键,或者是建立索引。否者小数据量的时候没有问题,但是数据量一大速度就会明显变慢,而且内存占用量也会增加很多。

解决方案 »

  1.   

    贴点代码/// <summary>
    /// 清空GRID的COLUMN
    /// </summary>
    /// <param name="vardg"></param>
    private static void RefreshGrid(DataGrid vardg)
    {
    vardg.Columns.Clear();

    BoundColumn dc = new BoundColumn();
    dc.HeaderText = "ID";
    dc.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
    dc.DataField = "ID";
    dc.Visible = false;
    dc.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
    vardg.Columns.AddAt(0,dc); dc = new BoundColumn();
    dc.HeaderText = "序号";
    dc.HeaderStyle.Wrap = false;
    dc.HeaderStyle.Width = 30;
    dc.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
    dc.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
    vardg.Columns.AddAt(1,dc);
    } /// <summary>
    /// 创建按专家打印的GRID
    /// </summary>
    /// <param name="vardg"></param>
    private static void CreateExpertDataGrid(DataGrid vardg)
    {
    RefreshGrid(vardg); BoundColumn dc = new BoundColumn();
    dc.HeaderText = "专家姓名";
    dc.HeaderStyle.Wrap = false;
    dc.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
    dc.DataField = "EXPERT_NAME";
    dc.SortExpression = "EXPERT_NAME";
    dc.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
    vardg.Columns.AddAt(2,dc);

    dc = new BoundColumn();
    dc.HeaderText = "所属医院";
    dc.HeaderStyle.Wrap = false;
    dc.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
    dc.DataField = "HOSPITAL";
    dc.SortExpression = "HOSPITAL";
    vardg.Columns.AddAt(3,dc);

    TemplateColumn  tc = new TemplateColumn();
    tc.ItemTemplate = new ColumnTemplate();
    tc.HeaderText = "打印";
    dc.HeaderStyle.Wrap = false;
    tc.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
    tc.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
    tc.HeaderStyle.Width = 30;
    vardg.Columns.AddAt(4,tc);

    tc = new TemplateColumn();
    tc.ItemTemplate = new ColumnTemplateExport();
    tc.HeaderText = "导出";
    dc.HeaderStyle.Wrap = false;
    tc.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
    tc.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
    tc.HeaderStyle.Width = 30;
    vardg.Columns.AddAt(5,tc);
    }
      

  2.   


    /// <summary>
    /// 实现ITemplate接口,用来打印
    /// </summary>
    public class ColumnTemplate : ITemplate
    {
    public void InstantiateIn(Control container)
    {
    LinkButton lb = new LinkButton();
    lb.Text = "打印";
    lb.CommandName = "Print";
    container.Controls.Add(lb);
    }
    } /// <summary>
    /// 实现ITemplate接口,用来导出
    /// </summary>
    public class ColumnTemplateExport : ITemplate
    {
    public void InstantiateIn(Control container)
    {
    LinkButton lb = new LinkButton();
    lb.Text = "导出";
    lb.CommandName = "Export";
    container.Controls.Add(lb);
    }
    }
      

  3.   

    http://dev.csdn.net/develop/article/22/22942.shtm
      

  4.   

    好像是给DataGrid用的,正在换成Repeater,看能不能行。谢谢。
      

  5.   

    前些天做的,可以参考一下!注意用到了 ITemplateusing System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    namespace TASKMNG.SystemAdmin
    {
    /// <summary>
    /// UserRight 的摘要说明。
    /// </summary> public class UserRight : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DropDownList ddl_department;
    protected System.Web.UI.WebControls.DataGrid dg;
    protected System.Web.UI.WebControls.Label l_error;
    protected System.Web.UI.WebControls.LinkButton lb_save;
    protected SqlConnection myConnection = new SqlConnection();
    public int DepartmentCount;
    public string dpname;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    if ( Session["User_ID"] == null )
    {
    Response.Redirect( "../Login/Login2.aspx");
    } myConnection.ConnectionString=Application["App_ConnectString"].ToString();
    l_error.Visible=false;

    myConnection.ConnectionString=Application["App_ConnectString"].ToString();
    if(!IsPostBack) {  
    BindDepartment(); }
    BindGrid(ddl_department.SelectedItem.Value);
    } //绑定部门
    public void BindDepartment()
    {
    myConnection.Open();
    SqlCommand myCommand = new SqlCommand("select Department_ID='0',Department_Name='所有员工' union select Department_ID,Department_Name from Dim_Department  ",myConnection);
    SqlDataReader myReader = myCommand.ExecuteReader();
    ddl_department.DataSource = myReader;
    ddl_department.DataTextField="Department_Name";
    ddl_department.DataValueField="Department_ID";
    ddl_department.DataBind();
    myReader.Close();
    myConnection.Close();
              
    } // 动态横向部门标题显示
    public string DepartName() {
    SqlConnection myConnection = new SqlConnection();
    myConnection.ConnectionString=Page.Application["App_ConnectString"].ToString();
    myConnection.Open();
    SqlCommand cmd = new SqlCommand("select Department_Name from Dim_Department",myConnection);
    SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
    while (myReader.Read())
    {
    dpname += myReader.GetString(0).ToString()+"-";
    }

    dpname = dpname.Substring(0,dpname.Length-1);
    dpname ="新建任务-"+dpname;
    myReader.Close(); return dpname;

    }
    //统计部门个数已显示动态的checkbox模板列
    public int DepartCount()
    {
    myConnection.Open();
    SqlCommand cmd = new SqlCommand("select Count(*) from Dim_Department ",myConnection);
    DepartmentCount = (int)cmd.ExecuteScalar();
    return DepartmentCount;



    } //绑定datagrid
    public void BindGrid(string depart)
    {   
    DepartCount();
    DepartName();
    string strComm="";
    if (depart=="0")
    {
    strComm="select * from V_UserRight";
    }
    else 
    {
    strComm="select * from V_UserRight where Department_ID='"+depart+"'";
    }
    SqlDataAdapter da = new SqlDataAdapter(strComm,myConnection);
    DataSet ds = new DataSet();

    da.Fill(ds,"UserRight");
    DataView dv = new DataView();
    dv = ds.Tables[0].DefaultView;
    //Response.Write(DepartmentCount.ToString());
    for (int i=0;i<=DepartmentCount;i++)
      {
    TemplateColumn tc1 = new TemplateColumn();
    tc1.HeaderTemplate = new 
    DataGrid_Template(ListItemType.Header,dpname,i);
    tc1.ItemTemplate = new 
    DataGrid_Template(ListItemType.Item, "Function_Right",i); dg.Columns.Add(tc1); }

    dg.DataKeyField="User_ID";
    dg.DataSource=dv;
    dg.DataBind();
    myConnection.Close();

    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.ddl_department.SelectedIndexChanged += new System.EventHandler(this.ddl_department_SelectedIndexChanged);
    this.lb_save.Click += new System.EventHandler(this.lb_save_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion //部门选择绑定
    private void ddl_department_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    //BindGrid(ddl_department.SelectedItem.Value);
    }
             //保存
    private void lb_save_Click(object sender, System.EventArgs e)
    {     //BindGrid(ddl_department.SelectedItem.Value);
           for (int i=0;i<=dg.Items.Count-1;i++)
    {
    string strRight=""; 
    for (int j=2;j<dg.Items[0].Cells.Count;j++) //从checkbox列开始检查
    {
    CheckBox chkTemp = (CheckBox)dg.Items[i].Cells[j].Controls[0];
    //CheckBox chkTemp = (CheckBox)dg.Items[i].Cells[j].FindControl(cbx);
    if (chkTemp.Checked == false)
    { strRight += "0";
    } else
    {   
    strRight += "1";
    }
    }
    //Response.Write(dg.DataKeys[i].ToString()+":"+strRight);
    //Response.Write("<br>");
    //更新权限
    myConnection.Open();
    string strCmd = "update Rel_User_Function set Function_Right ='"+strRight+"' where User_ID='"+dg.DataKeys[i].ToString()+"'";
    SqlCommand cmd = new SqlCommand(strCmd,myConnection);
    cmd.ExecuteNonQuery();
    myConnection.Close(); }
               l_error.Visible = true;
           l_error.Text = "保存成功!"; }
    } // 动态模板类
    public class DataGrid_Template : ITemplate
    {
    ListItemType templateType;
    string columnName;
    int i;

       
    public DataGrid_Template(ListItemType type, string colname,int j)
    {
    templateType = type;
    columnName = colname;
    i = j;
    } public void InstantiateIn(System.Web.UI.Control container)
    {

    switch(templateType)
    {
    case ListItemType.Header:
    Label lb = new Label();
    lb.Text=GetHeadText(columnName,i);
    lb.ForeColor = Color.White;
    container.Controls.Add(lb);
    break;
    case ListItemType.Item:
    System.Web.UI.WebControls.CheckBox cb = new System.Web.UI.WebControls.CheckBox();
    //cb.ID = "cb"+i.ToString();
    cb.DataBinding += new EventHandler(cb_DataBinding);
    container.Controls.Add(cb);
    break; }
    }
    //得到head标题
    public string GetHeadText(string partname ,int i)
    {
    string[] part = partname.Split('-');
    return part[i].ToString();




    } //得到checkbox的值        
    public void cb_DataBinding(object sender,System.EventArgs e)
    {
    System.Web.UI.WebControls.CheckBox cb;
    cb =(CheckBox) sender;
    DataGridItem  container = (DataGridItem)cb.NamingContainer ;
    cb.Checked = GetCheck(((DataRowView)container.DataItem)[columnName].ToString().Substring(i,1)); } //把1转化为true,0转化为false
    public bool GetCheck(string r)
    {
    if(r=="0")
    {
    return false;
    }
    else
    {
    return true;
    }
    }
    }

    } // public void lb_DataBinding(object sender,System.EventArgs e)
    // {
    // LinkButton lb;
    // lb =(LinkButton) sender;
    // DataGridItem  container = (DataGridItem)lb.NamingContainer ;
    // DepartmentName dp = new DepartmentName();
    // string[] dpName = dp.DepartName().Split('-');
    // lb.Text = dpName[i] ;
    //
    //
    //
    // }
      

  6.   

    感谢大家。
    其实不用那么麻烦,只是怎么给 ITemplate 负值(或者叫对象)的问题。
    代码好长呀,我得慢慢看了。
      

  7.   

    终于实现了。
    原来是要这么用呀。
    public class addTemplate : ITemplate
    {
    public void InstantiateIn(Control container)
    {
    LinkButton lb = new LinkButton();
    lb.ID="pageIndex";
    container.Controls.Add(lb);
    }

    } Repeater1.ItemTemplate =new addTemplate();在前台很容易的事情怎么到了后台就这么麻烦呢?而且MSDN里面也没有实例,看得我直晕。还是有事例的好。