Question

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
            {
            }
        }
    }
}
Was it helpful?

Solution

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
                {
                }
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top