locked
consume custom UserControl from Visual Web Part RRS feed

  • Question

  • User1853245500 posted

    I have created a Visual Web Part for SharePoint 2010 called vwpResultsPlan.  I override the OnInit function of the vwpResultsPlanUserControl and dynamically add a UserControl (created within my SharePoint project) called ucPlanDetails to some TabPanels of a TabContainer.  Up to this point this all works fine and the control gets added where it should be.  The problem is that there is a save button in ucPlanDetails that when clicked does not fire the button clicked event.

    Since the control is added dynamically I'm not sure how to handle the button click event from ucPlanDetails in the parent Visual Web Part vwpResultsPlan.  When the button is cliked now, vwpResultsPlan posts back and reloads maintaining any data entered in ucPlandetails (which is good).  I just can't figure out how to capture the button click event on ucPlanDetails.  

    I figure you have to add an event to the User Control, then call that event on the button click, then handle that event in the Visual Web Part, but with the control loaded dynamically I can't see how to create an event handler for my custom event in the Visual Web Part code.

    Any input would be greatly appreciated.  I can provide code if need be.

    Thanks,
    Justin 

    Wednesday, December 28, 2011 4:29 PM

All replies

  • User3866881 posted

    I figure you have to add an event to the User Control, then call that event on the button click, then handle that event in the Visual Web Part, but with the control loaded dynamically I can't see how to create an event handler for my custom event in the Visual Web Part code.

    Hello:)

    Please have a try something like this:

    YourControl class c = new YourControl Class();

    this.Controls.Add(c);

    c.XXXEvent+=……;

    Correct me if I'm wrong,not an expert in SharePoint but still want you lend you a hand……

    Best regaurds!

    Thursday, December 29, 2011 8:51 PM
  • User1853245500 posted

    Ok I don't know what is going on here.  Just for trouble-shooting I added the control to the tab container in my visual web part in the designer instead of doing it dynamically which I do need to do in the long run.  The control shows up on the vwp fine.  The visual web part has a button on it to refresh some data.  Now with the control on the page the button causes a generic Sharepoint error.  If I step through the code the error page comes up right after the Page_Load event of the vwp is done.  Using fiddler is looks like there is a 401 unauthorized error happening on the postback which sharepoint is not displaying for some reason.  This error does not appear if I don't have my custom user control on the page and it works as designed.  Also if I add my user control to a regular aspx page and do the exact same process it works fine, so it seems like an issue with the vwp.  Anyone have any idea why this would happen when tryinh to postback a visual web part with a user control inside it?

    Justin

    Friday, December 30, 2011 10:29 AM