473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

DataGrid ButtonColumn/BoundColumn problem

The ItemCommand event not getting fired when I add both a BoundColumn and a
ButtonColumn to a datagrid. When I add a ButtonColumn by itself, everything
works fine, but as soon as I add a BoundColumn, the ItemCommand event
doesn't seem to get called.

I've been struggling with this for too long now, I'm certain there's
something I'm not getting. Here's the relevant code. Thanks in advance!

// As is, this code does not correctly call the ItemCommand event handler.
// Comment out AddIDColumn however and the ItemCommand event handler
// is called when expected.
private void Page_Load(object sender, System.EventArgs e) {
AddIDColumn();
AddButtonColumn();
dg.DataSource = GetDataSource();
dg.DataKeyField = "employeeid";
if ( !IsPostBack )
dg.DataBind();
}

private void AddIDColumn() {
BoundColumn bound = new BoundColumn();
bound.DataField = "EmployeeID";
bound.HeaderText = "EmployeeID";
dg.Columns.Add( bound );
}

private void AddButtonColumn() {
ButtonColumn button = new ButtonColumn();
button.ButtonType = ButtonColumnType.LinkButton;
button.Text = "Click Me";
button.CommandName = "whatever";
dg.Columns.Add( button );
}

private void dg_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e) {
dg.DataBind();
}
Nov 17 '05 #1
3 2336
Hi

If you add some column at runtime, these have to be loaded at the
initialization of the page (OnInit method) not in Page_load otherwise the
page doesn't succeed to load the relative viewstate.

If you move AddIDColumn(); and AddButtonColumn();
into the OnInit method your example it will work

Ciao
Giorgio

"John" <fu******@hotmail.com> ha scritto nel messaggio
news:1H******************@news20.bellglobal.com...
The ItemCommand event not getting fired when I add both a BoundColumn and a ButtonColumn to a datagrid. When I add a ButtonColumn by itself, everything works fine, but as soon as I add a BoundColumn, the ItemCommand event
doesn't seem to get called.

I've been struggling with this for too long now, I'm certain there's
something I'm not getting. Here's the relevant code. Thanks in advance!

Nov 17 '05 #2
Thanks for your response, you're right this does work. But this brings upon
more questions! Why do these need to be set in the Init exactly? Also, I
want to allow the user to show or hide the ButtonColumn, so I store a flag
in the ViewState to control this. I believe the ViewState is only loaded
after Init is called. How can I decide to show or hide the ButtonColumn
then? What's the solution to this problem?

"Giorgio Parmeggiani" <gi******@gipadotnet.com> wrote in message
news:eA**************@TK2MSFTNGP10.phx.gbl...
Hi

If you add some column at runtime, these have to be loaded at the
initialization of the page (OnInit method) not in Page_load otherwise the
page doesn't succeed to load the relative viewstate.

If you move AddIDColumn(); and AddButtonColumn();
into the OnInit method your example it will work

Ciao
Giorgio

"John" <fu******@hotmail.com> ha scritto nel messaggio
news:1H******************@news20.bellglobal.com...
The ItemCommand event not getting fired when I add both a BoundColumn and
a
ButtonColumn to a datagrid. When I add a ButtonColumn by itself,

everything
works fine, but as soon as I add a BoundColumn, the ItemCommand event
doesn't seem to get called.

I've been struggling with this for too long now, I'm certain there's
something I'm not getting. Here's the relevant code. Thanks in

advance!


Nov 17 '05 #3
Hi
Why do these need to be set in the Init exactly? Also, I
want to allow the user to show or hide the ButtonColumn, so I store a flag
in the ViewState to control this. I believe the ViewState is only loaded
after Init is called. How can I decide to show or hide the ButtonColumn
then? What's the solution to this problem?


Asp.NET has a well defined Control Life Cycle
(http://www.15seconds.com/issue/020102.htm)
so new controls added at runtime at the control collection must be added in
the OnInit.
but it is enough that is initialized in the OnInit! you can modify it in
other methods (for example in the Page_Load)
For your problem you can:
1 - Add the datagrdi column on the OnInit
2 - Hide the column in the Page_load event: datagrid.Columns[0].Visible =
false.

Ciao
Giorgio
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: James Norton-Jones | last post by:
Hi, Am I trying to hold the data of a DataGrid in a label so that when the form is reposted the DataGrid can be repopulated. The problem I am having is that I don't understand how to get the...
2
by: Marty McDonald | last post by:
When setting certain datagrid properties with the IDE (as in the property window), they don't seem to take effect. For instance, Font Name. So I have to set these via code-behind at the cell...
0
by: Colin Ramsay | last post by:
Hi all, I don't normally post swathes of code like this but I am truly banging my head off my desk here... I've dynamically created a datagrid within a usercontrol. There are two columns...
1
by: m3ckon | last post by:
Hi there, please help if you can, I'm having an issue with droponnlists in a datagrid I have a datagrid which is populated from a query .. all works fine I've added two extra columns, one...
2
by: wolfgang wagner | last post by:
hi all! im trying to edit data in a datagrid (like described here: http://aspnet.4guysfromrolla.com/articles/071002-1.aspx) but if i click the edit button i get the following error invalid...
1
by: Lenin Sakthees via .NET 247 | last post by:
Hi all, I am using a datagrid to show values from a SQL table. My problem is, I want to show the details of the each row when the user clicks the 'View details' buttoncolumn. I am able to fire a...
1
by: BW | last post by:
Hello, I am attempting to to take edited values from a datagrid control and update the underlying database. Problem is that some of the edited values don quite seem to make it to my update sub....
5
by: Jeff User | last post by:
Hello ..NET 1.1, VS 2003, C# & asp.net I have tried to follow msdn instructions and samples but I can not get an event to fire for this button on the datagrid. There has to be something obvious...
4
by: Jeff User | last post by:
Hi I tryed to solve this problem over in the framework.asp group, but still am having trouble. Hope someone here can help. using .net 1.1, VS 2003 and C# I have an asp.DataGrid control with a...
2
by: Ray Booysen | last post by:
Hi all I have a collection of Invoices that I bind to a datagrid. Some of these invoices have been processed and you can tell this via a property in the invoice class called DateProcessed. If...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.