Failed to load viewstate

  • Thread starter Thread starter hitendra15
  • Start date Start date
H

hitendra15

Hi

I have created web user control which has Repeater control and
Linkbutton in ItemTemplate of repeater control, following is the code
for this control

On first load it runs fine but when page gets post back it gives
following error

Failed to load viewstate. The control tree into which viewstate is
being loaded must match the control tree that was used to save
viewstate during the previous request. For example, when adding
controls dynamically, the controls added during a post-back must match
the type and position of the controls added during the initial request.

Please can any one help to resolve this problem, I tried all ways but I
haven't find any solution.

Thanks in advance

Aspx code

<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="CustomNavigation.ascx.cs"
Inherits="CustomNavigation" %>
<table>
<tr>
<td>
<asp:Repeater ID ="m_rptNav" runat=server OnItemCommand =
"m_rptNav_OnItemCommand"
OnItemDataBound = "m_rptNav_OnItemDataBound" OnItemCreated
= "m_rptNav_OnItemCreated">
<ItemTemplate>
<asp:LinkButton ID="m_LinkIndex" runat=server Text="0"
CommandArgument='<%#
DataBinder.Eval(Container.DataItem, "nIndex")%>'
CommandName="Filter" ForeColor="#8C4510"
EnableViewState=true>
<%# DataBinder.Eval(Container.DataItem,
"nIndex")%>
</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
</td>
</tr>
</table>



Code behide

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class CustomNavigation : System.Web.UI.UserControl
{

#region << Private Fields >>

private int m_TotalNumbers = 0;
private int m_VisibleNumbers = 0;
private int m_SelectedNumber = 0;
private int m_MinNum = 0;
private int m_MaxNum = 0;
private bool m_Next = false;
private bool m_Previous = false;
private IList<string> m_Values;
private IList<int> m_nItem;
private IList<Guid> m_gItem;

private DataTable m_dt ;
#endregion

#region << Proprties >>

#region Read Write Properties
public int TotalNumbers
{
get { return m_TotalNumbers; }
set { m_TotalNumbers = value; }
}
public int VisibleNumbers
{
get { return m_VisibleNumbers; }
set { m_VisibleNumbers = value; }
}
public int SelectedNumber
{
get { return m_SelectedNumber; }
set { m_SelectedNumber = value; }
}
#endregion

#region Read only Properties
public int MinNum
{
get { return m_MinNum; }
}
public int MaxNum
{
get { return m_MaxNum; }
}
public bool Next
{
get { return m_Next; }
}
public bool Previous
{
get { return m_Previous; }
}


public string Values
{
get
{
if (m_SelectedNumber > 0)
return m_Values[m_SelectedNumber - 1];
else
return "";
}
}

public int NItem
{
get
{
if (m_SelectedNumber > 0)
return m_nItem[m_SelectedNumber - 1];
else
return 0;
}
}

public Guid GItem
{
get
{
if (m_SelectedNumber > 0)
return m_gItem[m_SelectedNumber - 1];
else
return Guid.Empty;

}
}
#endregion

#endregion

public CustomNavigation()
{
m_Values = new List<String>();
m_nItem = new List<int>();
m_gItem = new List<Guid>();
m_dt = new DataTable();
m_dt.Columns.Add("nIndex", Type.GetType("System.Int32"));
}

public CustomNavigation(int VisibleNumbers)
{
//Default
m_TotalNumbers = 3;
m_VisibleNumbers = VisibleNumbers;
m_MaxNum = VisibleNumbers;
m_dt.Columns.Add("nIndex", Type.GetType("System.Int32"));
}

public void Add(string Values,Guid Item)
{
m_Values.Add(Values);
m_gItem.Add(Item);
AddRowToTable();
}

public void Add(string Values, int Item)
{
m_Values.Add(Values);
m_nItem.Add(Item);
AddRowToTable();
}

private void AddRowToTable()
{
DataRow dr = m_dt.NewRow();
dr[0] = m_Values.Count;
m_dt.Rows.Add(dr);

ViewState["dataTable"] = m_dt;
ViewState["strValue"] = m_Values;
ViewState["nItem"] = m_nItem;
ViewState["gItem"] = m_gItem;

ViewState["Min"] = m_MinNum;
ViewState["Max"] = m_MaxNum;

ViewState["Selected"] = m_SelectedNumber;
ViewState["Total"] = m_TotalNumbers;
ViewState["Visible"] = m_VisibleNumbers;
ViewState["Next"] = m_Next;
ViewState["Previous"] = m_Previous;

}

protected void Page_Init(object sender, EventArgs e)
{

}

protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == true)
{
m_dt = (DataTable)ViewState["dataTable"];
m_Values = (List<string>)ViewState["strValue"];
m_nItem = (List<int>)ViewState["nItem"];
m_gItem = (List<Guid>)ViewState["gItem"];

m_MinNum = Convert.ToInt32(ViewState["Min"]);
m_MaxNum = Convert.ToInt32(ViewState["Max"]);

m_SelectedNumber = Convert.ToInt32(ViewState["Selected"]);
m_TotalNumbers = Convert.ToInt32(ViewState["Total"]);
m_VisibleNumbers = Convert.ToInt32(ViewState["Visible"]);
m_Next = Convert.ToBoolean(ViewState["Next"]);
m_Previous = Convert.ToBoolean(ViewState["Previous"]);
}
BindRepeater();
}
protected void m_rptNav_OnItemCommand(object
sender,RepeaterCommandEventArgs args)
{

}



protected void m_rptNav_OnItemCreated(object sender,
RepeaterItemEventArgs args)
{

}
protected void m_rptNav_OnItemDataBound(object sender,
RepeaterItemEventArgs args)
{
try
{
LinkButton lnkButton =
(LinkButton)args.Item.FindControl("m_LinkIndex");
int nIndex = Convert.ToInt32(lnkButton.CommandArgument);
lnkButton.Text = nIndex.ToString();
lnkButton.ToolTip = m_Values[nIndex-1];
}
catch (Exception ex)
{
throw ex;
}
}


private void BindRepeater()
{
try
{
m_rptNav.DataSource = m_dt;
DataBind();

}
catch (Exception exp)
{
throw exp;
}

}
}
 
Hitendra,

When you place a control on a page dynamically the exact controls must be
recreated on post back in order for viewstate to be able to access them
again. This is best shown rather than explained. I have a few examples of
dynamically loading controls on my website here:
www.aboutfortunate.com?page=codelibrary

Use the search box on the page to search for "dynamic control" and you'll
see a few examples. I think that if you look them over it will help you.
Specifically the "Reload dynamically created user controls in order to use
their viewstate" sample will be very helpful.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
Hi

I have created web user control which has Repeater control and
Linkbutton in ItemTemplate of repeater control, following is the code
for this control

On first load it runs fine but when page gets post back it gives
following error

Failed to load viewstate. The control tree into which viewstate is
being loaded must match the control tree that was used to save
viewstate during the previous request. For example, when adding
controls dynamically, the controls added during a post-back must match
the type and position of the controls added during the initial request.

Please can any one help to resolve this problem, I tried all ways but I
haven't find any solution.

Thanks in advance

Aspx code

<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="CustomNavigation.ascx.cs"
Inherits="CustomNavigation" %>
<table>
<tr>
<td>
<asp:Repeater ID ="m_rptNav" runat=server OnItemCommand =
"m_rptNav_OnItemCommand"
OnItemDataBound = "m_rptNav_OnItemDataBound" OnItemCreated
= "m_rptNav_OnItemCreated">
<ItemTemplate>
<asp:LinkButton ID="m_LinkIndex" runat=server Text="0"
CommandArgument='<%#
DataBinder.Eval(Container.DataItem, "nIndex")%>'
CommandName="Filter" ForeColor="#8C4510"
EnableViewState=true>
<%# DataBinder.Eval(Container.DataItem,
"nIndex")%>
</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
</td>
</tr>
</table>



Code behide

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class CustomNavigation : System.Web.UI.UserControl
{

#region << Private Fields >>

private int m_TotalNumbers = 0;
private int m_VisibleNumbers = 0;
private int m_SelectedNumber = 0;
private int m_MinNum = 0;
private int m_MaxNum = 0;
private bool m_Next = false;
private bool m_Previous = false;
private IList<string> m_Values;
private IList<int> m_nItem;
private IList<Guid> m_gItem;

private DataTable m_dt ;
#endregion

#region << Proprties >>

#region Read Write Properties
public int TotalNumbers
{
get { return m_TotalNumbers; }
set { m_TotalNumbers = value; }
}
public int VisibleNumbers
{
get { return m_VisibleNumbers; }
set { m_VisibleNumbers = value; }
}
public int SelectedNumber
{
get { return m_SelectedNumber; }
set { m_SelectedNumber = value; }
}
#endregion

#region Read only Properties
public int MinNum
{
get { return m_MinNum; }
}
public int MaxNum
{
get { return m_MaxNum; }
}
public bool Next
{
get { return m_Next; }
}
public bool Previous
{
get { return m_Previous; }
}


public string Values
{
get
{
if (m_SelectedNumber > 0)
return m_Values[m_SelectedNumber - 1];
else
return "";
}
}

public int NItem
{
get
{
if (m_SelectedNumber > 0)
return m_nItem[m_SelectedNumber - 1];
else
return 0;
}
}

public Guid GItem
{
get
{
if (m_SelectedNumber > 0)
return m_gItem[m_SelectedNumber - 1];
else
return Guid.Empty;

}
}
#endregion

#endregion

public CustomNavigation()
{
m_Values = new List<String>();
m_nItem = new List<int>();
m_gItem = new List<Guid>();
m_dt = new DataTable();
m_dt.Columns.Add("nIndex", Type.GetType("System.Int32"));
}

public CustomNavigation(int VisibleNumbers)
{
//Default
m_TotalNumbers = 3;
m_VisibleNumbers = VisibleNumbers;
m_MaxNum = VisibleNumbers;
m_dt.Columns.Add("nIndex", Type.GetType("System.Int32"));
}

public void Add(string Values,Guid Item)
{
m_Values.Add(Values);
m_gItem.Add(Item);
AddRowToTable();
}

public void Add(string Values, int Item)
{
m_Values.Add(Values);
m_nItem.Add(Item);
AddRowToTable();
}

private void AddRowToTable()
{
DataRow dr = m_dt.NewRow();
dr[0] = m_Values.Count;
m_dt.Rows.Add(dr);

ViewState["dataTable"] = m_dt;
ViewState["strValue"] = m_Values;
ViewState["nItem"] = m_nItem;
ViewState["gItem"] = m_gItem;

ViewState["Min"] = m_MinNum;
ViewState["Max"] = m_MaxNum;

ViewState["Selected"] = m_SelectedNumber;
ViewState["Total"] = m_TotalNumbers;
ViewState["Visible"] = m_VisibleNumbers;
ViewState["Next"] = m_Next;
ViewState["Previous"] = m_Previous;

}

protected void Page_Init(object sender, EventArgs e)
{

}

protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == true)
{
m_dt = (DataTable)ViewState["dataTable"];
m_Values = (List<string>)ViewState["strValue"];
m_nItem = (List<int>)ViewState["nItem"];
m_gItem = (List<Guid>)ViewState["gItem"];

m_MinNum = Convert.ToInt32(ViewState["Min"]);
m_MaxNum = Convert.ToInt32(ViewState["Max"]);

m_SelectedNumber = Convert.ToInt32(ViewState["Selected"]);
m_TotalNumbers = Convert.ToInt32(ViewState["Total"]);
m_VisibleNumbers = Convert.ToInt32(ViewState["Visible"]);
m_Next = Convert.ToBoolean(ViewState["Next"]);
m_Previous = Convert.ToBoolean(ViewState["Previous"]);
}
BindRepeater();
}
protected void m_rptNav_OnItemCommand(object
sender,RepeaterCommandEventArgs args)
{

}



protected void m_rptNav_OnItemCreated(object sender,
RepeaterItemEventArgs args)
{

}
protected void m_rptNav_OnItemDataBound(object sender,
RepeaterItemEventArgs args)
{
try
{
LinkButton lnkButton =
(LinkButton)args.Item.FindControl("m_LinkIndex");
int nIndex = Convert.ToInt32(lnkButton.CommandArgument);
lnkButton.Text = nIndex.ToString();
lnkButton.ToolTip = m_Values[nIndex-1];
}
catch (Exception ex)
{
throw ex;
}
}


private void BindRepeater()
{
try
{
m_rptNav.DataSource = m_dt;
DataBind();

}
catch (Exception exp)
{
throw exp;
}

}
}
 
Hi

Thanks for your reply but i tried your way also but still it is giving
same error :( please can you help in this, i will be thankfull to
you......

Thanks & Regards
Hitendra
 
Ooops,

Ignore that last post, for some reason I didn't see the code you provided in
your first post. I found it now though!

:)

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
Hitendra,

As far as I can tell you are dynamically creating some nodes but you are not
storing those nodes anywhere and recreating them on postback. When you
recreate a control you also need to recreate the controls inside of said
control. The easiest way to do so would be to track what controls have been
created via viewstate. Similar to how I am setting the number of text boxes
on a page in the example I referred you to earlier.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
Back
Top