Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have repeater which use to add data into database
i am using paging in this repeater and work well
when i am select other page data in other pages clear
i am trying to use view stat to keep data after rebind repeater but there is error
here is my code
ASP.NET
<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>


and in CS coding in Page Load
C#
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();
    }

}

C#
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;
}
/// <summary>
/// set or get ObjectDataSource that's use to bind the control
/// Like(Repeater or Datalist)
/// </summary>
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;
        }

    }



}
/// <summary>
/// set or get Control Name EX. (Repeater1 or Datalist1)
/// </summary>
public object ObjectControl { get; set; }

/// <summary>
/// set or get count of pages
/// page size determine how many records will appears in every page
/// </summary>
public int PageSize { get; set; }
protected void btnnext2_Click(object sender, EventArgs e)
{

    //  MultiView1.SetActiveView(View3);

}
private static DataTable ConvertToDatatable<T>(List<T> data)
{
    PropertyDescriptorCollection props =
        TypeDescriptor.GetProperties(typeof(T));
    DataTable table = new DataTable();
    for (int i = 0; i < props.Count; i++)
    {
        PropertyDescriptor prop = props[i];
        if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
            table.Columns.Add(prop.Name, prop.PropertyType.GetGenericArguments()[0]);
        else
            table.Columns.Add(prop.Name, prop.PropertyType);
    }
    object[] values = new object[props.Count];
    foreach (T item in data)
    {
        for (int i = 0; i < values.Length; i++)
        {
            values[i] = props[i].GetValue(item);
        }
        table.Rows.Add(values);
    }
    return table;
}


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();
}
Posted
Comments
Afzaal Ahmad Zeeshan 1-Feb-15 8:43am    
What is the error that you're talking about? That error is required to solve this problem. Also, please try to tell how does the data disappear?
yallakorra 1-Feb-15 8:57am    
Type 'HR.tblstaticTranslation' in Assembly 'HR, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable. @afzaal

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900