Good idea! However I tried and it did not make a difference. Thanks
Main Topics
Browse All TopicsI am building an ASP.NET page, with C# code behind, using a MultiView with various DetailsViews and buttons on each View. Some of the buttons are dynamically captioned, and also I want to group them into the "value" cell of a DetailsView, rather than floating below the grid.
So I do this in a PreRender handler:
for ( int row = 0; row < DetailsViewResponse.Contro
{
DataControlFieldCell dcfc = DetailsViewResponse.Contro
if ( dcfc != null )
{
DataControlField dcf = dcfc.ContainingField;
if ( dcf != null )
{
BoundField bf = dcf as BoundField;
if ( bf != null )
{
Control dataCell = DetailsViewResponse.Contro
switch ( bf.DataField )
{
case MessageCollection.ColumnNa
// Clear any existing child controls.
dataCell.Controls.Clear();
dataCell.Controls.Add( this.ChoiceButton1 );
dataCell.Controls.Add( this.ChoiceButton2 );
dataCell.Controls.Add(this
// Manually re-register the event-handling method for
// the Click event of the Button control.
//this.ChoiceButton2.Attri
//this.ChoiceButton2.Click
break;
case MessageCollection.ColumnNa
// Clear any existing child controls.
dataCell.Controls.Clear();
dataCell.Controls.Add(this
break;
}
}
}
}
}
When I do this, the buttons' OnClick (e.g. ChoiceButton1_Click) stops working. Clicking on the button does nothing.
As soon as I comment out any of the dataCell.Controls.Add() calls, that button's OnClick starts working again.
I tried various ways to try to re-attach the OnClick handler, as shown by the commented out lines, but none worked.
How to reenable or retain the OnClick handler for a button that is moved into a DetailsView cell?
p.s. The DetailsView is enabled.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
See attached code snippets.
Create a C# Web Site.
Drop on a DetailsView, ObjectDataSource, 2 Buttons, and a TextBox.
Label the Buttons.
Configure the DataSource to use the JustSomeData class and its GetData() method for SELECT.
Attach the DetailsView to the DataSource.
Run it.
Click Button2 and you get a message in the TextBox.
Click Button1 and nothing happens.
Hi. Thanks for posting the code. I took a look and the problem is clear. The ASP.NET Page Life Cycle is important to understand when manipulating the control Hierarchy in any situation. In this case, moving the controls around (specifically in the case Button1) is not happening soon enough. When you do what you do, the button looses its link to the event handlers and the event handler is never called. The solution is pretty simple, move the calle to Render_DetailsView1 to an earlier point like the Page_Load method.
check the attached code and you'll be fine.
Enjoy.
Business Accounts
Answer for Membership
by: Infinite_RecursionPosted on 2008-08-01 at 07:41:11ID: 22138703
Hi, I am not sure if it is relevant, but you might want to remove the button from its original Parent's Controls collection before adding it to the DetailsView.