我有一个modalpopupextender,它会弹出一个预先加载了编辑和删除按钮的gridview。我创建了一个事件,当单击编辑按钮时,它应该能够编辑网格中的一行。但是当它被点击时,唯一发生的事情就是它只是来到一个空白页面。这是我的课程的代码:
Class1.cs
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.Text = "Edit Grid";
Button cancelbtn = new Button();
cancelbtn.ID = "Cancel-" + i.ToString();
cancelbtn.Text = "Cancel";
GridView gv1 = new GridView();
gv1.ID = "GridViewNew-" + (i + 5).ToString();
gv1.AutoGenerateColumns = true;
gv1.AutoGenerateDeleteButton = true;
gv1.AutoGenerateEditButton = true;
gv1.RowEditing += (sender, e) => gv1_RowEdit(sender, e, gv1);
gv1.DataSource = HttpContext.Current.Session[(i).ToString()];
gv1.DataBind();
Panel pn = new Panel();
pn.ID = "Panel-" + (i + 5).ToString();
pn.CssClass = "modalPopup";
pn.Controls.Add(gv1);
pn.Controls.Add(cancelbtn);
AjaxControlToolkit.ModalPopupExtender modalPop = new AjaxControlToolkit.ModalPopupExtender();
modalPop.ID = "ModalPopup" + i;
modalPop.PopupControlID = pn.ID;
modalPop.TargetControlID = btn.ID;
modalPop.CancelControlID = cancelbtn.ID;
modalPop.BackgroundCssClass = "modalBackground";
// Adding modalpop to panel
pn.Controls.Add(modalPop);
pn.Controls.Add(gv1);
// Adding Panel to placeholder
ph.Controls.Add(pn);
ph.Controls.Add(gv);
ph.Controls.Add(btn);
}
}
private void gv1_RowEdit(object sender, GridViewEditEventArgs e, GridView gv)
{
gv.EditIndex = e.NewEditIndex;
}
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;
}
}
Default3.aspx.cs
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);
}
}
}