自定义控件无法触发click事件?

zhoudahaiwojiushi 2011-04-30 01:47:38
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ServerControl1
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")]
public class ServerControl1 : WebControl,INamingContainer,IPostBackEventHandler
{
TextBox username;
TextBox password;
Button btnlogin;
CheckBox cbxRemember;
Table t;

/// <summary>
/// 创建控件的方法
/// </summary>
protected override void OnInit(EventArgs e)
{
btnlogin = new Button();
btnlogin.ID = "btn1";
btnlogin.Text = "登陆";
btnlogin.Click += new EventHandler(btnlogin_Click);
base.OnInit(e);
}

protected override void CreateChildControls()
{


base.CreateChildControls();
t = new Table();
TableRow tr1 = new TableRow();
TableCell cell11 = new TableCell();
TableCell cell12 = new TableCell();
tr1.Controls.Add(cell11);
tr1.Controls.Add(cell12);
cell11.Text = "用户名";
username = new TextBox();
cell12.Controls.Add(username);
TableRow tr2 = new TableRow();
TableCell cell21 = new TableCell();
TableCell cell22 = new TableCell();
cell21.Text = "密码:";
tr2.Controls.Add(cell21);
password = new TextBox();
cell22.Controls.Add(password);
tr2.Controls.Add(cell22);

TableRow tr3 = new TableRow();
TableCell cell31 = new TableCell();
TableCell cell32 = new TableCell();
cbxRemember = new CheckBox();
cbxRemember.Text = "记住cookies";
cell31.Controls.Add(cbxRemember);
tr3.Controls.Add(cell31);


cell32.Controls.Add(btnlogin);
tr3.Controls.Add(cell32);
t.Controls.Add(tr1);
t.Controls.Add(tr2);
t.Controls.Add(tr3);
}

void btnlogin_Click(object sender, EventArgs e)///此位置无法触发
{

this.onLogin(EventArgs.Empty);
}

public string UserName
{
get
{
return this.username.Text;
}
set
{
this.username.Text = value;
}
}
public string PassWord
{
get
{
return this.password.Text;
}
set
{
this.password.Text = value;
}
}

public event EventHandler Login;

//触发事件的方法
protected void onLogin(EventArgs e)
{
if (this.Login != null)
{
this.Login(this, e);
}
}

//protected override void EnsureChildControls()
//{
// if (!ChildControlsCreated)
// {
// this.BuildControls();
// this.ChildControlsCreated = true;
// }

// base.EnsureChildControls();

//}
protected override void RenderContents(HtmlTextWriter output)
{
//this.EnsureChildControls();
t.RenderControl(output);
}

#region IPostBackEventHandler 成员

public void RaisePostBackEvent(string eventArgument)
{
Login(this, EventArgs.Empty);
}

#endregion
}
}
...全文
165 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
子夜__ 2011-04-30
  • 打赏
  • 举报
回复
zhoudahaiwojiushi 2011-04-30
  • 打赏
  • 举报
回复
运行期不就点击登录按钮了吗?点击不就触发Click事件了吗?click事件触发Login事件不就触发了吗?
我现在不触发click
zhoudahaiwojiushi 2011-04-30
  • 打赏
  • 举报
回复
运行期不就点击登录按钮了吗?点击不就触发Click事件了吗?click事件一出发Load事件不就触发了吗?
我现在不触发click
ycproc 2011-04-30
  • 打赏
  • 举报
回复
你没有 委托事件

既然没有自定义的实力

还是拉上去的控件 给 先做好了

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧