Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 允许动态gridview的分页_C#_Asp.net_Gridview - Fatal编程技术网

C# 允许动态gridview的分页

C# 允许动态gridview的分页,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个动态创建步骤的向导控件。每个步骤都包含一个gridview控件,该控件也是动态创建的 该页面允许用户上载多个excel文件,然后在向导控件中显示这些文件。更具体地说,文件显示在GridView中,GridView是在向导控件的步骤中动态创建的 如何以编程方式启用每个gridview的分页?标记如下所示: <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPages/Frontend.master" AutoEve

我有一个动态创建步骤的向导控件。每个步骤都包含一个gridview控件,该控件也是动态创建的

该页面允许用户上载多个excel文件,然后在向导控件中显示这些文件。更具体地说,文件显示在GridView中,GridView是在向导控件的步骤中动态创建的

如何以编程方式启用每个gridview的分页?标记如下所示:

    <%@ 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>
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";
    }

}

支持的文件格式:.csv、.xls、.xlsx

单击以添加文件



隐藏的代码如下所示:

    <%@ 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>
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";
    }

}
protectedvoidbtnupload\u单击(对象发送方,事件参数e)
{
if(Request.Files.Count==0)
{
this.pnlTemp.Controls.Clear();
lbl_upload.Visible=true;
}
其他的
{
this.pnlTemp.Controls.Clear();
向导gvWizard=新建向导();
对于(int i=0;i0)
{
字符串ConStr=“”;
字符串ext=Path.GetExtension(PostedFile.FileName).ToLower();
字符串FileName=System.IO.Path.GetFileName(PostedFile.FileName);
字符串路径=Server.MapPath(“~/Analysis/Files/”+文件名);
PostedFile.SaveAs(Server.MapPath(“Files\\”)+文件名);//将文件保存到驱动器以备将来使用
WizardStepBase newStep=新建WizardStep();
newStep.ID=“uploadFile”+(i+1);
newStep.Title=文件名;
如果(外部修剪()=“.xls”)
{
//extantion为.xls的文件的连接字符串
ConStr=“Provider=Microsoft.Jet.OLEDB.4.0;数据源=“+path+”“扩展属性=\”Excel 8.0;HDR=Yes;IMEX=2\”;
}
else if(ext.Trim()=“.xlsx”)
{
//extantion为.xlsx的文件的连接字符串
ConStr=“Provider=Microsoft.ACE.OLEDB.12.0;数据源=“+path+”“扩展属性=\”Excel 12.0;HDR=Yes;IMEX=2\”;
}  
else if(ext.Trim()==“.csv”)//尚未工作!
{
ConStr=“Provider=Microsoft.ACE.OLEDB.12.0;数据源=“+path+”;扩展属性='text;HDR=YES;FMT=CSVDelimited';
}
其他的
{
lbl_fileformat.Visible=true;
打破
}
GridView GridView=新的GridView();
gridview.ID=“gd”+(i+1);
//绑定网格视图
string query=“从[Sheet1$]中选择*”;
//提供连接
OLEDB连接连接=新OLEDB连接(连接);
//检查连接状态是否已关闭(如果已关闭)
//打开连接
if(conn.State==ConnectionState.Closed)
{
conn.Open();
}
//创建命令对象
OleDbCommand cmd=新的OleDbCommand(查询,连接);
//创建一个数据适配器并将数据放入dataadapter
OleDbDataAdapter da=新的OleDbDataAdapter(cmd);
数据集ds=新数据集();
//将Excel数据填入数据集
da.填充(ds);
//设置网格视图的数据源
gridview.DataSource=ds.Tables[0];
//绑定gridview
gridview.DataBind();
//关闭连接
康涅狄格州关闭();
添加(gridview);
gvWizard.WizardSteps.Add(newStep);
}
其他的
{
this.pnlTemp.Controls.Clear();
lbl_upload.Visible=true;
}
}
wizardPanel.Controls.Add(向导);
gvWizard.finishcompletebuttonext=“分析数据”;
}
}

您可以按如下语法设置分页

GridView1.DataSource = ds.Tables[0];

GridView1.AllowPaging = true;
GridView1.PageSize = 50;
GridView1.PageIndexChanged += GridView1_PageIndexChanged;

GridView1.DataBind();
但是,您必须记住,由于要将这些控件绑定到动态创建的控件,并且更改页面会导致回发,因此必须在每次回发时重新创建GrdView控件