none
asp.net c# 分页问题 (aspnetpager) RRS feed

  • 问题

  • asp.net c# 分页问题。用的是aspnetpager,看了实例,默认用的是StoredProcedure,但是我不会,就想改成Text的形式。部分代码如下:
    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string total = "select count(id) from news";
                int totalOrders = (int)SqlHelper.ExecuteScalar(CommandType.Text, total);
                AspNetPager1.RecordCount = totalOrders;
                //bindData(); //使用url分页,只需在分页事件处理程序中绑定数据即可,无需在Page_Load中绑定,否则会导致数据被绑定两次
            }
        }

        void bindData()
        {
            Repeater1.DataSource = SqlHelper.ExecuteReader(CommandType.StoredProcedure, ConfigurationManager.AppSettings["pagedSPName"],
                new SqlParameter("@startIndex", AspNetPager1.StartRecordIndex),
                new SqlParameter("@endIndex", AspNetPager1.EndRecordIndex));
            Repeater1.DataBind();
        }

        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
            bindData();
        }

    Page_Load的部分我已经改了,但是bindData()不知道怎么改,不知道command text最终写在哪。。 SqlHelper.cs里的内容对我来说有点复杂。求大神们指点!!
    ------------------------------------------------------------------------------------------------------
    附上部分 SqlHelper.cs内容:
    public sealed class SqlHelper
    {
    private SqlHelper(){}

      public static SqlDataReader ExecuteReader(CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)
        {
            SqlCommand cmd = new SqlCommand();
            SqlConnection conn = new SqlConnection(CONN_STRING);

                   try
            {
                PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
                SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                cmd.Parameters.Clear();
                return rdr;
            }
            catch
            {
                conn.Close();
                throw;
            }
        }
        public static readonly string CONN_STRING = ConfigurationManager.ConnectionStrings["yuchen_beta"].ConnectionString;
    。。。。。。。。。。
    public static object ExecuteScalar(CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)
        {
            SqlCommand cmd = new SqlCommand();
    。。。。。。。。。。。。。。
            using (SqlConnection conn = new SqlConnection(CONN_STRING))
            {
                PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
                object val = cmd.ExecuteScalar();
                cmd.Parameters.Clear();
                return val;
            }
        }

    啥都不懂

    2012年4月21日 6:08

答案