I am having a User control [ascx] which is a radgrid. It also has a Edit column template which allow user to Insert/ change/update the values.
I am loading this user control on click of a button [Say EDIT] in aspx page.
When I click on EDIT button of aspx page the user control loads perfectly, but when I click on the Add New Record of user control I get this error message.
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.
//code of user control -ascx
<telerik:RadGrid AllowAutomaticDeletes="True"
AllowAutomaticInserts="False" AllowAutomaticUpdates="False"
Height="410px" ID="rgrd1" runat="server" AutoGenerateColumns="False"
OnUpdateCommand="rgrd1_UpdateCommand"
OnInsertCommand="rgrd1_InsertCommand" OnNeedDataSource="rgrd1_NeedDataSource" GridLines="None">
<MasterTableView CommandItemDisplay="top" CommandItemStyle-Wrap="False" CommandItemStyle-HorizontalAlign="Left"
DataKeyNames="Type">
<Columns>
<telerik:GridEditCommandColumn UniqueName="EditColumn" HeaderStyle-HorizontalAlign="left"
HeaderText="Edit" ButtonType="ImageButton">
</telerik:GridEditCommandColumn>
<telerik:GridTemplateColumn DataField="Type" HeaderStyle-HorizontalAlign="left"
HeaderText="Type" UniqueName="Type">
<ItemTemplate>
<asp:Label ID="TypeLabel" runat="server" Text='<%# Eval("Type") %>'></asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
<EditFormSettings EditFormType="Template">
<EditColumn FilterControlAltText="Filter EditCommandColumn1 column" UniqueName="EditCommandColumn1">
</EditColumn>
<FormTemplate>
<table>
<td>
<telerik:RadTextBox ID="txtType" Width="50" runat="server" Text='<%# Eval("Type") %>'>
</telerik:RadTextBox>
</td>
</tr>
</table>
</FormTemplate>
</EditFormSettings>
</MasterTableView>
<HeaderStyle HorizontalAlign="Center" />
</telerik:RadGrid>
..................................... Code of my aspx page - in which i am loading the above usercontrol in Radwindow
On click of EDIT button - below code i have written-
RadWindow window = new RadWindow();
window.Height = Unit.Pixel(500);
window.Width = Unit.Pixel(500);
window.VisibleOnPageLoad = true;
UserControl uc = (UserControl)Page.LoadControl("../../Controls/TypeUserControl.ascx");
uc.EnableViewState = false;
window.ContentContainer.Controls.Add(uc);
pnl.Controls.Add(window);
When I click on Add New Record in usercontrol it throws the exception "Failed to load view state:"
I have aspx page on which this user control is loaded is being inherited by my BasePage.
Exception is coming in OnPreRender of base page
protected override void OnPreRender(EventArgs e)
{
try
{
base.OnPreRender(e);
}
}
User control - code behing
public partial class TypeUserControl : System.Web.UI.UserControl
{
protected void rgrd1Types_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
try
{
//dbase call
}
catch (Exception ex)
{
SetValidationMessage(ex.Message.ToString());
}
}
protected void rgrd1_InsertCommand(object source, GridCommandEventArgs e)
{
try
{
//dbase call
}
catch (Exception ex)
{
SetValidationMessage(ex.Message.ToString());
}
}
UserControl uc = (UserControl)Page.LoadControl("TypeUserControl.ascx");- MethodMan