Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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
            {
            }
        }
    }
}
share|improve this question
1  
Have you tried examining the sender parameter, that usually is the object that triggered the call. –  James Love Sep 9 '12 at 9:04
    
The sender is a click event. (Of an asp button inside the webpart.) Maybe in the parent chain. –  Wesley Sep 10 '12 at 1:39
add comment

1 Answer 1

up vote 0 down vote accepted

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
                {
                }
            }
        }
    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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