J'ai un contrôle d'assistant qui comporte des étapes créées dynamiquement. Chaque étape contient un contrôle gridview également créé dynamiquement.Autoriser la pagination pour la grille dynamique
La page permet aux utilisateurs de télécharger plusieurs fichiers Excel qui sont ensuite affichés dans un contrôle de l'assistant. Plus spécifiquement, les fichiers sont affichés dans des grilles qui sont créées dynamiquement dans les étapes du contrôle de l'assistant.
Comment activer le paging de chaque gridview par programme? Le balisage est illustré ci-dessous:
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPages/Frontend.master" AutoEventWireup="true" CodeFile="FileUpload_Multi.aspx.cs" Inherits="Analysis_Files_FileUpload_Multi" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
<span style="font-family: Arial">Supported file formats: .csv, .xls, .xlsx<br />
<br />
Click to add files</span>
<asp:Button ID="btnAdd" Text="Add" OnClick="OnAdd" runat="server" />
<asp:Panel ID="pnlTemp" runat="server">
</asp:Panel>
<br />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
<asp:Label ID="lbl_upload" runat="server" Text="No file added!"
Visible="False"></asp:Label>
<asp:Label ID="lbl_fileformat" runat="server" Text="File format not supported!" Visible="False"></asp:Label>
<br />
<br />
<asp:Panel id="wizardPanel" runat="server" >
</asp:Panel>
<br />
</asp:Content>
Le code est derrière indiqué ci-dessous:
protected void btnUpload_Click(object sender, EventArgs e)
{
if (Request.Files.Count == 0)
{
this.pnlTemp.Controls.Clear();
lbl_upload.Visible = true;
}
else
{
this.pnlTemp.Controls.Clear();
Wizard gvWizard = new Wizard();
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFile PostedFile = Request.Files[i];
if (PostedFile.ContentLength > 0)
{
string ConStr = "";
string ext = Path.GetExtension(PostedFile.FileName).ToLower();
string FileName = System.IO.Path.GetFileName(PostedFile.FileName);
string path = Server.MapPath("~/Analysis/Files/" + FileName);
PostedFile.SaveAs(Server.MapPath("Files\\") + FileName); //save file to drive for future use
WizardStepBase newStep = new WizardStep();
newStep.ID = "uploadFile" + (i + 1);
newStep.Title = FileName;
if (ext.Trim() == ".xls")
{
//connection string for that file which extantion is .xls
ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (ext.Trim() == ".xlsx")
{
//connection string for that file which extantion is .xlsx
ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
else if (ext.Trim() == ".csv") //not working yet!
{
ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='text;HDR=YES;FMT=CSVDelimited'";
}
else
{
lbl_fileformat.Visible = true;
break;
}
GridView gridview = new GridView();
gridview.ID = "gd" + (i + 1);
//bind gridview
string query = "SELECT * FROM [Sheet1$]";
//Providing connection
OleDbConnection conn = new OleDbConnection(ConStr);
//checking that connection state is closed or not if closed the
//open the connection
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
//create command object
OleDbCommand cmd = new OleDbCommand(query, conn);
// create a data adapter and get the data into dataadapter
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
//fill the Excel data to data set
da.Fill(ds);
//set data source of the grid view
gridview.DataSource = ds.Tables[0];
//binding the gridview
gridview.DataBind();
//close the connection
conn.Close();
newStep.Controls.Add(gridview);
gvWizard.WizardSteps.Add(newStep);
}
else
{
this.pnlTemp.Controls.Clear();
lbl_upload.Visible = true;
}
}
wizardPanel.Controls.Add(gvWizard);
gvWizard.FinishCompleteButtonText = "Analyze data";
}
}