DataGrid ButtonColumn/BoundColumn problem

J

John

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();
}
 
G

Giorgio Parmeggiani

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
 
J

John

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?
 
G

Giorgio Parmeggiani

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
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top