|
Page 2 of 2
.
.
To create a component in the Application_Code folder
1. In Solution Explorer, right-click the Application_Code folder, and then click Add New Item.
2. Under Visual Studio installed templates, choose Class.

3. Name the class ExForSysClass1.
4. From the Language list, select C#.
5. Click Add.
Visual Web Developer opens the new class file in the editor.
1. Create a class that has a single property named testString.
public class ExForSysClass1
{
..........public ExForSysClass1() {}
..........private string testStringValue;
..........public string testString
..........{
....................get
....................{
..............................return testStringValue;
....................}
....................set
....................{
..............................testStringValue = value;
....................}
..........}
}
2. Save the file and close it.
Using the Component
1. Create a file called ExForSys.aspx
2. From the Standard folder of the toolbox, drag a TextBox control, Label control, and Button control onto the page.
3. Double-click the button to create a Click handler it.
4. In the handler, type in the code just below the page directive.
< script runat="server" language=C# >
..........void Button1_Click(object sender, EventArgs e)
..........{
....................ExForSysClass1 Test = new ExForSysClass1();
..............................Test.testString=TextBox1.Text;
........................................Label1.Text=Test.testString;
..........}
< / script >
5. When you enter a space after New or new, Visual Web Developer displays a list of available classes. The class you created earlier, ExForSysClass1, appears in the drop-down list.

Testing the Page and Component
1. Press CTRL+F5 to run the page.
2. When the page appears in the browser, enter something into the text box and click the button. Doing so sets a property in your simple class, which is then displayed in the Label control.

Navigate to Windows Explorer and examine the directory in which the Website is located. Note that the page App_code directory and the .aspx files appear in the website folder. Also note that the .dll file is not present in the directory. ASP.NET has compiled the page and the component dynamically.
Trackback(0)

|