Setting custom properties of a user control loaded in a web part

Last post 11-05-2009 8:27 AM by trivedid. 7 replies.

Sort Posts:

  • Setting custom properties of a user control loaded in a web part

    09-15-2008, 12:54 PM
    • Member
      26 point Member
    • nrocha
    • Member since 06-08-2007, 3:36 PM
    • Posts 24

     Hello,

     I've created a user control with some custom properties. Next, I've created a web part where I loaded my user control and added a label.

    I've connected this web part with another web part and I'm setting both the value of the label and the value of one property of the custom control to a string variable that I receive from the connection.

    The label text is being correctly updated, but the user control property isn't (I know this because I have a button on the control that puts the contents of the custom variable in a textbox and nothing is appearing there).

     

    Any hints on this problem?

    Thanks in advance...

  • Re: Setting custom properties of a user control loaded in a web part

    09-16-2008, 3:22 PM
    • Member
      446 point Member
    • Sasa Popovic
    • Member since 02-17-2008, 9:32 AM
    • Serbia
    • Posts 93

    Hello,

    You are probably storing the value of property in a variable whose value is lost during post-backs. I suggest you post the source code of at least the property (or the whole web part and control).

    I guess you didn't use ViewState to store the value that is set to property. So if you didn't then you should probably modify your property to something like:

    public string MyProperty{

    get{ return ViewState["MyProperty"];}

    set{ ViewState["MyProperty"] = value;}

    }

    Regards,

    Sasa

  • Re: Setting custom properties of a user control loaded in a web part

    09-17-2008, 5:53 AM
    • Member
      26 point Member
    • nrocha
    • Member since 06-08-2007, 3:36 PM
    • Posts 24

    Ok, I think that was the first step towards a solution :-)

    But now I have some other issue regarding the order of the events: this web part is supposed to be connected to other web parts and the value that I want to see on the user control is the value passed from that connection. But when I hit the button, the value I see on the textbox is the one from the previous postback because the event handler of the button (defined in the ascx file) is running before the method that retrieves the value from the other web part.

    Can you help me with this?

    I'm posting the relevant code of both the web part and the user control.

     

    WebPart1.cs:
    public class
    WebPart1 : Microsoft.SharePoint.WebPartPages.WebPart
    {
    public String Start
    {
    get { return ViewState["Start"].ToString(); }
    set { ViewState["Start"] = value; }
    }

    public WebPart1()
    {
    }

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

    Label l = new Label();
    l.ID = "_label";
    this.Controls.Add(l);

    testcontrol t = (testcontrol)Page.LoadControl("~/_controltemplates/testcontrol.ascx");

    t.ID = "_testcontrol";
    t.BeginDate = this.Start;
    this.Controls.Add(t);
    }

    protected override void OnPreRender(EventArgs e)
    {
    base.OnPreRender(e);

    testcontrol t = (testcontrol) this.FindControl("_testcontrol");

    t.BeginDate = this.Start;
    }

    [ConnectionConsumer("Parameters")]
    public void GetProvider(IWebPartParameters providerPart)
    {
    //Specify what properties this part can map
    PropertyDescriptor[] property ={ TypeDescriptor.GetProperties(this)["Start"] };
    PropertyDescriptorCollection schema = new PropertyDescriptorCollection(property);
    providerPart.SetConsumerSchema(schema);

    //Give provider reference to callback function
    ParametersCallback callback = new ParametersCallback(ReceiveParameters);
    providerPart.GetParametersData(callback);
    }

    public void ReceiveParameters(IDictionary parameters)
    {
    testcontrol t = (testcontrol)this.FindControl("_testcontrol");
    Label l = (Label)this.FindControl("_label");
    foreach(DictionaryEntry dic in parameters)
    {
    switch(dic.Key.ToString())
    {
    case "Start": Start = dic.Value.ToString();
    t.BeginDate = Start;
    l.Text = Start;
    break;
    }
    }
    }
    }

      

    testcontrol.ascx:

    public partial class testcontrol : System.Web.UI.UserControl
    {
    private String beginDate;

    public String BeginDate
    {
    get { return ViewState["Start"].ToString(); }
    set { ViewState["Start"] = value; }
    }

    private String endDate;

    public String EndDate
    {
    get { return endDate; }
    set { endDate = value; }
    }


    protected override void OnLoad(EventArgs e)
    {
    this.EnableEventHandlers();

    if (this.IsPostBack == false)
    {
    }

    base.OnLoad(e);
    }


    protected void EnableEventHandlers()
    {
    this.Button1.Click += new EventHandler(EventBtOKClicked);
    }

    protected void EventBtOKClicked(object sender, EventArgs e)
    {
    if (this.Button1 != null)
    {
    TextBox1.Text = BeginDate;
    TextBox2.Text = EndDate;
    }
    }
    }
     
  • Re: Setting custom properties of a user control loaded in a web part

    09-17-2008, 6:17 AM
    • Member
      446 point Member
    • Sasa Popovic
    • Member since 02-17-2008, 9:32 AM
    • Serbia
    • Posts 93

    Modify web part and user control so that:

    • your custom properties are set on button clicks and similar post-back events (set values into ViewState as you did with "String" property)
    • set values to ASP.NET controls (like TextBox) in OnPreRender events

    If you follow those guidelines then everything will work fine.

    Btw, don't forget to mark all posts that helped you as "Answer" Wink

    Regards,

    Sasa

  • Re: Setting custom properties of a user control loaded in a web part

    09-17-2008, 6:55 AM
    • Member
      26 point Member
    • nrocha
    • Member since 06-08-2007, 3:36 PM
    • Posts 24

    Hi,

    Sasa Popovic:

    Modify web part and user control so that:

    • your custom properties are set on button clicks and similar post-back events (set values into ViewState as you did with "String" property)

     

    the only property I'm using is already being set into ViewState within the connection consumer method. I'm not currently looking at the other user control's property ('EndDate').

    Sasa Popovic:

    • set values to ASP.NET controls (like TextBox) in OnPreRender events

     

    I wanted that the user control and web part were independent from each other. I mean, I didn't want to set the values of the controls inside my user control from the web part... that's why I've exposed the user control properties...

    Also, I've taken a look at the order of the events when I click the user control's button and here is what is happening:

    1. WebPart1: CreateChildControls
    2. testcontrol.ascx: OnLoad
    3. testcontrol.ascx: EventBtOkClicked
    4. WebPart1: GetProvider
    5. WebPart1: ReceiveParameters
    6. WebPart1: OnPreRender

     

    So, this is why I'm not getting the most actual value in the textbox, because when the value is being accessed (EventBtOkClicked) I haven't received the parameters from the connection (it only happens on step 5). In OnPreRender, I'm setting the user control's property but this will only be accessed the next time EventBtOkClicked fires, thus the delay...

    Actually, this situation with the TextBoxes inside the user control is just for testing purposes. What I want to achieve is that when the button is clicked I can used the value of the user control's properties as arguments for a series of calculations that will be performed inside the user control. The web part would only serve as wrapper for the user control, passing it the values obtained from other web parts in that page. That's why I don't think that setting the values of the ASP.NET controls in OnPreRender would solve my problem.

     

    Is is possible to achieve what I've described?

     

    Thanks in advance.

  • Re: Setting custom properties of a user control loaded in a web part

    09-17-2008, 12:07 PM
    • Member
      12 point Member
    • bluerose
    • Member since 06-18-2008, 9:21 PM
    • Posts 35

    Does your user control inherit from IWebPart, IWebEditable, and IWebActionable?  That's #1.

    Also, here's an example of how to make a property of a user control personalizable.  You need to add the Personalizable() attribute to the custom property.

     private string _title = String.Empty;

             [Personalizable(), WebDisplayName("Title")]
            public string Title
            {
                get
                {
                    return _title;
                }
                set
                {
                    _title = value;
                }
            }

  • Re: Setting custom properties of a user control loaded in a web part

    11-04-2009, 10:07 PM
    • Member
      6 point Member
    • trivedid
    • Member since 11-02-2009, 1:47 PM
    • Posts 3

    I spent hours what you described in the first post and found the solution ...


    easy way to solve the problem is below


    in webpart OnPreRender  do following

               
                            TextBox fn =(TextBox) usercontrol.FindControl("txtFirstName");
                            fn.Text = this.m_provider.ProvidedFirstName;


    and it will show value in the user control's first name text box



  • Re: Setting custom properties of a user control loaded in a web part

    11-05-2009, 8:27 AM
    • Member
      6 point Member
    • trivedid
    • Member since 11-02-2009, 1:47 PM
    • Posts 3

    better way to do it

    create a function in user control

    internal void getuserinformation(string username)

    {

     your logic goes here


    }


    from webpart call the usercontrol.getuserinformation("value from web connector") in OnPreRender Event ...




Page 1 of 1 (8 items)