Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

Problem in user control web part

  Asked By: Tommie    Date: Aug 15    Category: Sharepoint    Views: 3205

I created a usercontrol web part with following code.....in the
webpart.cs(webpart class)

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;

namespace WebPartLibrary1
/// <summary>
/// Description for WebPart1.
/// </summary>
ToolboxData("<{0}:WebPart1 runat=server></
public class WebPart1 :

private const string defaultText = "";
private string _userControl = defaultText;
private Control _control = null;

Category("Custom Properties"),
FriendlyName("User Control (.ascx)"),
Description("Path to the User Control (.ascx)")]
public string UserControl
return _userControl;

_userControl = value;

public bool ShouldSerializeUserControl()
return true;

protected override void CreateChildControls()
base.CreateChildControls ();

if (_userControl != defaultText)
_control =
_control = new LiteralControl
(string.Format("To link to content, <a
('{0}','{1}','{2}');\">open the tool pane</a> and then type a URL in
the Link text box.", 1, 129, this.ID));
catch (System.Exception ex)
_control = new LiteralControl
(string.Format("Error: unable to load {0}<br />Details:
{1}", _userControl, ex.Message));

if (_control != null)
// Add to the Controls collection to
support postback

protected override void RenderWebPart(HtmlTextWriter
if (_control != null)
_control.EnableViewState = false;


The problem is that first time when I load a user control it gets
Loaded properly i.e the value entered as string in custom properties
gets set to UserControl property defined above..
However when I change the string in custom properties and then click
apply the control goes first to create child controls method where
the user control which was given earlier gets Loaded as the control
does not initially flow to the Location where the UserControl
Property gets set..and as a result user control with changed value is
not Loaded...
Now again if I click on Apply changes button in the web part tool pane
the user control which which I required previously gets Loaded.
So in short I have to click on the apply changes button twice to get
the user Control Loaded whoch I require.
I have been debugging for last three days...
Please Help



2 Answers Found

Answer #1    Answered By: Brooks Bond     Answered On: Aug 15

I didn't look closely at your code  but you will likely need to move the
code that gets the altered value  from the CreateChildControls method
(run during PreRender) to the RenderWebPart method (run after PostBack
and event handling much later in the Page Life Cycle). If that doesn't
solve the problem, I'll be surprised but post again.

Answer #2    Answered By: Gregg Wilkinson     Answered On: Aug 15

I tried that option of getting the altered value  in render web  part
method instead of createchildcontrols method.....The required user
control(with altered value) gets loaded....
However the user  control does not work properly....For example a user
control which adds two numbers does not show any result...which used
to work initially when I tried getting the control  in
createchildcontrols method as in the code  ....

I think (with the code which I tried initially) after the control is
rendered in the renderwebpart method....
its value gets stored may be in viewstate...so I had also set
enableviewstate to false for the control before calling the
rendercontrol method...However the Enableview state attribute in the
Web.config of the virtual server is set to true....which I guess
overrides the change which I set in the code....I also tried to
change it to false but then sharepoint  does not allow me to add the
web part  to the page...then the web part fails...

So the problem  is as a developer I cannot cotrol the flow sequence
(also I could not find any workaround for this problem) or is it that
web part should be used only for adding one user control.
Please help me with this.

Didn't find what you were looking for? Find more on Problem in user control web part Or get search suggestion and latest updates.