我一直在网站上搜索此类问题的好答案,但是每个人都有不同的解决方法,而且我的英语在文档方面也有些呆滞:
https://msdn.microsoft.com/en-us/library/bb399001.aspx
我尝试使用Session变量强制转换表OnPostBack,但仍然无法正常工作:
我的HTML代码(精简版)是:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="popup.aspx.cs" Inherits="AgroRiego.popup" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Horario</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
<form id="form1" runat="server">
<div class="container mb-5">
<div class="row">
<div class="offset-4 col-4">
<asp:DropDownList ID="select_predo" AutoPostBack="true" runat="server" CssClass="form-control"></asp:DropDownList>
</div>
</div>
</div>
<div class="container">
<asp:Table ID="riego" ViewStateMode="Enabled" runat="server" CssClass="container">
<asp:TableHeaderRow CssClass="row">
<asp:TableHeaderCell CssClass="col" Enabled="false">Múltiplo</asp:TableHeaderCell>
<asp:TableHeaderCell CssClass="col">Dia programado</asp:TableHeaderCell>
<asp:TableHeaderCell CssClass="col" Enabled="false">Horario</asp:TableHeaderCell>
<asp:TableHeaderCell CssClass="col">Cantidad de Horas</asp:TableHeaderCell>
<asp:TableHeaderCell CssClass="col">Horario término</asp:TableHeaderCell>
<asp:TableHeaderCell CssClass="col">Fertilización</asp:TableHeaderCell>
</asp:TableHeaderRow>
</asp:Table>
</div>
<div class="container">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="updatepanel1" runat="server">
<ContentTemplate>
<asp:Table ID="fertilizacion" EnableViewState="true" Visible="false" runat="server" CssClass="container">
<asp:TableHeaderRow CssClass="row">
<asp:TableHeaderCell CssClass="col">Horas de Fertilización</asp:TableHeaderCell>
<asp:TableHeaderCell CssClass="col">Hora de Inicio</asp:TableHeaderCell>
<asp:TableHeaderCell CssClass="col">Producto</asp:TableHeaderCell>
<asp:TableHeaderCell CssClass="col">Unidad</asp:TableHeaderCell>
<asp:TableHeaderCell CssClass="col">Hectareas</asp:TableHeaderCell>
<asp:TableHeaderCell CssClass="col">Has</asp:TableHeaderCell>
<asp:TableHeaderCell CssClass="col">Mult</asp:TableHeaderCell>
</asp:TableHeaderRow>
</asp:Table>
</ContentTemplate>
</asp:UpdatePanel>
</div>
基本上,我用一个按钮在Ontable上向第二个表中添加行,这是该表仅保存最后添加的行!而且我仍然不明白为什么它不保存以前的迭代,如果有人给我答案并给出详尽的解释,我会一直爱他直到我的日子。
服务器端代码:
public partial class popup : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable predos = Session["predos"] as DataTable;
if (!IsPostBack)
{
foreach (DataRow row in predos.Rows)
select_predo.Items.Add(row["ID"].ToString());
Deploy(predos.Rows[0]["ID"].ToString());
}
else
{
Deploy(select_predo.SelectedValue);
}
}
public void Deploy(string first_let)
{
DataTable taba = Session[0] as DataTable;
foreach (DataRow row in taba.Rows)
{
if (row["Multiplo"].ToString().Contains(first_let))
{
TableRow r = new TableRow();
r.ID = row["Multiplo"].ToString();
r.CssClass = "row";
TableCell sector = new TableCell();
sector.CssClass = "col";
sector.Text = row["Multiplo"].ToString();
r.Cells.Add(sector);
TableCell dia = new TableCell();
dia.CssClass = "col";
dia.Text = DateTime.Parse(row["Fecha"] as string).ToShortDateString();
r.Cells.Add(dia);
TableCell hora = new TableCell();
hora.CssClass = "col";
DropDownList horas = new DropDownList();
horas.ID = row["Multiplo"].ToString() + "drop";
horas.SelectedIndexChanged += new EventHandler(horachange);
horas.Attributes.Add("OnSelectedIndexChange", "horachange");
horas.AutoPostBack = true;
for (int i = 0; i < 24; i++)
{
ListItem hor = new ListItem();
if (i < 10)
hor.Text = "0" + i.ToString() + ":00";
else
hor.Text = i.ToString() + ":00";
horas.Items.Add(hor);
}
hora.Controls.Add(horas);
r.Cells.Add(hora);
TableCell canthoras = new TableCell();
canthoras.CssClass = "col";
canthoras.Text = row["Total Hrs"] as string;
r.Cells.Add(canthoras);
TableCell termino = new TableCell();
termino.ID = row["Multiplo"] + "end";
termino.CssClass = "col";
termino.Text = (Int32.Parse(horas.SelectedValue.Remove(2, 3)) + Int32.Parse(canthoras.Text)).ToString() + ":00";
r.Cells.Add(termino);
TableCell adfert = new TableCell();
Button but = new Button();
but.ID = row["Multiplo"] + "_fert";
but.Click += new EventHandler(addfert);
but.Text = "Agregar Fertilización";
adfert.Controls.Add(but);
r.Cells.Add(adfert);
riego.Rows.Add(r);
}
}
}
void addfert(object sender, EventArgs e)
{
Button b = sender as Button;
TableRow r = b.Parent.Parent as TableRow;
if (!fertilizacion.Visible)
fertilizacion.Visible = true;
TableRow row = new TableRow();
row.ID = r.ID + "fert";
row.CssClass = "row";
TableCell horasfert = new TableCell();
horasfert.CssClass = "col";
TableCell horainit = new TableCell();
horainit.CssClass = "col";
TableCell product = new TableCell();
product.CssClass = "col";
TableCell unit = new TableCell();
unit.CssClass = "col";
TableCell hectas = new TableCell();
hectas.CssClass = "col";
TableCell HAS = new TableCell();
HAS.CssClass = "col";
TableCell Mult = new TableCell();
Mult.CssClass = "col";
TextBox horasferti = new TextBox();
horasferti.EnableViewState = true;
horasferti.CssClass = "form-control";
horasferti.ID = r.ID + "_hor_fert";
horasferti.Text = r.Cells[3].Text;
horasfert.Controls.Add(horasferti);
DropDownList horain = new DropDownList();
horain.EnableViewState = true;
for (int i = 1; i <= 24; i++)
horain.Items.Add(i.ToString() + ":00");
horainit.Controls.Add(horain);
TextBox producto = new TextBox();
producto.EnableViewState = true;
producto.CssClass = "form-control";
product.Controls.Add(producto);
unit.Text = "Grs";
TextBox hecta = new TextBox();
hecta.EnableViewState = true;
hecta.CssClass = "form-control";
hectas.Controls.Add(hecta);
TextBox ha = new TextBox();
ha.EnableViewState = true;
ha.CssClass = "form-control";
HAS.Controls.Add(ha);
Mult.Text = r.ID;
row.Cells.Add(horasfert);
row.Cells.Add(horainit);
row.Cells.Add(product);
row.Cells.Add(unit);
row.Cells.Add(hectas);
row.Cells.Add(HAS);
row.Cells.Add(Mult);
row.EnableViewState = true;
fertilizacion.Rows.Add(row);
}