[英]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.