Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Failed to load viewstate

Reply
Thread Tools

Failed to load viewstate

 
 
hitendra15@gmail.com
Guest
Posts: n/a
 
      01-11-2006
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;
}

}
}

 
Reply With Quote
 
 
 
 
S. Justin Gengo
Guest
Posts: n/a
 
      01-11-2006
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
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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;
> }
>
> }
> }
>



 
Reply With Quote
 
 
 
 
hitendra15@gmail.com
Guest
Posts: n/a
 
      01-11-2006
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

 
Reply With Quote
 
S. Justin Gengo
Guest
Posts: n/a
 
      01-11-2006
Hitendra,

Do you have some sample code?

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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
>



 
Reply With Quote
 
S. Justin Gengo
Guest
Posts: n/a
 
      01-11-2006
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
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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
>



 
Reply With Quote
 
S. Justin Gengo
Guest
Posts: n/a
 
      01-11-2006
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
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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
>



 
Reply With Quote
 
hitendra15@gmail.com
Guest
Posts: n/a
 
      01-12-2006
thanks for the support justin i will try out this.........


Thanks
Hitendra

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Errors: Failed to load viewstate. & Validation of viewstate MAC failed. sck10 ASP .Net 6 09-01-2006 05:59 PM
Failed to load viewstate. The control tree into which viewstate... Josema ASP .Net Building Controls 2 05-22-2005 10:38 AM
Failed to load viewstate. The control tree into which viewstate... Josema ASP .Net Web Controls 0 05-09-2005 10:58 AM
Failed to load viewstate (i.e., I'm doomed). Mike Hnatt ASP .Net 2 11-03-2003 08:27 PM
Failed to load ViewState John Kirksey ASP .Net 5 07-22-2003 03:06 PM



Advertisments