somesogoood

Wednesday, February 18, 2009

GridView custom control that contains how to Export to Excel

GridView custom control that contains how to Export to Excel
We often use GridView Export to Excel functionality, I also have some articles prior to the introduction to do.

Today I was asked, if the template Girdview out using a custom control, then how to deal with when exported?

For example, the custom control contains a DROPDOWNLIST and three Label controls, which used to display data on the page is one of the Label control, the question now is, if not PrepareControlForExport () in the custom control to handle, then Export The Excel files used in the corresponding GridView custom control out of the contents of all the same (DROPDOWNLIST controls for the first Item), but the custom control to handle the case, do not know how to remove it in order to correct the value ( used to display the label of the value), trying to not be able to custom control into Dropdowlist and Label in any of, but the mandatory conversion if the statement will run error.

We first look at the GridView Export to Excel and open source chart tools mentioned in the development of export tools, source files can be downloaded here: Export GridView to Excel

In GridViewExportUtil.cs, the function PrepareControlForExport are like this:


/ / /
/ / / Replace any of the contained controls with literals
/ / /
/ / /
private static void PrepareControlForExport (Control control)
(
for (int i = 0; i
(
Control current = control.Controls [i];
if (current is LinkButton)
(
control.Controls.Remove (current);
control.Controls.AddAt (i, new LiteralControl ((current as LinkButton). Text));
)
else if (current is ImageButton)
(
control.Controls.Remove (current);
control.Controls.AddAt (i, new LiteralControl ((current as ImageButton). AlternateText));
)
else if (current is HyperLink)
(
control.Controls.Remove (current);
control.Controls.AddAt (i, new LiteralControl ((current as HyperLink). Text));
)
else if (current is DropDownList)
(
control.Controls.Remove (current);
control.Controls.AddAt (i, new LiteralControl ((current as DropDownList). SelectedItem.Text));
)
else if (current is CheckBox)
(
control.Controls.Remove (current);
control.Controls.AddAt (i, new LiteralControl ((current as CheckBox). Checked? "True": "False"));
)

if (current.HasControls ())
(
GridViewExportUtil.PrepareControlForExport (current);
)
)
)




This section of code is introduced to control traversal of all sub-control, for each sub-control, in accordance with sub-type of control to generate a LiteralControl, and with this son LiteralControl to replace the current control. For LinkBtton, use it to replace the text, for ImagaButton, are using it to replace the Alternate Text. Function against the LinkButton, ImageButton, HyperLink, DropDwonList, CheckBox to do a deal, if the current control is not any one of which, it has to determine whether sub-control, and recursive calls.

Back to the question before us, if there is a GridView custom control, then how do we do? In fact, the solution on the original idea to be able to determine are not custom control, use this custom control in one way or another in a hope to export the text information read out, converted to a Literal control can be.

We can write a function to deal with such a comparison is complicated, the model code is as follows:

else if (current is MyWebController)
(
control.Controls.Remove (current);
control.Controls.AddAt (i, GetLiteralController (current));
)

GetLiteralControler As for the wording, you can custom control traversal of all sub-controls, according to the characteristics of control, such as location, ID select a control, converted to Literal controls and back.

This article is an essay, recorded about ideas, not the actual test, if the question, please also pointed out that the U.S. gradually enriched.

No comments: