<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
WebUserControl.ascx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class WebUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class WebUserControl : System.Web.UI.UserControl
{
public event EventHandler Click;
protected void Page_Load(object sender, EventArgs e)
{
Submit.Click += new EventHandler(Submit_Click);
}
protected void Submit_Click(object sender, EventArgs e)
{
Click(this, e);
}
public string Title
{
get { return TitleLabel.Text; }
set { TitleLabel.Text = value; }
}
public string UserName
{
get { return UserNameText.Text; }
set { UserNameText.Text = value; }
}
public string Password
{
get { return PasswordText.Text; }
set { PasswordText.Text = value; }
}
}
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ASP.webusercontrol_ascx Login1 = Page.LoadControl("~/WebUserControl.ascx");
Page.Form.Controls.Add(Login1);
}
}
ย้ำว่าเลือกเอาอย่างใดอย่างหนึ่ง
5. จัดการ Event เมื่อกดปุ่ม Login
Default.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ASP.webusercontrol_ascx Login1 = Page.LoadControl("~/WebUserControl.ascx");
Page.Form.Controls.Add(Login1);
Login1.Click += new EventHandler(Login1_Click);
}
protected void Login1_Click(object sender, EventArgs e)
{
string uName = Login1.UserName;
string pWord = Login1.Password;
//จัดการตามความต้องการ
}
}