Bug reports
Version 2.x > Bug reports > ViewState Problem View modes: 
User avatar
Member
Member
nick@bluemodus.com - 3/21/2007 7:32:19 PM
   
ViewState Problem
i have a very simple usercontorl i am trying to use in a page. It has a button and a gridview. On page load for the control i bind the gridview. see the code below:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim tree As New CMS.TreeEngine.TreeProvider

GridView1.DataSource = tree.SelectNodes("TEST", "/Events/%", "", True, "TEST.TestClass", "", "", -1)
GridView1.DataBind()
End If
End Sub

I am using this tag to place the user control in the page.
<cc1:CMSWebPartZone ID="zoneTopRight" runat="server" />

its the "Generic/user control" option when i add a web part to the page.

This works fine. However when I click the button to make a post back the gridview is not rendered. Basically the viewstate for the gridview dose NOT work. Is this a limitation of Kentico and is there a work around?

Also, if i use this same user control in regular page the gridview is displayed after the post back. So, i know it is not a problem with the user control.

I know i can place the user control in a regular page and then use an IFrame but i would very much like to avoid this.

This is pressing matter and any help i can get would be great.

User avatar
Member
Member
nick@bluemodus.com - 3/21/2007 7:42:24 PM
   
RE:ViewState Problem
Also, the result is the same if I use System.Web.UI.UserControl or CMS.PortalControls.CMSAbstractWebPart as the base class.

User avatar
Kentico Developer
Kentico Developer
jaroslavk - 5/8/2007 2:37:37 PM
   
RE:ViewState Problem
Hello,

This issue is caused by the fact, that the user control is loaded dynamically too late. Please edit webpart 'usercontrol.ascx' in the folder ~/CMSWebParts/General, find this code:
    public override void OnContentLoaded()
{
base.OnContentLoaded();
SetupControl();
}

and replace it with this code:
    protected override void OnInit(EventArgs e)
{
base.OnInit(e);
SetupControl();
}

It ensures that the usercontrol's viewstate is included in the page viewstate.

Best Regards,
Jaroslav Kordula

User avatar
Member
Member
mark-markscott - 1/15/2009 3:31:25 PM
   
RE:ViewState Problem
Changing the webpart 'usercontrol.ascx' code to:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
SetupControl();
}

... does not seem to be changing anything in my code. The problem I see is that the 'SetupControl()' method is dynamically loading (and then re-loading over and over again) your custom user control ('myTestControl.ascx' for example)... but each time it's probably getting a new ID. IF... in the page lifecycle... the ViewState is loaded from the parent AFTER the dynamic control is added to the collection, then it doesn't know how to wire back up the view state - because (I'm thinking) of the new control id. Here's a suggested change for the SetupControl() method...

Instead of:
Controls.Add(Page.LoadControl(this.UserControlPath));

Try:
Control c = Page.LoadControl(this.UserControlPath);
c.ID = "ucMyUserControl";
Controls.Add(c);

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 1/20/2009 2:23:28 AM
   
RE:ViewState Problem
Hi,

This thread is pretty old and it is related to one of the older versions. You are right and this was already fixed in the newer versions. There was no reply on this forum thread so we did not posted here the answer. Anyway, thank you for your post.

Best Regards,
Juraj Ondrus

User avatar
Member
Member
lauriec-eurekasoft - 1/21/2009 12:39:35 PM
   
RE:ViewState Problem
I just ran into exactly the same problem. We are using Kentico 3.1a. Do we need to upgrade to 4.0?

If I do the following code replacement in CMSWebParts_General_usercontrol, our user control works fine.
 //public override void OnContentLoaded()
//{
// base.OnContentLoaded();
// SetupControl();
//}

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
SetupControl();
}

Thanks

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 1/22/2009 5:00:00 AM
   
RE:ViewState Problem
Hi,

Yes, the upgrade is recommended. However, the upgrade package is not available for the RC version. It will be published with the 4.0 final version.

Best Regards,
Juraj Ondrus

User avatar
Member
Member
AlgarveWeb - 2/28/2011 3:28:36 AM
   
RE:ViewState Problem - VERY URGENT
Hi

I am having the same problem where the Repeater ItemCommand is not fired inside a WebPart.Tried above solutions to no avail.

Please advice, thanks, Mike.

Using V5.5 with latest Hotfix.

User avatar
Kentico Consulting
Kentico Consulting
kentico_borisp - 3/1/2011 8:15:28 AM
   
RE:ViewState Problem - VERY URGENT
Hello,

Could you please post the exact code you are using?

Best regards,
Boris Pocatko

User avatar
Member
Member
AlgarveWeb - 3/1/2011 8:25:55 AM
   
RE:ViewState Problem - VERY URGENT
Hi Boris

Thanks, please see below, the code has been, and still is, working online for over 3 years and online now, when porting to a WebPart ceased working:
Protected Sub rpt_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles rpt.ItemCommand
If IsPostBack Then
Select e.CommandName
Case "cmd_del"
Try
temp = New VistaDBConnection(ConfigurationManager.ConnectionStrings("temp").ToString)
my_sql = "DELETE FROM GOLF_booking Where ID = '" + CType(rpt.Items(e.Item.ItemIndex).FindControl("lbl_id"), Label).Text + "' "
my_cmd = New VistaDBCommand(my_sql, temp)
temp.Open()
my_dtr = my_cmd.ExecuteReader
my_dtr.Close()
temp.Close()
Catch ex As Exception
Response.Write(ex.ToString)
Finally
sel_GOLF_booking()
End Try
End Select
End If
End Sub

User avatar
Kentico Consulting
Kentico Consulting
kentico_borisp - 3/2/2011 8:46:07 AM
   
RE:ViewState Problem - VERY URGENT
Hello,

Regrettably, this code isn't sufficient for us. Could you please post a link to the exported webpart or links to your code behind file and aspx file of your webpart after you've uploaded it somewhere?

Best regards,
Boris Pocatko

User avatar
Member
Member
AlgarveWeb - 3/3/2011 4:26:12 AM
   
RE:ViewState Problem - VERY URGENT
Hi

Please download from:
http://www.algarveweb.com/AWB_Media/EN/download/CMSWebPart.zip

The code not working starts on Line 139

Thanks,
Mike

User avatar
Member
Member
AlgarveWeb - 3/9/2011 1:27:36 AM
   
RE:ViewState Problem - VERY URGENT
Hi

This problem keeps me from publishing the whole site - any news?

Thanks,
Mike

User avatar
Kentico Consulting
Kentico Consulting
kentico_borisp - 3/14/2011 9:34:13 AM
   
RE:ViewState Problem - VERY URGENT
Hello,

This seems to a general .NET issue. I've checked your code and there doesn't seem to be any Kentico-specific issue involved. Could you please try the suggestions described in the following forum?

http://forums.asp.net/p/1450218/3308436.aspx

Best regards,
Boris Pocatko