您好,登录后才能下订单哦!
这篇文章主要为大家展示了“如何给自定义Web控件添加事件”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“如何给自定义Web控件添加事件”这篇文章吧。
------------myRegister1.ascx前台代码-----------
代码如下:
<script src="js/Jquery1.7.js" type="text/javascript"></script>
<script type="text/javascript">
function Register() {
if ($('#myRegister1_txtUserName').val() == '') {
$('#spanUserName').text('请输入用户名');
return false;
}
if ($('#myRegister1_txtPwd').val() == '') {
$('#spanPwd').text('请输入密码');
return false;
}
if ($('#myRegister1_txtPwd1').val() == '') {
$('#spanPwd1').text('请输入密码');
return false;
}
if ($('#myRegister1_txtPwd1').val() != $('#myRegister1_txtPwd').val()) {
$('#spanPwd1').text('两次密码要一致');
return false;
}
if ($('#myRegister1_txtEmail').val() == '') {
$('#spanEmail').text('请输入邮箱');
return false;
}
return true;
}
</script>
<table>
<tr>
<td>用户名:</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>确认密码:</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="注册"
onclick="btnRegister_Click" OnClientClick="return Register()" /></td>
<td>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</td>
</tr>
</table>
------------myRegister1.ascx后台代码------------
复制代码 代码如下:
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 = "注册成功";
}
else
{ Label1.Text = "注册失败"; }
}
}
}
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事件无参.aspx前台代码--------------
复制代码 代码如下:
<uc1:myRegister ID="myRegister1" runat="server" />
---------WebForm事件无参.aspx后台代码---------------
复制代码 代码如下:
public partial class WebForm事件无参 : 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("用户名:{0},密码:{1},邮箱:{2}",userName,pwd,email);
}
}
return true;
}
}
以上是“如何给自定义Web控件添加事件”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。