[英]ASP.NET Adding gridview to panel
我是ASP.NET的新手。 我正在建4頁。 應該將gridview
導入頁面。 並且有一個類可以保存它。 在另一個頁面中,我應該得到gridview
。 但是當我加載頁面時,我收到錯誤:
RegisterRequiresControlState
只能在PreRender
之前和期間調用。
這是我的頁面加載代碼:
protected void Page_Load(object sender, EventArgs e) { if (Data._grid != null) { p1.Controls.Add(Data._grid); } else { } }
對於aspx:
<asp:Panel runat="server" ID="p1"></asp:Panel>
這里是將gridview
保存到類的代碼:
protected void import_btn_Click(object sender, EventArgs e) { string filepath = ConfigurationManager.AppSettings["FilePath"].ToString(); string filename = string.Empty; string[] allowedfile = { ".xls", ".xlsx" }; string filetext = Path.GetExtension(FileUpload1.PostedFile.FileName); if (!allowedfile.Contains(filetext)) { } else { int filesize = FileUpload1.PostedFile.ContentLength; if (filesize <= 1048576) { filename = Path.GetFileName(Server.MapPath(FileUpload1.FileName)); FileUpload1.SaveAs(Server.MapPath(filepath) + filename); string _filepath = Server.MapPath(filepath) + filename; OleDbConnection con = null; if (filetext == ".xls") { con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + _filepath + ";Extended Properties=Excel 8.0"); } else if (filetext == ".xlsx") { con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + _filepath + "';Extended Properties=Excel 12.0;"); } con.Open(); DataTable dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); string get_sheet = dt.Rows[0]["Table_Name"].ToString(); OleDbCommand excelcommand = new OleDbCommand(@"SELECT * FROM [" + get_sheet + @"]", con); OleDbDataAdapter exceladapter = new OleDbDataAdapter(excelcommand); DataSet exceldataset = new DataSet(); exceladapter.Fill(exceldataset); con.Close(); grid.DataSource = exceldataset; grid.DataBind(); Data._grid = grid; } }
您無法將控件從一個頁面導入到其他頁面。 您可以創建可重用的用戶控件.ascx並在不同的頁面中使用它們。
在您的情況下,您可以創建一個具有GridView的Web用戶控件,並在兩個頁面中添加該用戶控件,就像我們使用asp.net Web控件一樣。 閱讀本MSDN文章ASP.NET用戶控件概述中有關用戶控件的更多信息
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.