转发器中的寻呼(保持数据)

时间:2015-02-02 08:06:22

标签: c# asp.net repeater

用于将数据插入数据库的asp转发器

<asp:Repeater ID="Repeater1" runat="server">
  <HeaderTemplate>
        <div class="form-group" >
          <div class="col-sm-2 control-label">
              English
              </div>
       <center>   <div class="col-sm-2 control-label">
          Other Language
              </div>
           </center>
            </div>
  </HeaderTemplate>
    <ItemTemplate>
           <div class="form-group" >
               <div class="col-sm-2 control-label">  <%# Eval("EnglishTranslation")%><asp:Label ID="Label1" runat="server" Text=' <%# Eval("StaticTRanslationId")%>' Visible="false"></asp:Label> </div>
        <div class="col-sm-10">    <input id="Text1" type="text" class="form-control"  runat="server" /></div>
        </div>
    </ItemTemplate>
</asp:Repeater>

我正在使用分页和它的工作正常,但是当转到输入文本中清除的下一页或上一页数据时 我试图保持数据在视图统计,但我得到错误这是我的c#代码

Type 'HR.tblstaticTranslation' in Assembly 'HR, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable. 



 List<tblstaticTranslation> list = new List<tblstaticTranslation>();
        ReturnDbForTesEntities db = new ReturnDbForTesEntities();
        protected void Page_Load(object sender, EventArgs e)
        {

            list = db.tblstaticTranslation.Where(p => p.LanguageId == 1).ToList();
            if (!IsPostBack)
            {
                GetItems();
                retrivedata();
            }

        }
     public int CurrentPage
        {
            get
            {
                //get current page number
                object obj = this.ViewState["_CurrentPage"];
                if (obj == null)
                {
                    return 0;
                }
                else
                {
                    return (int)obj;
                }
            }
            set
            {
                //set in viewstate the current page number
                this.ViewState["_CurrentPage"] = value;
            }
        }
        private int GetItems()
        {
            //create new instance of PagedDataSource
            PagedDataSource objPds = new PagedDataSource();
            //set number of pages will appear
            Ods = list;
            objPds.PageSize = 10;
            objPds.DataSource = Ods.ToList(); ;
            objPds.AllowPaging = true;
            int count = objPds.PageCount;
            objPds.CurrentPageIndex = CurrentPage;
            if (objPds.Count > 0)
            {
                //dispaly controls if there are pages
                btnPrevious.Visible = true;
                btnNext.Visible = true;
                btnLastRecord.Visible = true;
                btnFirstRecord.Visible = true;
                lblCurrentPage.Visible = true;
                lblCurrentPage.Text = "Page " +
                  Convert.ToString(CurrentPage + 1) + " of " +
                  Convert.ToString(objPds.PageCount);
            }
            else
            {
                //disable controls if there are no pages
                btnPrevious.Visible = false;
                btnNext.Visible = false;
                btnLastRecord.Visible = false;
                btnFirstRecord.Visible = false;
                lblCurrentPage.Visible = false;
            }
            btnPrevious.Enabled = !objPds.IsFirstPage;
            btnNext.Enabled = !objPds.IsLastPage;
            btnLastRecord.Enabled = !objPds.IsLastPage;
            btnFirstRecord.Enabled = !objPds.IsFirstPage;

            Repeater1.DataSource = objPds;
            Repeater1.DataBind();
            //}
            return count;
        }

        public List<tblstaticTranslation> Ods { get; set; }

        void getdata()
        {
            List<tblstaticTranslation> liststatic = new List<tblstaticTranslation>();
            tblstaticTranslation tbstatic = new tblstaticTranslation();
            foreach (RepeaterItem item in Repeater1.Items)
            {

                string x = ((Label)item.FindControl("Label1")).Text;
                HtmlInputText y = ((HtmlInputText)item.FindControl("Text1"));

                if (y.Value .Trim() != string.Empty)
                {
                    tbstatic.StaticTRanslationId = Convert.ToInt32(x);
                    tbstatic.OtherLanguageTranslation = y.Value ;
                    if (liststatic.Where(p => p.StaticTRanslationId == tbstatic.StaticTRanslationId).FirstOrDefault() == null)
                    {
                        liststatic.Add(tbstatic);
                    }
                    else
                    {

                        liststatic.Remove(liststatic.Where(p => p.StaticTRanslationId == tbstatic.StaticTRanslationId).FirstOrDefault());
                        liststatic.Add(tbstatic);
                    }
                    tbstatic = new tblstaticTranslation();
                }
            }
            var dt = ConvertToDatatable(liststatic);
            ViewState.Add("data", dt);

        }
        void retrivedata()
        {
            //  List<tblstaticTranslation> data = new List<tblstaticTranslation>();
            tblstaticTranslation[] newArrayname = (tblstaticTranslation[])ViewState["data"];

            List<tblstaticTranslation> data = new List<tblstaticTranslation>();
            if (newArrayname != null)
            {
                data = new List<tblstaticTranslation>(newArrayname);
            }

            DataTable dt = (DataTable)ViewState["data"];
            DataView dataView = new DataView(dt.Select);

            foreach (RepeaterItem items in Repeater1.Items)
            {
                string x = ((Label)items.FindControl("Label1")).Text;
                HtmlInputText y = ((HtmlInputText)items.FindControl("Text1"));
                int id = Convert.ToInt32(x);
                if (dt.Select("StaticTRanslationId=" + id) != null)
                {
                    y.Value = data.Where(p => p.StaticTRanslationId == id).FirstOrDefault().OtherLanguageTranslation;
                }

            }

        }

        public object ObjectControl { get; set; }


        protected void btnFirstRecord_Click(object sender, EventArgs e)
        {
            CurrentPage = 0;

             if (ViewState["data"] != null)
             {

                 getdata();
             //    retrivedata();
             }
             else
             {
              //   retrivedata();
                 getdata();

             }
             GetItems();
        }

        protected void btnLastRecord_Click(object sender, EventArgs e)
        {
            CurrentPage = GetItems() - 1;


            if (ViewState["data"] != null)
            {

                getdata();
             //   retrivedata();
            }
            else
            {
             //   retrivedata();
                getdata();

            }
            GetItems();
        }

        protected void btnPrevious_Click1(object sender, EventArgs e)
        {
            CurrentPage -= 1;


            if (ViewState["data"] != null)
            {

                getdata();
            //    retrivedata();
            }
            else
            {
               // retrivedata();
                getdata();

            }
            GetItems();
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            CurrentPage += 1;


            if (ViewState["data"] != null)
            {

                getdata();
             //   retrivedata();
            }
            else
            {
                getdata();
           //     retrivedata();


            }
            GetItems();
        }

1 个答案:

答案 0 :(得分:0)

如果您想在ViewState中保留一些数据,则必须将其标记为serializable

如果您已将某些地方声明HR.tblstaticTranslation,请使用serializable属性,如下所示

[Serializable()]
class tblstaticTranslation
{
    ...
}