Repeater控件自定义数据源代码绑定实例

前台repeater定义:
         
    
    
     
      
       选择
       标题       
       频道
       栏目
       二级栏目
       创建时间
       省份
       城市
       编辑
       点击数
       权重
       审核
       操作
      
     

     
      
               
                 
        
                
       
       "  >
       
         <%#useually.getleft(DataBinder.Eval(Container.DataItem, "News_Title").ToString(),20)%>          
        
        

                
       
       <%#DataBinder.Eval(Container.DataItem, "Channel_Name")%>
       <%#DataBinder.Eval(Container.DataItem, "Column_Name")%>
       <%#DataBinder.Eval(Container.DataItem, "Class1_Name")%>
           <%#DataBinder.Eval(Container.DataItem, "News_Date")%>
       <%#DataBinder.Eval(Container.DataItem,"Province")%>
       <%#DataBinder.Eval(Container.DataItem,"City")%>
       <%#DataBinder.Eval(Container.DataItem, "News_Edit")%>
       <%#DataBinder.Eval(Container.DataItem, "Click_Number")%>
       <%#DataBinder.Eval(Container.DataItem, "News_Weight")%>
       <%#DataBinder.Eval(Container.DataItem, "News_Verify").ToString() != "" ? "已审核" : "未审核"%>
       转移频道
      
     
  
     
     
          
     
                        
       
       
        
       
     "  >
       
         <%#useually.getleft(DataBinder.Eval(Container.DataItem, "News_Title").ToString(),20)%>          
       
       

       
       <%#DataBinder.Eval(Container.DataItem, "Channel_Name")%>
       <%#DataBinder.Eval(Container.DataItem, "Column_Name")%>
       <%#DataBinder.Eval(Container.DataItem, "Class1_Name")%>
           <%#DataBinder.Eval(Container.DataItem, "News_Date")%>
       <%#DataBinder.Eval(Container.DataItem,"Province")%>
       <%#DataBinder.Eval(Container.DataItem,"City")%>
       <%#DataBinder.Eval(Container.DataItem, "News_Edit")%>
       <%#DataBinder.Eval(Container.DataItem, "Click_Number")%>
       <%#DataBinder.Eval(Container.DataItem, "News_Weight")%>
       <%#DataBinder.Eval(Container.DataItem, "News_Verify").ToString() != "" ? "已审核" : "未审核"%>
       转移频道
       
     
     

    
    
   
    
   
 
后台cs代码:
 
     public string channel;
     public int is_yc;
     public int fenye;
    #region 分页属性
    private int pageSize = 16;
    private int recordCount;
    public int PageCount
    {
        get
        {
            return Convert.ToInt32(ViewState["PageCount"]);
        }
    }
    public int PageSize
    {
        get
        {
            return pageSize;
        }
    }
    public int PageIndex
    {
        get
        {
            return Convert.ToInt32(ViewState["CurrentPage"].ToString());
        }
        set
        {
            ViewState["CurrentPage"] = value;
        }
    }
    public int RecordCount
    {
        get
        {
            return recordCount;
        }
    }
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["hg_loginname"] == null)
        {
            Response.Redirect("../login.aspx");
        }
        else
        {
             channel=Request.QueryString["channel"];
             lb_message.Text = "->" + channel;
        }
        //判断是否具有管理信息的功能
        if (!HasPermission("26"))
        {
            Response.Write("");
            Response.Write("");
         
        }
        if (!IsPostBack)
        {
           //传递存储过程参数的初始化
            ViewState["editor"] = "";
            ViewState["LookOwn"] = "";        //查看登陆用户发布的信息!
            ViewState["LookUnPassed"] = "";   //查看为审核的信息!
            is_yc=0; //查看自主原创的信息!
            ViewState["CurrentPage"] = 0;
            ViewState["PageCount"] = 0;
            ViewState["Keyword"] = "";
            ViewState["editor"] = Session["hg_loginname"].ToString();
            RptDataBind();    
        }
        //为删除按钮添加删除前提示确认脚本
        btnDelete.Attributes.Add(" "return confirm('您真的要删除选定的记录吗?');");
        // 在此处放置用户代码以初始化页面
    }
    #region 数据绑定
    private void RptDataBind()
    {
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dfhgConnString"].ConnectionString);
        SqlCommand cmd = new SqlCommand("InforManage_HuaGong123", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@PageIndex", SqlDbType.Int));
        cmd.Parameters["@PageIndex"].Value = PageIndex;  //页索引
        cmd.Parameters.Add(new SqlParameter("@PageSize", SqlDbType.Int));
        cmd.Parameters["@PageSize"].Value = PageSize;  //每页记录大小
        cmd.Parameters.Add(new SqlParameter("@RecordCount", SqlDbType.Int));
        cmd.Parameters["@RecordCount"].Direction = ParameterDirection.Output;   //返回--记录总数
        cmd.Parameters.Add(new SqlParameter("@PageCount", SqlDbType.Int));
        cmd.Parameters["@PageCount"].Direction = ParameterDirection.Output;  //返回--总页数
        cmd.Parameters.Add(new SqlParameter("@channel", SqlDbType.NVarChar, 10));
        cmd.Parameters["@channel"].Value = channel; //频道
        cmd.Parameters.Add(new SqlParameter("@editor", SqlDbType.VarChar, 10));
        cmd.Parameters["@editor"].Value = ViewState["editor"];   //添加作者
        cmd.Parameters.Add(new SqlParameter("@lookUnpassed", SqlDbType.Char, 1));
        cmd.Parameters["@lookUnpassed"].Value = ViewState["LookUnPassed"];   //未审核的
        cmd.Parameters.Add(new SqlParameter("@lookOwn", SqlDbType.Char, 1));
        cmd.Parameters["@lookOwn"].Value = ViewState["LookOwn"];     //自己发布的信息
        cmd.Parameters.Add(new SqlParameter("@is_yc", SqlDbType.Bit));
        cmd.Parameters["@is_yc"].Value =is_yc;     //原创的信息
        cmd.Parameters.Add(new SqlParameter("@keyword", SqlDbType.NVarChar, 100));
        cmd.Parameters["@keyword"].Value = ViewState["Keyword"].ToString();   //查询关键字
        try
        {
            cmd.Connection.Open();
            SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            rptArticleManage.DataSource = reader;
            rptArticleManage.DataBind();
            reader.Close();
            recordCount = (int)cmd.Parameters["@RecordCount"].Value;  //记录返回的记录数
            ViewState["PageCount"] = (int)cmd.Parameters["@PageCount"].Value;  //记录返回的页数
            fenye = (int)ViewState["PageCount"];
            cmd.Dispose();
        }
        catch (SqlException ex)
        {
            Response.Write(ex.ToString());
            Response.End();
        }
        SetPagingState();   
    }
    #endregion 
  
    #region 分页导航按钮点击监听事件
    private void LBtnNavigation_Click(object sender, System.EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        switch (btn.CommandName)
        {
            case "First":
                PageIndex = 0;
                break;
            case "Prev":
                PageIndex = PageIndex - 1;
                break;
            case "Next":
                PageIndex = PageIndex + 1;
                break;
            case "Last":
                PageIndex = PageCount - 1;
                break;
            case "Go":
                try
                {
                    Convert.ToInt32(txtGo.Text);
                }
                catch
                {
                    return;
                }
                if (Convert.ToInt32(txtGo.Text) < 1)
                {
                    PageIndex = 0;
                }
                else if (Convert.ToInt32(txtGo.Text) > PageCount - 1)
                {
                    PageIndex = PageCount - 1;
                }
                else
                {
                    PageIndex = Convert.ToInt32(txtGo.Text) - 1;
                }
                break;
        }
        RptDataBind();
    }
    #endregion
  
    #region 删除所选信息
    protected void btnDelete_Click1(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dfhgConnString"].ConnectionString);
        SqlCommand cmd = new SqlCommand("InForDelete_HuaGong", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@ID", SqlDbType.Int, 4);   //参数绑定    
        foreach (RepeaterItem item in this.rptArticleManage.Items)
        {
            HtmlInputCheckBox cb = (HtmlInputCheckBox)item.FindControl("chkb");
            HtmlInputHidden hCpID = (HtmlInputHidden)item.FindControl("hidCpID");
            HtmlInputHidden hCpUrl = (HtmlInputHidden)item.FindControl("hidUrl");
            if (cb.Checked == true)
            {
                cmd.Parameters["@ID"].Value = Convert.ToInt32(hCpID.Value);
                string dir = Server.MapPath(("News_hmtl/" + hCpUrl.Value));//用来生成文件夹
                if (File.Exists(dir))
                {
                    File.Delete(dir);
                }
                try
                {   cmd.Connection.Open();
                    cmd.ExecuteNonQuery();
              
                }
                catch (SqlException ex)
                {
                    Response.Write("删除失败,错误信息如下:
");
                    Response.Write(ex.ToString());
                    Response.End();
                }
                finally
                {
                    cmd.Connection.Close();
                    cmd.Dispose();
                }
            }
        }
        Response.Write("");       
        RptDataBind();
    }
    #endregion
    #region 搜索
    private void btnSearch_Click(object sender, System.EventArgs e)
    {
        if (txtKeyword.Text.Trim().Length == 0)
        {
            Response.Write("");
            RptDataBind();
            return;
        }
        ViewState["Keyword"] = "%" + txtKeyword.Text.Trim() + "%";   //用"存储容器 "--ViewState--存储查询数据
        RptDataBind();
    }
    #endregion 
    #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
        //
        // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
        //
        InitializeComponent();
        base.OnInit(e);
    }
    ///
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    ///

    private void InitializeComponent()
    {
        //this.lb_yc.Click += new System.EventHandler(this.lb_yc);
        this.btnLookUnPassed.Click += new System.EventHandler(this.btnLookUnPassed_Click);
        this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
        this.btnLookOwn.Click += new System.EventHandler(this.btnLookOwn_Click);
        this.LBtnFirst.Click += new System.EventHandler(this.LBtnNavigation_Click);
        this.LBtnPrev.Click += new System.EventHandler(this.LBtnNavigation_Click);
        this.LBtnNext.Click += new System.EventHandler(this.LBtnNavigation_Click);
        this.LBtnLast.Click += new System.EventHandler(this.LBtnNavigation_Click);
        this.LBtnGo.Click += new System.EventHandler(this.LBtnNavigation_Click);
        this.Load += new System.EventHandler(this.Page_Load);
    }
    #endregion  
    #region 控制导航按钮或数字的状态
    ///
    /// 控制导航按钮或数字的状态
    ///

    public void SetPagingState()
    {
        if (PageCount <= 1)//( RecordCount <= PageSize )//小于等于一页
        {
            this.LBtnFirst.Enabled = false;
            this.LBtnPrev.Enabled = false;
            this.LBtnNext.Enabled = false;
            this.LBtnLast.Enabled = false;
        }
        else //有多页
        {
            if (PageIndex == 0)//当前为第一页
            {
                this.LBtnFirst.Enabled = false;
                this.LBtnPrev.Enabled = false;
                this.LBtnNext.Enabled = true;
                this.LBtnLast.Enabled = true;
            }
            else if (PageIndex == PageCount - 1)//当前为最后页
            {
                this.LBtnFirst.Enabled = true;
                this.LBtnPrev.Enabled = true;
                this.LBtnNext.Enabled = false;
                this.LBtnLast.Enabled = false;
            }
            else //中间页
            {
                this.LBtnFirst.Enabled = true;
                this.LBtnPrev.Enabled = true;
                this.LBtnNext.Enabled = true;
                this.LBtnLast.Enabled = true;
            }
        }
        this.LtlPageSize.Text = PageSize.ToString();
        this.LtlRecordCount.Text = RecordCount.ToString();
        if (RecordCount == 0)
        {
            this.LtlPageCount.Text = "0";
            this.LtlPageIndex.Text = "0";
        }
        else
        {
            this.LtlPageCount.Text = PageCount.ToString();
            this.LtlPageIndex.Text = (PageIndex + 1).ToString();
        }
    }
    #endregion
   
    #region 察看待审核的信息
    private void btnLookUnPassed_Click(object sender, System.EventArgs e)
    {
        ViewState["Keyword"] = "";
        ViewState["LookUnPassed"] = "T";
        ViewState["LookOwn"] = "";
        ViewState["is_yc"] = "";
        is_yc = 0;
        RptDataBind();
    }
    #endregion
    #region 查看自发信息
    private void btnLookOwn_Click(object sender, System.EventArgs e)
    {
        ViewState["Keyword"] = "";
        ViewState["LookUnPassed"] = "";
        ViewState["LookOwn"] = "T";
        is_yc = 0;
        RptDataBind();
    }
    #endregion
    #region 查看原创信息
    protected void lb_yc_Click(object sender, EventArgs e)
    {
        ViewState["Keyword"] = "";
        ViewState["LookUnPassed"] = "";
        ViewState["LookOwn"] = "";
        is_yc = 1;
        RptDataBind();
    }
    #endregion
    #region 判断主体对象是否具有指定的权限
    ///
    /// 判断主体对象是否具有指定的权限
    ///

    /// 要进行检查的权限名称
    /// 如果具有指定权限,则返回true,否则返回false
    private bool HasPermission(string permid)
    {
        ArrayList permissions = (ArrayList)Session["hg_loginname_perlist"];
        if (permissions == null)
            return false;
        foreach (object perm in permissions)
        {
            if (perm.ToString() == permid)
                return true;
        }
        return false;
    }
    #endregion
 
http://51xingfu.blog.51cto.com/219185/46139
 

你可能感兴趣的:(ASP.NET,permissions,object,server,session,string,asp.net)