我有以下代码,经过测试,效果很好:
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 1; i < 5; i++)
{
var temp = i;
DataTable table = GetTable();
HttpContext.Current.Session[i.ToString()] = table;
GridView gv = new GridView();
gv.ID = "GridView-" + i.ToString();
gv.DataSource = HttpContext.Current.Session[i.ToString()];
gv.DataBind();
Button btn = new Button();
btn.ID = "button-" + i.ToString();
btn.Click += (senders, es) => CreateModalPop(temp,PlaceHolder1, btn.ID);
PlaceHolder1.Controls.Add(gv);
PlaceHolder1.Controls.Add(btn);
}
}
static DataTable GetTable()
{
//
// Here we create a DataTable with four columns.
//
DataTable table = new DataTable();
table.Columns.Add("Dosage", typeof(int));
table.Columns.Add("Drug", typeof(string));
table.Columns.Add("Patient", typeof(string));
table.Columns.Add("Date", typeof(DateTime));
//
// Here we add five DataRows.
//
table.Rows.Add(25, "Indocin", "David", DateTime.Now);
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
return table;
}
protected void CreateModalPop(int num, PlaceHolder ph, string btnID)
{
GridView gv = new GridView();
gv.ID = "GridViewNew-" + num.ToString();
gv.AutoGenerateColumns = true;
gv.DataSource = HttpContext.Current.Session[num.ToString()];
gv.DataBind();
Panel pn = new Panel();
pn.ID = "Panel-" + num.ToString();
pn.CssClass = "modalPopup";
pn.Controls.Add(gv);
AjaxControlToolkit.ModalPopupExtender modalPop = new AjaxControlToolkit.ModalPopupExtender();
modalPop.ID = "ModalPopup-1";
modalPop.PopupControlID = pn.ID;
modalPop.TargetControlID = btnID;
modalPop.BackgroundCssClass = "modalBackground";
// Adding modalpop to panel
pn.Controls.Add(modalPop);
// Adding Panel to placeholder
ph.Controls.Add(pn);
modalPop.Show();
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.modalBackground { background-color: Gray; filter: alpha(opacity=70); opacity: 0.7; cursor:not-allowed; position: absolute; top: 0%; left: 0%; width: 100%; height: 140%; overflow:hidden;}
.modalPopup { background-color: #ffffdd; border-width: 3px; border-style: solid; border-color: Gray; padding: 3px; width: 350px; }
</style>
</head>
<body>
<form id="form1" runat="server">
<ajaxToolkit:ToolkitScriptManager CombineScripts="false" ScriptMode="Release" ID="ToolkitScriptManager1" runat="server"></ajaxToolkit:ToolkitScriptManager>
<div>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</div>
</form>
</body>
</html>
现在我接受了这段代码,并尝试将它放在一个类中,然后使用该类。执行此操作后,表格会在页面加载时加载,但是当您单击其中一个按钮时,它会将您带到一个空白页面。为什么这样做?或者我该如何解决?
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.modalBackground { background-color: Gray; filter: alpha(opacity=70); opacity: 0.7; cursor:not-allowed; position: absolute; top: 0%; left: 0%; width: 100%; height: 140%; overflow:hidden;}
.modalPopup { background-color: #ffffdd; border-width: 3px; border-style: solid; border-color: Gray; padding: 3px; width: 350px; }
</style>
</head>
<body>
<form id="form1" runat="server">
<ajaxToolkit:ToolkitScriptManager CombineScripts="false" ScriptMode="Release" ID="ToolkitScriptManager1" runat="server"></ajaxToolkit:ToolkitScriptManager>
<div>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default3 : System.Web.UI.Page
{
// Global Object initializer
Class1 inv = new Class1();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
inv.SetInitialRow(PlaceHolder1);
}
}
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
/// <summary>
/// Summary description for Class1
/// </summary>
public class Class1
{
public Class1()
{
//
// TODO: Add constructor logic here
//
}
public void SetInitialRow(PlaceHolder ph)
{
for (int i = 1; i < 5; i++)
{
var temp = i;
DataTable table = GetTable();
HttpContext.Current.Session[i.ToString()] = table;
GridView gv = new GridView();
gv.ID = "GridView-" + i.ToString();
gv.DataSource = HttpContext.Current.Session[i.ToString()];
gv.DataBind();
Button btn = new Button();
btn.ID = "button-" + i.ToString();
btn.Click += (senders, es) => CreateModalPop(temp, ph, btn.ID);
ph.Controls.Add(gv);
ph.Controls.Add(btn);
}
}
static DataTable GetTable()
{
//
// Here we create a DataTable with four columns.
//
DataTable table = new DataTable();
table.Columns.Add("Dosage", typeof(int));
table.Columns.Add("Drug", typeof(string));
table.Columns.Add("Patient", typeof(string));
table.Columns.Add("Date", typeof(DateTime));
//
// Here we add five DataRows.
//
table.Rows.Add(25, "Indocin", "David", DateTime.Now);
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
return table;
}
protected void CreateModalPop(int num, PlaceHolder ph, string btnID)
{
GridView gv = new GridView();
gv.ID = "GridViewNew-" + num.ToString();
gv.AutoGenerateColumns = true;
gv.DataSource = HttpContext.Current.Session[num.ToString()];
gv.DataBind();
Panel pn = new Panel();
pn.ID = "Panel-" + num.ToString();
pn.CssClass = "modalPopup";
pn.Controls.Add(gv);
AjaxControlToolkit.ModalPopupExtender modalPop = new AjaxControlToolkit.ModalPopupExtender();
modalPop.ID = "ModalPopup-1";
modalPop.PopupControlID = pn.ID;
modalPop.TargetControlID = btnID;
modalPop.BackgroundCssClass = "modalBackground";
// Adding modalpop to panel
pn.Controls.Add(modalPop);
// Adding Panel to placeholder
ph.Controls.Add(pn);
modalPop.Show();
}
}