views:

292

answers:

2

I have a datalist inside a usercontrol that gets loaded into a page where users can customize a report based on some checkboxes.

One of the checkboxes, however, is "Hide Worklog" which should hide the worklog column from the result set because it can be quite long and interfere with the report.

If I do:

datatable1.Columns.Remove("WorkLog");

the code throws an exception because:

<asp:Label ID="WorkLog" runat="server" Text='<%# Bind("WorkLog") %>'></asp:Label></td>

doesn't exist.

Am I going about the usercontrol all wrong? This usercontrol should always be able to show the worklog, so I don't think it's bad to bind it in there, but at the same time I want to be able to hide it if the user wants.

A: 

bind it in code behind after checking some condition. like

if (visible) {
    //bind
}

while removing control

  visible = false;

you might need to change visible to session var :)

Ratnesh Maurya
+1  A: 

Try removing the label control from your DataList instead of removing the column from the data source (i.e. the DataTable)

DataList1.Controls.Remove(DataList1.FindControl("WorkLog"));

You shouldn't get an error if the data source has more columns than you're displaying on the page, however, you will get an error, as you've discovered, if you're trying to display a column that doesn't exist in the data source.

Ken Pespisa
yeah.. but why to send across the data when it is not required.. unless you want to do it using AJAX or something :)
Ratnesh Maurya
How would I do this in a datalist?
somacore
I revised my answer.
Ken Pespisa