Control definido por el usuario
MyControl.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyControl.ascx.cs" Inherits="test5_13.MyControl" %>
当前时间为:<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
MyControl.ascx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace test5_13
{
public partial class MyControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
var now = DateTime.Now;
var years = now.Year;
var months = now.Month;
var dates = now.Day;
var day = now.DayOfWeek;
var hour = now.Hour;
var min = now.Minute;
var ss = now.Second;
var week = "";
if (day.ToString() == "Monday")
{
week = "星期一";
}
if (day.ToString() == "Tuesday")
{
week = "星期二";
}
if (day.ToString() == "Wednesday")
{
week = "星期三";
}
if (day.ToString() == "Thursday")
{
week = "星期四";
}
if (day.ToString() == "Friday")
{
week = "星期五";
}
if (day.ToString() == "Staturday")
{
week = "星期六";
}
if (day.ToString() == "Sunday")
{
week = "星期日";
}
var today = + years + "-" + months + "-" + dates + " "
+ hour + ":" + min + ":" + ss + " " + week;
Label1.Text = today;
}
}
}
webform1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="test5_13.WebForm1" %>
<%@ Register Src="~/MyControl.ascx" TagPrefix="uc1" TagName="MyControl" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:MyControl runat="server" id="MyControl1" />
</div>
</form>
</body>
</html>