articles

Home / DeveloperSection / Articles / Creating User Define Control in ASP .Net

Creating User Define Control in ASP .Net

Anurag Chaurasia11470 16-Feb-2011
Step:
  •   Open a Blank Default.aspx page from visual studio 2005.
  •   Go to the solution explorer and right click on to the Default.aspx and then click on the Add New Item.
  •   After that you have to select the Web User Control.ascx file from add new item dialog box.
  •   That page will be the same like as default asp page draw your user control on that page.
  •   Code on the User Control.ascx page:
Code:
 <%@ControlLanguage="C#"AutoEventWireup="true"CodeFile="WebUserControl.ascx.cs"Inherits="WebUserControl"%>
<tablestyle="width: 330px">
    <tr>
        <tdstyle="height: 148px"align="center">
            <asp:LabelID="Label1"runat="server"Text="Thanks To awadh Sir"Font-Bold="True"Font-Size="XX-Large"ForeColor="Red"Visible="False"></asp:Label></td>
    </tr>
    <tr>
        <tdstyle="height: 55px"align="center">
            <asp:ButtonID="Button1"runat="server"Text="Button"OnClick="Button1_Click"/></td>
    </tr>
</table>
Design View of User Control:

Creating User Define Control in ASP .Net

After making your user control you have to go on the Source view of Design Page and add that code in the start of your page.

Code:
 <%@PageLanguage="C#"AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default"%>
<%@RegisterSrc="~/WebUserControl.ascx"TagPrefix="shw"TagName="ShowData"%>
 
<!-- This is how use to declare and use a user control on your web page -->
 
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<htmlxmlns="http://www.w3.org/1999/xhtml">
<!—You can use that control by using that code -- >
<headrunat="server">
    <title>Untitled Page</title>
</head>
<body>
    <formid="form1"runat="server">
    <div>
    <shw:ShowDatarunat="server"  ID="sh1"/>
    </div>
    </form>
</body>
</html>

After that when you will add and click on to the button there will be a text which will diplay on your web form……

   


Updated 04-Mar-2020

Leave Comment

Comments

Liked By