responding to events raised by controls in a webcontrol

  • Thread starter colter k?ntz via .NET 247
  • Start date
C

colter k?ntz via .NET 247

I am attempting to respond to the Click event of a button (WebControls.Button) that is programatically created by a WebControl. I want the event handler to be defined in the WebForm that is using my WebControl.

I bind an event handler to the Click event of the button, but it never seems to get called.

But if i put the code (to create button and add event handler) into the WebForm, it works fine, the event handler is called.

Here is my code, the webcontrol .cs, followed by my webform .aspx and codebehind .cs:

Webcontrol:

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections;

namespace EmployeeInfoWebControl
{
public class foo : System.Web.UI.WebControls.WebControl, System.Web.UI.INamingContainer
{
public System.Web.UI.WebControls.Button w = new Button();

protected override void Render(HtmlTextWriter output)
{
w.ID = "blah";
w.Text = "webcontrol button";
w.Click += new EventHandler(w_Click);
w.RenderControl(output);
}

private void w_Click(object sender, EventArgs e)
{
Page.Response.Write("webcontrol button handler!!");
}
}
}


webform .aspx:


<%@ Register TagPrefix="EmployeeInfoWebControl" Namespace="EmployeeInfoWebControl" Assembly="EmployeeInfoWebControl" %>
<%@ Page language="c#" Codebehind="xt.aspx.cs" AutoEventWireup="false" Inherits="SecurityControl.xt" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Applications</title>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<EmployeeInfoWebControl:foo runat="server" id="goo" />
</form>
</body>
</HTML>


webform codebehind:


using System;
using System.ComponentModel;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace SecurityControl
{
/// <summary>
/// Summary description for Applications.
/// </summary>
public class xt: System.Web.UI.Page
{
protected EmployeeInfoWebControl.foo goo;

public System.Web.UI.WebControls.Button p = new Button();
public System.Web.UI.HtmlControls.HtmlForm Form1;

private void Page_Load(object sender, System.EventArgs e)
{
p.Text = "page button";
p.Click += new EventHandler(p_Click);
Form1.Controls.Add(p);

goo.w.Click += new EventHandler(w_Click);
}

private void p_Click(object sender, EventArgs e)
{
Page.Response.Write("page button handler");
}

private void w_Click(object sender, EventArgs e)
{
Page.Response.Write("webcontrol button handler defined on page");
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion

}
}



the above code will put two buttons on the form, one created by the webcontrol, and the other by the webform itself.

the event handler is only being called for the button created by the webform.


the rendered html output looks like so:

<form name="Form1" method="post" action="xt.aspx" id="Form1">
<input type="hidden" name="__VIEWSTATE" value="<snip>" />
<input type="submit" name="blah" value="webcontrol button" id="blah" />
<input type="submit" name="_ctl0" value="page button" />
</form>


i am not sure if the naming (id/name) of the submit button has something to do with it. i tried implementing the INamingContainer interface but did not help.

I also tried using the Command event rather than Click because the docs say Command is bubbled to the parent control if that matters??

Any ideas???
 
R

Robert

Are you making a Composite Control ? If so - you should append your
WebControls.Button via the CreateChildControls method

like:

// pass through - You can omit this
protected override void Render(HtmlTextWriter output)
{
base.Render(output)
}

protected override void CreateChildControls()
{
w.ID = "blah";
w.Text = "webcontrol button";
w.Click += new EventHandler(w_Click);
Controls.Add( w );
}

private void w_Click(object sender, EventArgs e)
{
Page.Response.Write("webcontrol button handler!!");
}


You can raise an event in w_Click which can be caught inyour webform.

Robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top