您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“如何給自定義Web控件添加事件”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“如何給自定義Web控件添加事件”這篇文章吧。
------------myRegister1.ascx前臺(tái)代碼-----------
代碼如下:
<script src="js/Jquery1.7.js" type="text/javascript"></script>
<script type="text/javascript">
function Register() {
if ($('#myRegister1_txtUserName').val() == '') {
$('#spanUserName').text('請(qǐng)輸入用戶(hù)名');
return false;
}
if ($('#myRegister1_txtPwd').val() == '') {
$('#spanPwd').text('請(qǐng)輸入密碼');
return false;
}
if ($('#myRegister1_txtPwd1').val() == '') {
$('#spanPwd1').text('請(qǐng)輸入密碼');
return false;
}
if ($('#myRegister1_txtPwd1').val() != $('#myRegister1_txtPwd').val()) {
$('#spanPwd1').text('兩次密碼要一致');
return false;
}
if ($('#myRegister1_txtEmail').val() == '') {
$('#spanEmail').text('請(qǐng)輸入郵箱');
return false;
}
return true;
}
</script>
<table>
<tr>
<td>用戶(hù)名:</td>
<td>
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox></td>
<td><span id="spanUserName"></span></td>
</tr>
<tr>
<td>密碼:</td>
<td>
<asp:TextBox ID="txtPwd" runat="server"></asp:TextBox></td>
<td><span id="spanPwd"></span></td>
</tr>
<tr>
<td>確認(rèn)密碼:</td>
<td>
<asp:TextBox ID="txtPwd1" runat="server"></asp:TextBox></td>
<td><span id="spanPwd1"></span></td>
</tr>
<tr>
<td>郵箱:</td>
<td>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
<td><span id="spanEamil"></span></td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnRegister" runat="server" Text="注冊(cè)"
onclick="btnRegister_Click" OnClientClick="return Register()" /></td>
<td>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</td>
</tr>
</table>
------------myRegister1.ascx后臺(tái)代碼------------
復(fù)制代碼 代碼如下:
public partial class myRegister : System.Web.UI.UserControl
{
public event MyRegeitserDelegate On_MyRegister;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnRegister_Click(object sender, EventArgs e)
{
if (On_MyRegister != null)
{
MyRegister.userName=txtUserName.Text;
MyRegister.pwd=txtPwd.Text;
MyRegister.email = txtEmail.Text;
On_MyRegister();
if (MyRegister.isBool == true)
{
Label1.Text = "注冊(cè)成功";
}
else
{ Label1.Text = "注冊(cè)失敗"; }
}
}
}
public class MyRegister
{
public static string userName { set; get; }
public static string pwd { set; get; }
public static string email { set; get; }
public static bool isBool { set; get; }
}
public delegate void MyRegeitserDelegate();
---------WebForm事件無(wú)參.aspx前臺(tái)代碼--------------
復(fù)制代碼 代碼如下:
<uc1:myRegister ID="myRegister1" runat="server" />
---------WebForm事件無(wú)參.aspx后臺(tái)代碼---------------
復(fù)制代碼 代碼如下:
public partial class WebForm事件無(wú)參 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.myRegister1.On_MyRegister += new MyRegeitserDelegate(myRegister1_On_MyRegister);
}
void myRegister1_On_MyRegister()
{
MyRegister.isBool = writer(MyRegister.userName, MyRegister.pwd, MyRegister.email);
}
private bool writer(string userName, string pwd, string email)
{
using (Stream stream = File.Open(@"E:\new.txt", FileMode.Append, FileAccess.Write))
{
using (StreamWriter writer = new StreamWriter(stream))
{
writer.WriteLine("用戶(hù)名:{0},密碼:{1},郵箱:{2}",userName,pwd,email);
}
}
return true;
}
}
以上是“如何給自定義Web控件添加事件”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。