Inheriting from DataGrid problem

T

Tom K

Hi folks,

I am trying to inherit from the DataGrid control and add a
few buttons and dropdowns to a
a header I am rendering above the grid. This all looks
fine (the controls render correctly) however I am not able
to capture the events from these child controls. I am
overriding
the OnBubbleEvent method which correctly captures events
from within the underlying datagrid itself but fails to
capture events from the child buttons and dropdowns. I
have tried to register delagates at various stages of the
control lifecycle however this seems to make
no difference.

I have included a simplified version of the Grid Control
below.
Any help would be greatly appreciated.

Thanks
Tom


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

namespace SimpleGridCtl
{
public class SimpleDataGrid :
System.Web.UI.WebControls.DataGrid , INamingContainer
{
System.Web.UI.WebControls.Button m_btnTest;

protected override void CreateChildControls()
{
Controls.Clear();
m_btnTest = new Button();
m_btnTest.Click += new EventHandler(m_btnTest_Click);
m_btnTest.CommandName = "Test";
m_btnTest.EnableViewState=true;
Controls.Add(m_btnTest);

base.CreateChildControls();

}

private void m_btnTest_Click(object sender, EventArgs e)
{
// never gets hit
}


public override ControlCollection Controls
{
get
{
EnsureChildControls();
return base.Controls;
}
}


protected override void OnItemCommand
(DataGridCommandEventArgs e)
{

base.OnItemCommand (e);
}

protected override bool OnBubbleEvent(object source,
EventArgs e)
{
// never gets hit for a the child button
return false;
}

protected override void Render(HtmlTextWriter writer)
{
m_btnTest.RenderControl(writer);
writer.Write("<BR>");
base.Render (writer);
}



}
}
 
L

Lewis Wang [MSFT]

Hi Tom,

Thanks for your posting. I am checking this issue, and will get back to you
with my findings.

Best regards,
Lewis

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
| Content-Class: urn:content-classes:message
| From: "Tom K" <[email protected]>
| Sender: "Tom K" <[email protected]>
| References: <[email protected]>
| Subject: Inheriting from DataGrid problem
| Date: Tue, 19 Aug 2003 06:00:53 -0700
| Lines: 99
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcNmUewURX93Fw2pR/+/lrI8mEp38w==
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:14026
| NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| Will repost this in
| dotnet.framework.aspnet.buildingcontrols. It seems more
| appropriate there.
|
| >-----Original Message-----
| >Hi folks,
| >
| >I am trying to inherit from the DataGrid control and add
| a
| >few buttons and dropdowns to a
| >a header I am rendering above the grid. This all looks
| >fine (the controls render correctly) however I am not
| able
| >to capture the events from these child controls. I am
| >overriding
| >the OnBubbleEvent method which correctly captures events
| >from within the underlying datagrid itself but fails to
| >capture events from the child buttons and dropdowns. I
| >have tried to register delagates at various stages of the
| >control lifecycle however this seems to make
| >no difference.
| >
| >I have included a simplified version of the Grid Control
| >below.
| >Any help would be greatly appreciated.
| >
| >Thanks
| >Tom
| >
| >
| >using System;
| >using System.Web.UI;
| >using System.Web.UI.WebControls;
| >using System.ComponentModel;
| >
| >namespace SimpleGridCtl
| >{
| >public class SimpleDataGrid :
| >System.Web.UI.WebControls.DataGrid , INamingContainer
| >{
| >System.Web.UI.WebControls.Button m_btnTest;
| >
| >protected override void CreateChildControls()
| >{
| >Controls.Clear();
| >m_btnTest = new Button();
| >m_btnTest.Click += new EventHandler(m_btnTest_Click);
| >m_btnTest.CommandName = "Test";
| >m_btnTest.EnableViewState=true;
| >Controls.Add(m_btnTest);
| >
| >base.CreateChildControls();
| >
| >}
| >
| >private void m_btnTest_Click(object sender, EventArgs e)
| >{
| >// never gets hit
| >}
| >
| >
| >public override ControlCollection Controls
| >{
| >get
| >{
| >EnsureChildControls();
| >return base.Controls;
| >}
| >}
| >
| >
| >protected override void OnItemCommand
| >(DataGridCommandEventArgs e)
| >{
| >
| >base.OnItemCommand (e);
| >}
| >
| >protected override bool OnBubbleEvent(object source,
| >EventArgs e)
| >{
| >// never gets hit for a the child button
| >return false;
| >}
| >
| >protected override void Render(HtmlTextWriter writer)
| >{
| >m_btnTest.RenderControl(writer);
| >writer.Write("<BR>");
| >base.Render (writer);
| >}
| >
| >
| >
| >}
| >}
| >
| >.
| >
|
 
L

Lewis Wang [MSFT]

Hi Tom,

I tested the customer control and found that the button is on the top of
datagrid. When the derived datagrid is rendered, the datagrid is on the
right place, and the added button is shown on the top-left of the page(out
side the datagrid).

I also reproduced the problem, the OnBubbleEvent does not fire when the
button is clicked. To workaround this issue, you may use user control
instead of the custom control. Another way to workaround is adding the
controls in the datagrid's header template. The following is a
demonstration about how to add a button in the header template and how to
handle the button's click event.

Change the bound columns to template columns and add the controls in the
header template.
Add the events in the controls' tags. Take a button for example:

<HeaderTemplate>
<asp:Button ID="ButtonX" Runat="server"
OnClick="ButtonX_Click"></asp:Button>
</HeaderTemplate>

3. Define a public method in the code behind for Button.Click:
public void ButtonX_Click(object sender, System.EventArgs e)
{
Response.Write ("Clicked!");
}

Hope this helps.

If I misunderstood your concern, would you please provide more explanations
on your requirement? Thank you.

Best regards,
Lewis

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
| Content-Class: urn:content-classes:message
| From: "Tom K" <[email protected]>
| Sender: "Tom K" <[email protected]>
| References: <[email protected]>
| Subject: Inheriting from DataGrid problem
| Date: Tue, 19 Aug 2003 06:00:53 -0700
| Lines: 99
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcNmUewURX93Fw2pR/+/lrI8mEp38w==
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:14026
| NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| Will repost this in
| dotnet.framework.aspnet.buildingcontrols. It seems more
| appropriate there.
|
| >-----Original Message-----
| >Hi folks,
| >
| >I am trying to inherit from the DataGrid control and add
| a
| >few buttons and dropdowns to a
| >a header I am rendering above the grid. This all looks
| >fine (the controls render correctly) however I am not
| able
| >to capture the events from these child controls. I am
| >overriding
| >the OnBubbleEvent method which correctly captures events
| >from within the underlying datagrid itself but fails to
| >capture events from the child buttons and dropdowns. I
| >have tried to register delagates at various stages of the
| >control lifecycle however this seems to make
| >no difference.
| >
| >I have included a simplified version of the Grid Control
| >below.
| >Any help would be greatly appreciated.
| >
| >Thanks
| >Tom
| >
| >
| >using System;
| >using System.Web.UI;
| >using System.Web.UI.WebControls;
| >using System.ComponentModel;
| >
| >namespace SimpleGridCtl
| >{
| >public class SimpleDataGrid :
| >System.Web.UI.WebControls.DataGrid , INamingContainer
| >{
| >System.Web.UI.WebControls.Button m_btnTest;
| >
| >protected override void CreateChildControls()
| >{
| >Controls.Clear();
| >m_btnTest = new Button();
| >m_btnTest.Click += new EventHandler(m_btnTest_Click);
| >m_btnTest.CommandName = "Test";
| >m_btnTest.EnableViewState=true;
| >Controls.Add(m_btnTest);
| >
| >base.CreateChildControls();
| >
| >}
| >
| >private void m_btnTest_Click(object sender, EventArgs e)
| >{
| >// never gets hit
| >}
| >
| >
| >public override ControlCollection Controls
| >{
| >get
| >{
| >EnsureChildControls();
| >return base.Controls;
| >}
| >}
| >
| >
| >protected override void OnItemCommand
| >(DataGridCommandEventArgs e)
| >{
| >
| >base.OnItemCommand (e);
| >}
| >
| >protected override bool OnBubbleEvent(object source,
| >EventArgs e)
| >{
| >// never gets hit for a the child button
| >return false;
| >}
| >
| >protected override void Render(HtmlTextWriter writer)
| >{
| >m_btnTest.RenderControl(writer);
| >writer.Write("<BR>");
| >base.Render (writer);
| >}
| >
| >
| >
| >}
| >}
| >
| >.
| >
|
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top