2016-11-10 120 views
2

我对ASP.NET相对来说比较陌生,并且在创建自定义用户控件时遇到问题。我正在尝试创建一个文本框和一个图像按钮。当点击图像按钮时,GridView会弹出各种选择选项。我没有充实所有东西,但我不断遇到的问题是我的imageButton onClick事件不会触发。我在我的onClick命令上运行一个断点,但他们似乎从未开火。事件不会在自定义用户控件中触发

我检查了很多论坛,但找不到解决我的问题的信息。我包含用户控件的代码以及运行它的Web表单。对不起,在这个问题上有任何混淆。感谢您的任何建议或信息点。如果你认为有一个话题,我应该谷歌不要害怕让我知道。对于丑陋的代码也很抱歉(我也会接受任何基本的编码建议)。顺便说一句,我已经为updatepanel/scriptmanager导入了AJAX Toolkit。提前致谢。

--Custom控制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> 

--Custom控制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> 

--test 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;     
     }  
    } 
} 

回答

0

看起来你想从你的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; 
} 
+0

非常感谢。我阅读了一些关于代表的.Net文章,但无法完全理解它。似乎我不得不和他们一起玩弄一些想法。看起来Delegate存储一个事件,将该事件发送到父页面。父页面然后对该命令作出反应。再次感谢,当我明天回去工作时,我将不得不尝试这个。 – wsbobbitt

+0

今天尝试执行此操作。似乎这是正确的方式去做事情。我一直在阅读代表并观看一些视频。仍然无法解决问题。看起来我需要在我的用户控件中将事件处理程序指向委托。周一我会再讨论这个问题。在我学习的所有编码概念中,代表似乎是一个更抽象/更难学的概念。 – wsbobbitt

+0

我终于完成了我的解决方案。我最终没有将任何事件传递给我的父页面。我刚刚重写了解决方案以处理控件中的所有事件。代表证明太多,无法处理这一秒。如果有人向代表推荐任何优秀的文章,请告诉我。 – wsbobbitt

相关问题