新建一个Asp.Net空网站;加入一个Default页;
添加一个web用户控件;
解决方案结构如下;
用户控件页面添加一个Label;
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<asp:Label ID="Label1" runat="server" Height="30px" Width="200px"></asp:Label>
WebUserControl.ascx.cs加入2个方法;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class WebUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void MyAdd(int a, int b)
{
Label1.Text = "和:" + Convert.ToString(a + b);
}
public void MySub(int a, int b)
{
Label1.Text = "差:" + Convert.ToString(a - b);
}
}
Default页面加入用户控件,
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>
<!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>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>+
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="相加" /><br />
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> -<asp:TextBox ID="TextBox4"
runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="相减" OnClick="Button2_Click" />
<br />
<uc1:WebUserControl id="WebUserControl1" runat="server">
</uc1:WebUserControl>
</div>
</form>
</body>
</html>
Default页面cs文件调用用户控件方法;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
WebUserControl1.MyAdd(Convert.ToInt16(TextBox1.Text), Convert.ToInt16(TextBox2.Text));
}
protected void Button2_Click(object sender, EventArgs e)
{
WebUserControl1.MySub(Convert.ToInt16(TextBox3.Text), Convert.ToInt16(TextBox4.Text));
}
}
执行情况如下;
下面来进一步;
在用户控件增加三个方法,分别获取随机数、获取1-100之间随机数、获取GUID;结果都放到Label1;
public void MyRand()
{
Random rd = new Random();
int i = rd.Next();
Label1.Text = i.ToString();
}
public void MyRand100()
{
Random rd = new Random();
int i = rd.Next()%100+1;
Label1.Text = i.ToString();
}
public void MyGuid()
{
Label1.Text=Guid.NewGuid().ToString();
}
Default.aspx加入三个按钮;
<asp:Button ID="Button3" runat="server" Text="获取随机数" OnClick="Button3_Click" />
<asp:Button ID="Button4" runat="server" Text="获取1-100之间随机数" OnClick="Button4_Click" />
<asp:Button ID="Button5" runat="server" Text="获取GUID" OnClick="Button5_Click" />
Default.aspx.cs调用用户控件方法;
protected void Button3_Click(object sender, EventArgs e)
{
WebUserControl1.MyRand();
}
protected void Button4_Click(object sender, EventArgs e)
{
WebUserControl1.MyRand100();
}
protected void Button5_Click(object sender, EventArgs e)
{
WebUserControl1.MyGuid();
}
运行情况如下;