concept button in category sharePoint

appears as: button, buttons, buttons, button, The button
SharePoint 2010 Web Parts in Action

This is an excerpt from Manning's book SharePoint 2010 Web Parts in Action.

  • Create a Web Part— This Web Part will display the button and handle the click event for you.
  • Subclass the site page— Your new class will have the code that handles the click event and the site page ASPX code will define the button.
  • The UI is built using controls. A control can be a text label, a button, or something more complex, such as a data grid. Controls can also be combined to create composite controls. These controls can be ASP.NET, SharePoint, or custom controls. Some controls produce the HTML that’s used by the browser, and some only act on the server side. This chapter will start by exploring the ASP.NET basics and showing how you can utilize the ASP.NET controls and features in your Web Part. Remember that Web Parts are defined in ASP.NET and aren’t specific to SharePoint.

    Then you need to add a button and a label to this Visual Web Part. When you’ve added the button, go to the Design mode of the control and double-click the button. The double-click will automatically generate the click event handler in the user control and its code-behind. The code for the label and the button will look like this:

    <asp:Button ID="Button1" runat="server" Text="Add"
        onclick="Button1_Click" />
    <br />
    <asp:Label ID="Label1" runat="server" />

    In the code-behind for the Visual Web Part control, edit the button click event handler so that it adds the values from the two custom user controls. You need to cast the RequiredNumberField objects into the correct class because Visual Studio has added these controls as UserControl objects:

    Label1.Text = (((RequiredNumberField)RequiredNumberField1).Value
        + ((RequiredNumberField)RequiredNumberField2).Value).ToString();

    Finally, add code to the CreateChildControls method so that the validation group is set on the custom controls and the button:

    To demonstrate how you can solve persisting the state of dynamically added controls, look at listing 4.1, which contains a simple Web Part with a button and a counter variable. The counter is incremented on every click and shown in the Web Part. This sample will also illustrate when to use the OnPreRender method to add controls.

    Listing 4.1. A Web Part with view state

    counter is a local variable and initialized to 0 every time the class is loaded. In the CreateChildControls method, you add a button that increments the value of counter. If you added a control that printed the value in the CreateChildControls, you wouldn’t see the changed value. The postback event is fired after the CreateChildControls event, as seen in figure 4.2. To see the changed variable value, you can take advantage of the OnPreRender method and add a LiteralControl with the counter value . If you test this Web Part, you’ll see that counter is incremented but only once, to the value of 1. This is because the variable is initialized to 0 every time the class is created.