ASP.NET

1. ASP.NET Introduction 2. Comp of .Net Framework 3.5 3. Sys.Req for VS 2008 4. ASP.NET Envrmnt Setup 5. VS2010 Ultimate Sys.Req 6. Installing VS 2010 7. VS 2012 Sys.Req 8. Installing VS Exp 2012 9. Start the VS 2008 10. Application Life Cycle 11. Page Life Cycle 12. Page Life Cycle Events 13. ASP.NET Example 14. Event Handling 15. Default Events 16. Server Side 17. Request Object 18. Response Object 19. Server Controls 20. Server Controls Properties 21. Server Controls Methods 22. HTML Server Controls 23. Client Side 24. ASP.NET Basic Controls 25. TextBox Control 26. CheckBox Controls 27. RadioButton Controls 28. ListBox Control 29. HyperLink Control 30. Image Control 31. BulletedList Control 32. ASP.NET Directives 33. Implements Directive 34. Master Type Directive 35. Page Directive 36. PreviousPage Type 37. Managing State 38. Control State 39. Cookies 40. Query Strings 41. Server-Side State 42. Session State 43. Validation Controls 44. Required FieldValidator 45. Range Validator 46. Compare Validator 47. RegularExpressionValidator 48. Custom Validator 49. Validation Summary 50. Data Source Control 51. AdRotator Control 52. Calendar Control 53. Calendar Control Example 54. Panel Control 55. Panel Control Example 56. Multi Views Control 57. MultiView & View controls 58. MultiView Control Example 59. FileUpload Control 60. FileUpload Control Eg 61. AJAX Control 62. UpdatePanel control 63. UpdateProgress Control 64. Custom Controls 65. Custom Control Eg 66. Personalization 67. Create Simple Profile 68. ADO.NET 69. ADO.NET Objects 70. DataTable 71. DataRow 72. DataColumn 73. Object Example 74. Error Handling 75. Tracing Errors 76. Debugger 77. Security 78. Authentication 79. Authorization 80. LINQ 81. LINQ Query Operators 82. LINQ Example 83. Caching 84. Data Caching 85. Output Caching 86. Object Caching 87. Web Services 88. Create Web Services Eg 89. Web & Machine.config 90. Settings Schema 91. ASP.NET Deployment 92. XCopy Deployment 93. Web Setup Project 94. ASP Interview Part 1 95. ASP Interview Part 2 96. ASP Interview Part 3 97. ASP Interview Part 4 98. ASP Interview Part 5 99. ASP Interview Part 6
Pr.Pg Next Pg

Create and use a simple custom control tutorials

  • Open Visual Studio.NET

  • Create a class library project, and give it a name, for example, CustomServerControlsLib.

  • Add a source file to the project, for example, SimpleServerControl.cs.

  • Include the reference of the System.Web namespace in the references section.

  • Check whether the following namespaces are included in the SimpleServerControl.cs file.

System

System.Collections

System.ComponentModel

System.Data

System.Web

System.Web.SessionState

System.Web.UI

System.Web.UI.WebControls

 

  • Inherit the SimpleServerControls class with the Control base class.

public class SimpleServerControl : Control

 

  • Override the Render method to write the output to the output stream.

protected override void Render(HtmlTextWriter writer)

{

writer.Write("This is custom control example");

}

Note The HtmlTextWriter class has the functionality of writing HTML to a text stream. The Write method of the HtmlTextWriter class outputs the specified text to the HTTP response stream and is the same as the Response.Write method.

  • Compile the class library project. It will generate the DLL output.

  • Open an existing or create a new ASP.NET Web application project.

  • Add a Web Forms page where the custom control can be used.

  • Add a reference to the class library in the references section of the ASP.NET project.

  • Register the custom control on the Web Forms page.

<%@ Register TagPrefix="CC " Namespace=" CustomServerControlsLib " Assembly="CustomServerControlsLib " %>

 

  • To instantiate or use the custom control on the Web Forms page, add the following line of code in the <form> tags.

<form id="Form1" method="post" runat="server">

<CC:SimpleServerControl id="ctlSimpleControl" runat="server">

</CC:SimpleServerControl >

</form>

 

  • Run the Web Forms page, and you will see the output from the custom control.

Example: Here we are going to create the file upload custom control them later use it inside a web application. The Code for ServerControl.cs is:

 

Following example shows the use of ASP.NET Custom Control:

Sam Sir

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Linq;

using System.Text;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Configuration;

using System.IO;

 

namespace ServerControl1

{

[DefaultProperty("Text"), ToolboxData("<{0}:AdFup runat=\"server\"> </{0}:AdFup>")]

public class AdFup : CompositeControl, INamingContainer

{

protected Label lbl;

protected FileUpload fu;

public AdFup()

{

}

[Bindable(true), Category("Miscellaneous"), DefaultValue(""), Localizable(true)]

public string LText

{

get { EnsureChildControls(); return lbl.Text; }

set { EnsureChildControls(); lbl.Text = value; }

}

[Bindable(true), Category("Miscellaneous"), DefaultValue(""), Localizable(true)]

public string FName

{

get

{

string s = (string)ViewState["FName"];

if (s == null)

{

return string.Empty;

}

else

{

return s;

}

}

set { ViewState["FName"] = value; }

}

[Bindable(true), Category("Miscellaneous"), DefaultValue(""), Localizable(true)]

public string FPath

{

get

{

string s = (string)ViewState["FPath"];

if (s == null)

{

return string.Empty;

}

else

{

return s;

}

}

set { ViewState["FPath"] = value; }

}

[Bindable(true), Category("Miscellaneous"), DefaultValue(""), Localizable(true)]

public string FCheck

{

get

{

string s = (string)ViewState["FCheck"];

if (s == null)

{

return string.Empty;

}

else

{

return s;

}

}

set { ViewState["FCheck"] = value; }

}

[Bindable(true), Category("Miscellaneous"), DefaultValue(""), Localizable(true)]

public string Ext

{

get

{

string s = (string)ViewState["Ext"];

if (s == null)

{

return string.Empty;

}

else

{

return s;

}

}

set { ViewState["Ext"] = value; }

}

public AdFup(string labelText, string fileName, string filePath, string extension)

{

LText = labelText;

FName = fileName;

FPath = filePath;

Ext = extension;

}

protected override void CreateChildControls()

{

Controls.Clear();

lbl = new Label();

Controls.Add(lbl);

fu = new FileUpload();

Controls.Add(fu);

fu.BackColor = System.Drawing.Color.Chocolate;

}

}

}

 

 

 

  • Now Build the ServerControl1 Project. And add a new website, now add the Existing project which we have already created (ServerControl1).Now Browse the dll file right click on the ToolBox. You will see the New Custom File Upload control (AdFup) will display in the Tool Box.

 

asp.net custom control example

 

  • Now Drag the AdFup from the toolbox on the pages. The code are such as:

 

 

Following example shows the use of ASP.NET Custom Control:

Sam Sir

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="ServerControl1" Namespace="ServerControl1" TagPrefix="FUPLD"%> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>cbtSAM ASP.NET Custom Control Example</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<FUPLD:AdFup ID="AdFup1" runat="server" />

</div>

</form>

</body>

</html>

Output

 

 

asp.net custom control example

 

Pr.Pg border                                              Next Pg