Saturday 10 December 2011

How To Create A Web User Control

7 Easy Steps to Create a Simple Web User Control

From First ,You Must Know What Is Web User Control


In My Words Its Nothing But Simple A Control not a page Coded and designed by You which will further use in .aspx pages,And Below Is Bit Explained Definition.


1) An ASP.NET Web user control is similar to a complete ASP.NET Web page (.aspx file), with both a user interface page and code. You create the user control in much the same way you create an ASP.NET page and then add the markup and child controls that you need. A user control can include code to manipulate its contents like a page can, including performing tasks such as data binding.


A user controls differs from an ASP.NET Web page in these ways:


•The file name extension for the user control is .ascx.


•Instead of an @ Page directive, the user control contains an @ Control directive that defines configuration and other properties.


•User controls cannot run as stand-alone files. Instead, you must add them to ASP.NET pages, as you would any control.


•The user control does not have html, body, or form elements in it. These elements must be in the hosting page.


2)  Thank You For Giving Time In Above Lines Now Add New Item To Your Project i.e Web User Control ,you will get a web user control with .ascx extention (lets says its name Example.ascx)
Below Is A Sample How Your  Web User Control Looks Like

Example.ascx

And Just Work In It As You do In Normal Asp.net pages.(add button,textbox,label whatever and write there .cs too)


3)  Then Open Any of your .aspx page(let it be Default.aspx)


4)   Now What You Require From Here Onward Is You Have To Use Your Own Made Control In Your Default.aspx Page.


Now Two Things Remain...
1st Register That Control In You Page.
2nd Call Web User Control In Your Page.  






For That You Have To Follow Below Procedure....

5)Add This Line In Head Of Your aspx page


<%@ Register Src="~/Example.ascx" TagName="TN" TagPrefix="TPRF" %>


as shown in below Image











Now Remember Few Things
Above Line means 
Register(you are registering that control in your Page)
Src(path of that control)
TagName(it could be any thing you wish to name)
TagPreffix(it could be any thing you wish to name)

6)  Now Its Time To Call Web User Control


<TPRF:TN Id="webuser1" runat="Server"/>




7)  Now Run Your default Page It Will show you the Things As You Did Above.
To Remind If Adding More Times In Single Pages give you Error You Must Need To Add That Much Register <%Register%> Line How Much You Need To Call.









1 comment:

Print Only Grid View in ASP.net

ASP.net How to Print Only GridView < div id ="gridviewDiv" >   < asp:GridView ID ="gridViewToPri...