自定义导航页面控件再探

    上一篇我用一下午时候写了一个自定义页面导航控件,虽然可以使用,但是问题还是很多的。比如:

1.浏览器的一次postback,可能会执行两次代理。这极大的影响了服务器效率。

2.不能区分是自定义页面导航空间的子空间还是页面上的其他空间激发的页面回发。

下面的代码可以解决第一个重大问题,但是未能解决第二个问题。目前还在探索中。

下面源码附上:

        #region functions

        /// <summary>

        /// excute the delegate function and refresh the total pagecount info.

        /// </summary>

        private void Excutedelegate()

        {

            OnrefreshData.Invoke(Convert.ToInt32(LblCurrent.Text), Convert.ToInt16(DdlPageSize.SelectedItem.Value));

            if (this.pageCount > 0)

            {

                LblTotal.Text = this.pageCount.ToString();

            }

            else

            {

                LblTotal.Text = "1";

            }

            LbtnEnableControl();

        }



        



        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                LblCurrent.Text = "1";

            }

        }



        /// <summary>

        /// 在页面即将提交的时候,执行绑定方法。

        /// </summary>

        /// <param name="e"></param>

        protected override void OnPreRender(EventArgs e)

        {

            if (OnrefreshData != null)

            {//excute the delegate function

                Excutedelegate();

            }

            base.OnPreRender(e);

        }



        /// <summary>

        /// go to first page

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        protected void BtnFirst_Click(object sender, EventArgs e)

        {



            LblCurrent.Text = "1";

            //LbtnEnableControl();

            //if (OnrefreshData != null)

            //{

            //    Excutedelegate();

            //}



        }



        /// <summary>

        /// go to previous page

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        protected void BtnPre_Click(object sender, EventArgs e)

        {

            if (LblCurrent.Text != "1")

            {

                LblCurrent.Text = (Convert.ToInt32(LblCurrent.Text) - 1).ToString();

            }

            //LbtnEnableControl();

            //if (OnrefreshData != null)

            //{

            //    Excutedelegate();

            //}

        }



        /// <summary>

        /// go to next page

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        protected void BtnNext_Click(object sender, EventArgs e)

        {

            LblCurrent.Text = (Convert.ToInt32(LblCurrent.Text) + 1).ToString();

            //LbtnEnableControl();

            //if (OnrefreshData != null)

            //{

            //    Excutedelegate();

            //}

        }



        /// <summary>

        /// go to last page

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        protected void BtnLast_Click(object sender, EventArgs e)

        {

            LblCurrent.Text = LblTotal.Text;

            //LbtnEnableControl();

            //if (OnrefreshData != null)

            //{

            //    Excutedelegate();

            //}

        }



        /// <summary>

        /// go to the designated page

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        protected void BtnPageTo_Click(object sender, EventArgs e)

        {

            int intpageto;

            if (int.TryParse(TxtPageTo.Text.Trim(), out intpageto))

            {



            }

            else

            {

                ScriptManager.RegisterStartupScript(this, this.GetType(), "JS", "<script>alert('请输入正确的数字!');</script>", false);

            }

            if (intpageto <= Convert.ToInt32(LblTotal.Text))

            {

                LblCurrent.Text = intpageto.ToString();

            }

            else

            {

                LblCurrent.Text = LblTotal.Text;

            }

            

            //if (OnrefreshData != null)

            //{

            //    Excutedelegate();

            //}

        }



        /// <summary>

        /// set the enable property of linkbutton according to page index and total pagecount

        /// </summary>

        private void LbtnEnableControl()

        {

            if (LblCurrent.Text == "1" && LblTotal.Text == "1")

            {

                BtnFirst.Enabled = false;

                BtnPre.Enabled = false;

                BtnNext.Enabled = false;

                BtnLast.Enabled = false;

            }

            else if (LblCurrent.Text == "1" && LblTotal.Text != "1")

            {

                BtnFirst.Enabled = false;

                BtnPre.Enabled = false;

                BtnNext.Enabled = true;

                BtnLast.Enabled = true;

            }

            else if (LblCurrent.Text != "1" && LblCurrent.Text == LblTotal.Text)

            {

                BtnFirst.Enabled = true;

                BtnPre.Enabled = true;

                BtnNext.Enabled = false;

                BtnLast.Enabled = false;

            }

            else

            {

                BtnFirst.Enabled = true;

                BtnPre.Enabled = true;

                BtnNext.Enabled = true;

                BtnLast.Enabled = true;

            }

        }

        #endregion



        protected void DdlPageSize_SelectedIndexChanged(object sender, EventArgs e)

        {

        }

  本次代码最大改进的地方就是将执行代理的函数放在了页面的preRender阶段执行。了解页面回发生命周期的人一定知道为什么要放在这里去执行。

      这篇文章只是为了记录,所以写的不仔细。

 

你可能感兴趣的:(自定义)