<uc2:Toolbar ID="toolbar" EditOnclick="OpenUpdatePage('MarketTypesEdit.aspx?state=edit&key=');" AddOnclick="OpenAddPage ('MarketTypesEdit.aspx?state=add');" SearchServerClick="btnSearchClick" runat="server" />
做一个用户控件,想在调用此控件页面的前端配置此用户控件里Button里的事件SearchServerClick="btnSearchClick"
用户控件里有配置一个属性 和事件初始化
/// <summary>
/// 查询事件
/// </summary>
public EventHandler SearchServerClick
{
get { return searchServerClick; }
set
{
searchServerClick = value;
}
}
private EventHandler searchServerClick;
btnSearch.ServerClick += searchServerClick;
为什么会出错,正确的要如何写?
搜索关键字:C# 自定义事件
太多教程了---
这样写事件,真不如在用户控件中的最外DIV里写。
是B/S吧?呵呵
btnSearch.ServerClick += searchServerClick;??
换一种写法
直接在用户控件的按钮事件中。添加:if(searchServerClick!=null) searchServerClick(); 回调
你在页面的 Page_Load 事件里面赋值。
给你一段代码看看:前台调用用户控件的按钮事件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace User_Control
{
public delegate void Click(object sender,EventArgs e);
public partial class Search : System.Web.UI.UserControl
{
//大类下拉框
public string Drop_Cate_Value
{
get { return Drop_Categories.SelectedValue; }
}
//小类下拉框
public string Drop_Small_Value
{
get { return Drop_Small.SelectedValue; }
}
/// <summary>
/// 为下拉提供绑定数据
/// </summary>
/// <param name="dt">table</param>
/// <param name="Key">键值</param>
/// <param name="Value">显示值</param>
/// <param name="bol">是大类还是小类</param>
public void Drop_DataBind(ListControl lsc, DataTable dt,string Key,string Value)
{
lsc.DataSource = dt;
lsc.DataValueField = Key;
lsc.DataTextField = Value;
lsc.DataBind();
}
public event Click OnClick;
protected void btn_search_Click(object sender, EventArgs e)
{
if (OnClick != null)
{
OnClick(this, e);
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
前台调用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace User_Control
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(string));
dt.Columns.Add("Name", typeof(string));
DataRow dr = dt.NewRow();
dr[0] = "1";
dr[1] = "Yes";
dt.Rows.Add(dr);
DataRow dr1 = dt.NewRow();
dr1[0] = "2";
dr1[1] = "No";
dt.Rows.Add(dr1);
DropDownList drop_cate =(DropDownList) Search1.FindControl("Drop_Categories");
DropDownList drop_small = (DropDownList)Search1.FindControl("Drop_Small");
Search1.Drop_DataBind(drop_cate, dt, "Id", "Name");
Search1.Drop_DataBind(drop_small,dt, "Id", "Name");
}
Search1.OnClick += new Click(Search1_OnClick);
}
void Search1_OnClick(object sender, EventArgs e)
{
Response.Write(string.Format("{0}/{1}",Search1.Drop_Cate_Value,Search1.Drop_Small_Value));
}
}
}