kamagra how much to take

Open Configuration Panel From WebPart ToolPane

Yeah, the title doesn’t make a ton of sense, but what I am about to introduce will once we get going. In this post, I will demonstrate how you can call a settings process from the WebPart ToolPane (using an event handler) to collect a bunch of user information before it is actually submitted to a WebPart. It is helpful if you are developing complex WebParts that before the WebPart can actually do any labor it has to get some information / configuration information from the user in order to really accomplish what it is built for.

Ready? Here we go….

The first thing to do is to call a new ToolPart derived class that will house the settings link that will trip the entire information gathering process, basically to invoke the settings logic that should be called in order to harvest and process the relevant user entered data. This class will inherit from the Microsoft.SharePoint.WebPartPages.ToolPart base class, so you have to make sure that you are referencing the Microsoft.SharePoint binary and have a using statement in place for Microsoft.SharePoint.WebPartPages.

  1. using System;
  2. using System.Web.UI;
  3. using System.Web.UI.WebControls;
  4. using Microsoft.SharePoint.WebPartPages;
  5.  
  6. namespace buenz.WebParts
  7. {
  8. public class myToolPart : ToolPart
  9. {
  10.  
  11. }
  12.  
  13. }

We have seen this before in other WebParts, nothing real fancy here.

Moving along, we are going to declare two variables for use within the class. One is for a reference to the WebPart for which this ToolPart class is targeted (and should therefore match your WebPart class name) and the other is for the button that when clicked will start the settings collection process. We will see why we have to reference the WebPart class in a little bit.

  1. using System;
  2. using System.Web.UI;
  3. using System.Web.UI.WebControls;
  4. using Microsoft.SharePoint.WebPartPages;
  5.  
  6. namespace buenz.WebParts
  7. {
  8. public class myToolPart : ToolPart
  9. {
  10.  
  11. private myWebPart housedWebPart;
  12. private LinkButton myLink = new LinkButton();
  13.  
  14. }
  15.  
  16. }

Now we have the two fields that we need. Next, we have to put together the ToolPart class constructor in order to initialize some of the ToolPart properties in order to control the ToolPart behavior. Because we are accessing members of the Microsoft.SharePoint.WebPartPages.ToolPart base class from within our custom ToolPart derived class, we are going to use the base keyword. There are various properties that you can set in the constructor, however we are only going to set three, one specifying whether the ToolPart can be minimized, one specifying the Title property of our custom ToolPart, and another that specifies the styles of the custom ToolPart by setting the UseDefaultStyles property to true . Now your class should look like this:

  1. using System;
  2. using System.Web.UI;
  3. using System.Web.UI.WebControls;
  4. using Microsoft.SharePoint.WebPartPages;
  5.  
  6. namespace buenz.WebParts
  7. {
  8. public class myToolPart : ToolPart
  9. {
  10. private myWebPart housedWebPart;
  11. private LinkButton myLink = new LinkButton();
  12.  
  13. public myToolPart()
  14. {
  15. base.AllowMinimize = true;
  16. base.Title = "Example Dynamic ToolPart";
  17. base.UseDefaultStyles = true;
  18. }
  19.  
  20. }
  21.  
  22. }

We have declared, in regards to the variables, some controls that should be rendered within this ToolPart, our main concern being the ASP.NET 2.0 link button control, and in order to activate the behavior, we also need an event handler to be wired up. In order to get these controls to render, we are simply going to override the CreateChildControls() method. To append the control, we are going to simply use the Controls.Add method and then pass in the link button variable name so it is added to the control collection that will be stapled. This is no different than any way you would otherwise append a control when building an ASP.NET 2.0 WebPart (or an ASP.NET 2.0 composite control).

  1. using System;
  2. using System.Web.UI;
  3. using System.Web.UI.WebControls;
  4. using Microsoft.SharePoint.WebPartPages;
  5.  
  6. namespace buenz.WebParts
  7. {
  8. public class myToolPart : ToolPart
  9. {
  10. private myWebPart housedWebPart;
  11. private LinkButton myLink = new LinkButton();
  12.  
  13. public myToolPart()
  14. {
  15. base.AllowMinimize = true;
  16. base.Title = "Example Dynamic ToolPart";
  17. base.UseDefaultStyles = true;
  18. }
  19.  
  20. protected override void CreateChildControls()
  21. {
  22. this.housedWebPart = ((myWebPart)base.ParentToolPane.SelectedWebPart);
  23. this.myLink.Text = "Open Clicked Event Handler";
  24. this.myLink.Click += new EventHandler(this.myLink_Click);
  25. this.Controls.Add(this.myLink);
  26. }
  27.  
  28. }
  29.  
  30. }

Now we have to add the event handler code for when a user actually clicks the settings link. In the event handler, we are going to use a Session object in order to interrogate the session of the page. Based on the value harvested from the session object, we can then use a Page.Response.Redirect in order to push the user to a certain page based on retrieved values.

  1. using System;
  2. using System.Web.UI;
  3. using System.Web.UI.WebControls;
  4. using Microsoft.SharePoint.WebPartPages;
  5.  
  6. namespace buenz.WebParts
  7. {
  8. public class myToolPart : ToolPart
  9. {
  10. private myWebPart housedWebPart;
  11. private LinkButton myLink = new LinkButton();
  12.  
  13. public myToolPart()
  14. {
  15. base.AllowMinimize = true;
  16. base.Title = "Example Dynamic ToolPart";
  17. base.UseDefaultStyles = true;
  18. }
  19.  
  20. protected override void CreateChildControls()
  21. {
  22. this.housedWebPart = ((myWebPart)base.ParentToolPane.SelectedWebPart);
  23. this.myLink.Text = "Open Clicked Event Handler";
  24. this.myLink.Click += new EventHandler(this.myLink_Click);
  25. this.Controls.Add(this.myLink);
  26. }
  27.  
  28. private void myLink_Click(object sender, EventArgs e)
  29. {
  30. try
  31. {
  32. this.housedWebPart.Page.Session["myWebPart" + this.housedWebPart.ID] = "Let's_Do_Stuff";
  33. this.housedWebPart.Page.Response.Redirect(this.housedWebPart.Page.Request.Url.ToString());
  34. }
  35. catch (Exception exception)
  36. {
  37. }
  38. }
  39. }
  40. }

Now that you have your fly event handler written, all you have to do is override the RenderToolPart to the output parameter specified. You final ToolPart class will look like this:

  1. using System;
  2. using System.Web.UI;
  3. using System.Web.UI.WebControls;
  4. using Microsoft.SharePoint.WebPartPages;
  5.  
  6. namespace buenz.WebParts
  7. {
  8. public class myToolPart : ToolPart
  9. {
  10. private myWebPart housedWebPart;
  11. private LinkButton myLink = new LinkButton();
  12.  
  13. public myToolPart()
  14. {
  15. base.AllowMinimize = true;
  16. base.Title = "Example Dynamic ToolPart";
  17. base.UseDefaultStyles = true;
  18. }
  19.  
  20. protected override void CreateChildControls()
  21. {
  22. this.housedWebPart = ((myWebPart)base.ParentToolPane.SelectedWebPart);
  23. this.myLink.Text = "Open Clicked Event Handler";
  24. this.myLink.Click += new EventHandler(this.myLink_Click);
  25. this.Controls.Add(this.myLink);
  26. }
  27.  
  28. protected override void RenderToolPart(HtmlTextWriter output)
  29. {
  30. this.EnsureChildControls();
  31. this.myLink.RenderControl(output);
  32. }
  33.  
  34. private void myLink_Click(object sender, EventArgs e)
  35. {
  36. try
  37. {
  38. this.housedWebPart.Page.Session["myWebPart" + this.housedWebPart.ID] = "Let's_Do_Stuff";
  39. this.housedWebPart.Page.Response.Redirect(this.housedWebPart.Page.Request.Url.ToString());
  40. }
  41. catch (Exception exception)
  42. {
  43. }
  44. }
  45. }
  46. }

Nice. Now, onto doing stuff with it with the WebPart class.
Firstly, you have to ensure that your WebPart is going to call the custom ToolPart class you just spent oh so long writing. You can do this by returning an array of ToolPart objects by overriding the GetToolParts method of the WebPart base class, like this:

  1. public override ToolPart[] GetToolParts()
  2. {
  3. ToolPart[] partArray = new ToolPart[2];
  4. myToolPart part = new myToolPart();
  5. partArray[0] = part;
  6. return partArray;
  7. }

This is all stuff that we have explored in previous posts.

We saw before that in the ToolPart we are doing a little bit of session interrogation, specifically, the myWebPart.Page.Session object since we are concerned with the session on the page, and moving through some steps to collect use information based on the value housed within that session object.

This means you can do some fun investigation, and act upon the results that are queried from that investigation. So, you can do something like test using a string and a call to the WebPart ID:

  1. try
  2. {
  3. this.Page.Session["myWebPart" + this.ID] = "Let's_Do_Stuff";
  4. this.testingForBoolean = true;
  5. }
  6.  
  7. catch (Exception exception)
  8. {
  9. ....
  10. }

and then based on the results of this test, call all sorts of methods.

  1. if (this.testingForBoolean)
  2. {
  3. this.myMethod();
  4. this.Page.Response.Redirect(this.Page.Request.Url.ToString());
  5. }

Just a small example, however powerful nonetheless when used appropriately and in the right manner.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>