Infolink

 

Search This Blog

Oct 14, 2012

User Control Example

In this example I show that how easily we can create User Control and then use it in asp.net. Here first I create a User Control name CalendarUserControl.ascx, then place it Default.aspx web form. Here is the source code of both files.


<%@ Control Language="C#" ClassName="CalendarUserControl" %> 
 
<script runat="server"> 
    protected void Calendar1_SelectionChanged(object sender, System.EventArgs e) { 
        Label1.Text = "Your selected Date is: " + 
            Calendar1.SelectedDate.ToShortDateString(); 
    } 
</script> 
<h2>This is user control</h2> 
<asp:Label ID="Label1" runat="server" Font-Size="Large" ForeColor="DarkGreen"></asp:Label> 
<br /><br /> 
<asp:Calendar ID="Calendar1" runat="server" BackColor="Beige" OnSelectionChanged="Calendar1_SelectionChanged"> 
</asp:Calendar>

<%@ Page Language="C#" %> 
 
<%@ Register src="CalendarUserControl.ascx" tagname="CalendarUserControl" tagprefix="uc1" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<script runat="server"> 
 
</script> 
 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title>User Control Example: how to create and use User Control in asp.net</title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
        <h1 style="color:Red;">This is page top</h1> 
        <hr /> 
        <uc1:CalendarUserControl ID="CalendarUserControl1" runat="server" /> 
     
    </div> 
    </form> 
</body> 
</html>

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...