事件不会在自定义用户控件中触发
本文关键字:控件 用户 自定义 事件 | 更新日期: 2023-09-27 18:17:25
我对ASP比较陌生。NET,并且在创建自定义用户控件时遇到了问题。我试图创建一个文本框和一个图像按钮。当点击图像按钮时,会弹出一个GridView,其中有各种选项可供选择。我还没有充实的一切,但我的问题,我一直遇到的是,我的imageButton onClick
事件不火。我在我的onClick
命令上运行一个断点,但是它们似乎从来没有触发过。
我已经检查了许多论坛,但找不到解决我问题的信息。我包括用户控件的代码以及运行它的web表单。对于问题中的任何困惑,我感到抱歉。感谢任何建议或指向的信息。如果你认为有一个话题我应该谷歌不要害怕让我知道。也很抱歉丑陋的代码(我也会采取任何基本的编码建议,我可以得到)。顺便说一句,我已经为updatepanel/scriptmanager导入了AJAX工具包。提前谢谢。
—自定义控件ASCX
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="CustomCntrl.WebUserControl1" ClassName="WebUserControl1" EnableViewState="true"%>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel runat="server" RenderMode="Inline" ID="UpdatePanel1">
<ContentTemplate>
<asp:TextBox ID="txtOutput" runat="server"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
<asp:ImageButton ID="IBtnLkUp" CausesValidation="false" runat="server" ImageUrl="~/Images/Lookup.png" OnClick="IBtnLkUp_Click" OnCommand="IBtnLkUp_Command" />
<ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="IBtnLkUp" PopupControlID="Panel1" CancelControlID="btnCancel" DropShadow="true"></ajaxToolkit:ModalPopupExtender>
<asp:Panel ID="Panel1" runat="server">
<asp:GridView runat="server" ID="gvSrch" OnRowCancelingEdit="gvSrch_RowCancelingEdit" OnRowDeleting="gvSrch_RowDeleting" OnRowUpdating="gvSrch_RowUpdating" OnSelectedIndexChanging="gvSrch_SelectedIndexChanging"></asp:GridView>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Search" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" />
</asp:Panel>
—自定义控件asx .cs—
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CustomCntrl
{
public partial class WebUserControl1 : System.Web.UI.UserControl
{
public event EventHandler evLookupBtn;
public event EventHandler evSelectBtn;
public event EventHandler evDeleteBtn;
public event EventHandler evEditBtn;
public string sImgBtnUrl { get; set; }
public DataTable dtLookup { get; set; }
public bool bSelectBtn { get; set; }
public bool bEditBtn { get; set; }
public bool bDeleteBtn { get; set; }
public bool bPaging { get; set; }
public int pagesize { get; set; }
protected void OnPreInit() {
//IBtnLkUp.Click += IBtnLkUp_Click;
//ImageButton ib = (ImageButton)IBtnLkUp;
//ib.Click += new EventHandler(IBtnLkUp_Click(this.IBtnLkUp, ImageClickEventArgs.Empty ));
Panel1.Controls.Add(IBtnLkUp);
}
/* public event ImageClickEventHandler IBtnLkUp_Click{
add { IBtnLkUp.Click += value; }
remove { IBtnLkUp.Click -= value; }
}*/
protected void Page_Load(object sender, EventArgs e)
{
//IBtnLkUp.Click += IBtnLkUp_Click;
IBtnLkUp.ImageUrl = sImgBtnUrl;
}
protected void IBtnLkUp_Click(object sender, ImageClickEventArgs e)
{
//Panel1.Visible = true;
//UpdatePanel1.Visible = true;
//gvSrch.Visible = true;
gvSrch.Visible = true;
//UpdatePanel1.Visible = true;
Panel1.Visible = true;
gvSrch.AutoGenerateSelectButton = bSelectBtn;
gvSrch.AutoGenerateEditButton = bEditBtn;
gvSrch.AutoGenerateDeleteButton = bDeleteBtn;
if (bPaging == true)
{
gvSrch.AllowPaging = bPaging;
gvSrch.PageSize = pagesize;
}
else gvSrch.AllowPaging = false;
gvSrch.DataSource = dtLookup;
gvSrch.DataBind();
if(evLookupBtn !=null)
evLookupBtn(this, EventArgs.Empty);
//Panel1.Visible = true;
//UpdatePanel1.Visible = true;
//gvSrch.Visible = true;
}
protected void gvSrch_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
evSelectBtn(this, EventArgs.Empty);
}
protected void gvSrch_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
evEditBtn(this, EventArgs.Empty);
}
protected void gvSrch_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
evDeleteBtn(this, EventArgs.Empty);
}
protected void gvSrch_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
e.Cancel = true;
gvSrch.EditIndex = -1;
gvSrch.DataBind();//rebind data?
}
/* protected void IBtnLkUp_Click1(object sender, ImageClickEventArgs e)
{
//Panel1.Visible = true;
//UpdatePanel1.Visible = true;
//gvSrch.Visible = true;
gvSrch.Visible = true;
//UpdatePanel1.Visible = true;
Panel1.Visible = true;
gvSrch.AutoGenerateSelectButton = bSelectBtn;
gvSrch.AutoGenerateEditButton = bEditBtn;
gvSrch.AutoGenerateDeleteButton = bDeleteBtn;
if (bPaging == true)
{
gvSrch.AllowPaging = bPaging;
gvSrch.PageSize = pagesize;
}
else gvSrch.AllowPaging = false;
gvSrch.DataSource = dtLookup;
gvSrch.DataBind();
if (evLookupBtn != null)
evLookupBtn(this, EventArgs.Empty);
//Panel1.Visible = true;
//UpdatePanel1.Visible = true;
//gvSrch.Visible = true;
}
*/
protected void IBtnLkUp_Command(object sender, CommandEventArgs e)
{
}
}
}
——Test.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="CustomCntrl.WebForm1"enableviewstate="true" %>
<%@ Register Src="~/UserControls/WebUserControl1.ascx" TagPrefix="uc1" TagName="WebUserControl1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title runat="server"></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:WebUserControl1 runat="server" ID="WebUserControl1" sImgBtnUrl="~/Images/Lookup.png" bDeleteBtn="false" bEditBtn="false" bSelectBtn="true" bPaging="false"/>
</div>
<asp:Label ID="LabelTest" runat="server" Text=""></asp:Label>
</form>
</body>
</html>
——测试ASPX.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CustomCntrl
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void WebUserControl1_evSelectBtn(object sender,EventArgs e) {
LabelTest.Text = "Success";
}
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Clear();
dt.Columns.Add("Name");
dt.Columns.Add("Marks");
DataRow dr = dt.NewRow();
dr[0] = "Smith";
dr[1] = "1";
dt.Rows.Add(dr);
WebUserControl1.dtLookup = dt;
}
}
}
看起来你想从你的UserControl
发送一个命令到父页面。如果是这样,你必须使用Delegate
而不是EventHandler
。
添加到用户控件
//declare the delegates
private Delegate _sendCommandToParentControl;
public Delegate sendCommandToParentControl
{
set { _sendCommandToParentControl = value; }
}
//just a button click event handler
protected void sendCommandToParentControl_Click(object sender, EventArgs e)
{
//send the textbox value to the parent by invoking the delegated command
_sendCommandToParentControl.DynamicInvoke(TextBox1.Text);
}
然后在父页
delegate void commandFromChildControlDelegate(string value);
protected void Page_Load(object sender, EventArgs e)
{
//add the command to the usercontrol
commandFromChildControlDelegate command = new commandFromChildControlDelegate(processCommandFromChildControl);
WebUserControl1.sendCommandToParentControl = command;
}
//the command invoked from the child control
private void processCommandFromChildControl(string value)
{
Label1.Text = value;
}