[c#] Récupérer le code HTML généré par le gridView
Bonjour à tous,
Voilà, tout les dans le titre.. J'aimerais récuperer dans une chaine (ou créer dans un fichier) juste le HTML généré par un gridview.
J'ai essayé un truc comme ça mais ça me renvoie une chaine vide.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| protected void lbExport_Click(object sender, EventArgs e) {
// LastIdxTab.Value.ToString()
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
PrepareGridViewForExport(gvXml);
gvXml.RenderControl(htw);
lblExport.Text = sw.ToString(); // Renvoie vide :-( */
}
private void PrepareGridViewForExport(Control gv) {
LinkButton lb = new LinkButton();
Literal l = new Literal();
string name = String.Empty;
for (int i = 0; i < gv.Controls.Count; i++) {
if (gv.Controls[i].GetType() == typeof(LinkButton)) {
l.Text = (gv.Controls[i] as LinkButton).Text;
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
} else if (gv.Controls[i].GetType() == typeof(DropDownList)) {
l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
} else if (gv.Controls[i].GetType() == typeof(CheckBox)) {
l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False";
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
if (gv.Controls[i].HasControls()) {
PrepareGridViewForExport(gv.Controls[i]);
}
}
}
public override void VerifyRenderingInServerForm(Control control) {
// Pour éviter d'avoir l'erreur: Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server." Lors du RenderControl.
} |
Est ce que quelqu’un a une idée ?
Merci d'avance.