Building Web Parts with Visual Studio 2010 in C#.NET

Make QR-Code in C#.NET Building Web Parts with Visual Studio 2010

Building Web Parts with Visual Studio 2010
QR-Code Generator In C#.NET
Using Barcode printer for Visual Studio .NET Control to generate, create QR Code image in .NET applications.
www.OnBarcode.com
Recognize QR Code JIS X 0510 In C#.NET
Using Barcode reader for VS .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Figure 3.6 The Solution Explorer in Visual Studio shows you all the files and Features of your Web Part project. The Web Part files are grouped together into a SharePoint project item.
Barcode Drawer In C#
Using Barcode generator for .NET framework Control to generate, create Barcode image in .NET framework applications.
www.OnBarcode.com
Paint QR Code JIS X 0510 In Visual C#.NET
Using Barcode maker for .NET framework Control to generate, create QR Code image in .NET applications.
www.OnBarcode.com
Rename the Feature to MyWebPartFeature by right-clicking the Feature, selecting Rename, and typing a name. If you want, double-click on the Feature you just renamed and the Feature designer will open in Visual Studio, as shown in figure 3.7.
Generate Data Matrix In Visual C#.NET
Using Barcode creator for .NET Control to generate, create DataMatrix image in VS .NET applications.
www.OnBarcode.com
Code 3/9 Creator In C#.NET
Using Barcode creator for .NET framework Control to generate, create ANSI/AIM Code 39 image in VS .NET applications.
www.OnBarcode.com
Figure 3.7 Use the Feature designer in Visual Studio to edit your Features. You can change the title, description, and scope as well as configure which items should be part of the Feature.
Painting Linear 1D Barcode In C#.NET
Using Barcode drawer for VS .NET Control to generate, create Linear 1D Barcode image in .NET framework applications.
www.OnBarcode.com
Encode ISBN - 13 In C#
Using Barcode generator for Visual Studio .NET Control to generate, create ISBN - 13 image in .NET framework applications.
www.OnBarcode.com
Traditional Web Part projects
Encode QR Code ISO/IEC18004 In Java
Using Barcode printer for Android Control to generate, create Denso QR Bar Code image in Android applications.
www.OnBarcode.com
Generating QR Code ISO/IEC18004 In C#.NET
Using Barcode maker for VS .NET Control to generate, create QR-Code image in .NET framework applications.
www.OnBarcode.com
You can use the designer to give the Feature a title and description that will be shown to web interface users. The Feature designer also contains functionality for changing the scope of your Feature. A Web Part project must always have the scope set to Site, which means that it s deployed to a site collection because the Web Part Gallery exists at the site collection level. Using the Feature designer, you can also add SharePoint items to or remove them from the Feature. You d do this, for example, if you have multiple Features in your solution and you d like to have different Web Parts in the different solutions. For now, though, let s leave our Feature as is. When you ve changed the title and description, save and close the Feature designer.
Barcode Creator In .NET
Using Barcode generation for .NET framework Control to generate, create Barcode image in .NET applications.
www.OnBarcode.com
QR Code ISO/IEC18004 Recognizer In C#.NET
Using Barcode reader for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Adding controls to the Web Part The Web Part class file is automatically opened in Source Code view when you add the Web Part item to the project. Because this project item doesn t support any visual editing, you only have this view of the Web Part. The class file looks similar to the corresponding Visual Web Part class except for the code that loads the user control.
Create Barcode In None
Using Barcode printer for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
Code 128A Scanner In None
Using Barcode scanner for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
NOTE
Code 39 Creator In None
Using Barcode creator for Word Control to generate, create Code 39 Full ASCII image in Microsoft Word applications.
www.OnBarcode.com
Code 128B Generator In Objective-C
Using Barcode generator for iPad Control to generate, create Code 128A image in iPad applications.
www.OnBarcode.com
For the code samples in this book, I ve stripped out most namespace declarations and all using statements.
Recognize UPC - 13 In Visual Basic .NET
Using Barcode reader for VS .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
PDF 417 Reader In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
You have to add controls to your Web Part manually, using the CreateChildControls method (except in certain scenarios that we ll cover in later chapters). The following listing shows what our code looks like after we ve added a text box, a label, and a button.
Recognize GTIN - 13 In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Reading Data Matrix 2d Barcode In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Listing 3.2 Adding controls to the CreateChildControls method
protected TextBox textBox; protected Label label; protected Button button; protected override void CreateChildControls() { textBox = new TextBox(); label = new Label(); button = new Button(); button.Text = "Click me!"; this.Controls.Add(textBox); this.Controls.Add(new LiteralControl("<br/>")); this.Controls.Add(label); this.Controls.Add(new LiteralControl("<br/>")); this.Controls.Add(button); base.CreateChildControls(); }
Defines controls
Instantiates controls
Adds controls to control tree
First, you define the text box, label, and button controls as protected local variables B. These controls are created in the CreateChildControls method C, and then you add them to the Web Part controls collection in the order that you want your controls to appear D. Use the LiteralControl control to add the <br/> tags that you used in the Visual Web Part to write each control on a new line. This control is great for creating HTML
Building Web Parts with Visual Studio 2010
markup that doesn t need any connected control. For example, if you need to write HTML content in the Web Part, you can use the LiteralControl and build a string with HTML instead of using several ASP.NET controls. The more controls you use, the more processing is needed. In addition, the view state of the page may grow larger.
Adding the button-click event manually Because you can t take advantage of the designer and automatically hook up the control events, you have to manually add the click event to the button. The event is wired up to the button control after the control is created in CreateChildControls. Using IntelliSense in Visual Studio helps you write the code. When you type += after the Button.Click event and press the Tab key, Visual Studio writes the code for you. If you press Tab a second time, it also creates the event handler method skeleton for you. The source code row you wrote should look like this:
button.Click += new EventHandler(button_Click);
And the automatically created event handler looks like this:
void button_Click(object sender, EventArgs e) { throw new NotImplementedException(); }
Replace the exception with the assignment of the text box text to the label just as we did in the Visual Web Part example. To complete your Web Part, your event handler code should look like this:
void button_Click(object sender, EventArgs e) { label.Text = textBox.Text; }
As in the Visual Web Part example, you can change which base class the Web Part will use here but, because this sample is deployed to the sandbox, you can t use a base class that inherits from the SharePoint implementation. Doing so will result in a runtime error when you add the Web Part to a page, even though the compilation will work fine.
Testing and debugging your Web Part To run your Web Part, press F5 (just like you did with the Visual Web Part), and SharePoint 2010 will open if the compilation succeeds. Any compilation or packaging errors will be reported and stop the debugging process. Once Internet Explorer has opened the SharePoint site, create a new page using the Site Actions menu. Or, you can edit the page you used earlier by clicking the Edit Page icon, inserting the Web Part on the page, and verifying that it s working. Because you just created a sandboxed solution, this Web Part is running in the isolated sandbox process. Visual Studio deployed it to a special gallery in the site collection called the Solution Gallery. Click Site Actions > Site Settings and then click Solutions under the Galleries header to access the Solutions Gallery. This gallery
Copyright © OnBarcode.com . All rights reserved.