wsbobbitt wsbobbitt - 29 days ago 9
ASP.NET (C#) Question

Events do not fire in Custom User Control

I am relatively new to ASP.NET, and was having trouble creating a Custom User Control. I am trying to create a Textbox and an Image Button. When the Image Button is clicked, a GridView pops up with various choices to select from. I haven't fleshed everything out, but my problem I keep running into is that my imageButton

onClick
event does not fire. I am running a breakpoint on my
onClick
command, but they never seem to fire.

I've checked many forums, but couldn't find information that solved my issue. I'm including the code for the user control as well as the web form running it. Sorry for any confusion in the question. Thanks for any advice or points toward info. If you think there is a topic I should google don't be afraid to let me know. Also sorry for the ugly code(I'll also take any basic coding advice I can get). BTW I've imported the AJAX Toolkit for the updatepanel/scriptmanager. Thanks in advance.

--Custom Control 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 Control 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;
}
}
}

Answer

It looks like you want to send a command from your UserControl to the parent page. If so you have to use Delegate instead of EventHandler.

Add this to the User Control

//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);
}

And then in the parent page

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