自定义用户控件中的事件未触发

3
我相对较新于ASP.NET,正在尝试创建一个自定义用户控件,里面包括一个文本框和一个图像按钮。当点击该按钮时,会弹出一个GridView以供选择。但我遇到的问题是我的图像按钮的onClick事件无法触发,尽管我在该命令上运行了断点监视器,但它们似乎从未触发过。我查看了许多论坛,但没有找到解决我的问题的信息。我在下面附上了用户控件代码和使用它的Web表单代码。抱歉如果我的问题有些混淆,请谅解。感谢任何建议或指向信息的指导,如果您认为有我应该Google的主题,请告诉我。同时也抱歉我的代码可能显得很丑陋(我将接受任何基础编码建议)。顺便提一下,我已经导入了用于UpdatePanel/ScriptManager的AJAX Toolkit。
<%@ 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>

--自定义控件 ASCX.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;                
        }    
    }
}
1个回答

1

看起来您想从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;
}

非常感谢。我读了一些关于委托的 .Net 文章,但是还没有真正理解它们。看起来我需要多试一下才能搞明白。似乎 Delegate 存储一个事件,将该事件发送到父页面。然后父页面会对该命令做出反应。再次感谢,明天回到工作岗位时我需要尝试一下。 - wsbobbitt
今天尝试了实现这个功能。看起来这是正确的方法。我一直在阅读委托相关的资料并观看一些视频,但仍然无法解决问题。似乎我需要将事件处理程序指向用户控件中的委托。我会在周一继续尝试。在我学习的所有编码概念中,委托似乎是一个更抽象/难以理解的概念。 - wsbobbitt
我终于完成了我的解决方案。最终,我没有将任何事件传递给我的父页面。我只是重写了解决方案,以便在控件内处理所有事件。目前而言,委托证明太难处理了。如果有人推荐任何关于委托的好文章,请告诉我。 - wsbobbitt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接