关于服务器控件的自定义事件
要需要动态的根据数据库的数据来生成imagebutton控件,生成的控件在页面上面点击的时候,要求生成的事件写在页面上。(因为我一个按钮可能因为页面的不同,产生完全不同的事件)
举例:我从数据库读取两个按钮:添加、修改;在一个页面上面点击添加执行添加人员的代码,在另一个页面上面点击添加执行添加课程的代码,怎么样实现阿?
更高的要求:怎么样可以写成象button这样的控件阿,直接双击控件的某一个事件就可以产生事件,写入自己的代码;
不知道说清楚了没有,考虑好久,只知道是委托,怎么实现还是不清楚,最好写有事例代码
------解决方案--------------------控件中声明事件:
public event System.EventHandler btnOKClick;
然后在控件的按纽事件中触自定义的事件.
在控件的render中
" <img onclick=\ " " + this.Page.GetPostBackClientEvent(this, "123 ") + "\ " > ";
控件实现IPostBackEventHandler接口
然后
#region IPostBackEventHandler 成员
public void RaisePostBackEvent(string eventArgument)
{
if (eventArgument == "123 ")
{
this.btnOKClick.Invoke(sender, e);
}
}
#endregion
调用的时候.
页面中
this.Myc1.btnOKClick += new EventHandler(Myc1_btnOKClick);
void Myc1_btnOKClick(object sender, EventArgs e)
{
Response.Write( "SSS ");
}
------解决方案--------------------NSToolBar.ascx的代码:
<%@ Control Language= "C# " AutoEventWireup= "true " CodeFile= "NSToolBar.ascx.cs " Inherits= "NSToolBar " %>
<table width= "0% " border= "0 ">
<tr>
<td> <asp:ImageButton ID= "imgbtn_Add " runat= "server " ImageUrl= "~/Images/Add.png " OnClick= "imgbtn_Add_Click " /> </td>
<td> <asp:ImageButton ID= "imgbtn_Edit " runat= "server " ImageUrl= "~/Images/Edit.png " OnClick= "imgbtn_Edit_Click " /> </td>
<td> <asp:ImageButton ID= "imgbtn_Delete " runat= "server " ImageUrl= "~/Images/Delete.png " OnClick= "imgbtn_Delete_Click " /> </td>
<td> <asp:ImageButton ID= "imgbtn_Undo " runat= "server " ImageUrl= "~/Images/Undo.png " OnClick= "imgbtn_Undo_Click " /> </td>
<td> <asp:ImageButton ID= "imgbtn_Save " runat= "server " ImageUrl= "~/Images/Save.png " OnClick= "imgbtn_Save_Click " /> </td>
</tr>
</table>
------解决方案--------------------使用委托
------解决方案--------------------GZ
------解决方案--------------------public delegate void PageIndexChangedEventHandler(object sender, System.EventArgs e);
public event PageIndexChangedEventHandler PageIndexChanged;
protected override void CreateChildControls()
{
Controls.Clear();
LinkButton lbtnFirst = new LinkButton();
lbtnFirst.ID = "lbtnFirst ";
lbtnFirst.Text = "首页 ";
lbtnFirst.Click += new EventHandler(lbtnFirst_Click);
this.Controls.Add(lbtnFirst);
}
private void lbtnFirst_Click(object sender, System.EventArgs e)
{
if ( PageIndexChanged != null )
{
PageIndexChanged(this, e);
}
}
------解决方案--------------------mark有时间好好看看!