2

I'm passing parameters to a WebPart from a textbox to have a better UI experience for a personalizable WebPart which in this case takes the form of a twitter feed.

The problem I have, is in the scenario that a user has more than one instance of this WebPart on the page, the click event which sets the parameter cannot tell which WebPart is making the call.

Can I pass some kind of parameter from EventArgs to GetLimitedWebPartManager that identifies a unique instance of the WebPart?

protected void setTwitterName_Click(object sender, EventArgs e)
{
    string Name = TwitterHandle.Text.Trim();

    if (!String.IsNullOrEmpty(Name))
    {
        using (var webPartManager = SPContext.Current.Web.GetLimitedWebPartManager("default.aspx",PersonalizationScope.User))
        {
            try
            {
                foreach (var twitterWebPart in webPartManager.WebParts.Cast<WebPart>().Where(webPart => webPart.Title == "Twitter").Cast<Twitter>())
                {
                    twitterWebPart.Name = Name;
                    webPartManager.SaveChanges(twitterWebPart);
                    PropertiesWebPart.Name = Name;
                    DisplayTweets();
                }
            }
            catch
            {
            }
        }
    }
}
2
  • 1
    Have you tried examining the sender parameter, that usually is the object that triggered the call.
    – James Love
    Commented Sep 9, 2012 at 9:04
  • The sender is a click event. (Of an asp button inside the webpart.) Maybe in the parent chain.
    – Wesley
    Commented Sep 10, 2012 at 1:39

1 Answer 1

1

Casting the control to its type, and then getting the ID of the BindingContainer, which is the WebPart, allows me to match that parameter to the ID of the Web Part being called.

    protected void setTwitterName_Click(object sender, EventArgs e)
    {
        string Name = TwitterHandle.Text.Trim();
        var x = (Button)sender;
        var WebPartID = x.BindingContainer.ID;

        if (!String.IsNullOrEmpty(Name))
        {
            using (var webPartManager = SPContext.Current.Web.GetLimitedWebPartManager("default.aspx",PersonalizationScope.User))
            {
                try
                {
                    foreach (var twitterWebPart in webPartManager.WebParts.Cast<WebPart>().Where(webPart => webPart.Title == "Twitter").Cast<Twitter>())
                    {
                        if(twitterWebPart.ID == WebPartID)
                        {
                            twitterWebPart.Name = Name;
                            webPartManager.SaveChanges(twitterWebPart);
                            PropertiesWebPart.Name = Name;
                            DisplayTweets();
                        }

                    }
                }
                catch
                {
                }
            }
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.