簡體   English   中英

表中的項目在回發后消失了

[英]Items in Table are gone after a Postback

每當我隱藏或顯示面板時,我(動態)添加到表格中的項目就消失了,另一件事是,當我嘗試將新行添加到表格中時,它不會顯示,而是覆蓋了同一行,為什么??。

這是CodeBehind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            DropDownList1.Items.Add(TextBox3.Text);
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            if (Panel1.Visible == true)
                Panel1.Visible = false;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Panel1.Visible == false)
                Panel1.Visible = true;
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            TableCell tdStudentName = new TableCell();
            tdStudentName.Text = TextBox1.Text;

            TableCell tdStudentCourse = new TableCell();
            tdStudentCourse.Text = DropDownList1.SelectedValue;

            TableCell tdStudentGrade = new TableCell();
            tdStudentGrade.Text = TextBox2.Text;

            TableRow tr = new TableRow();
            tr.Cells.Add(tdStudentName);
            tr.Cells.Add(tdStudentCourse);
            tr.Cells.Add(tdStudentGrade);

            Table1.Rows.Add(tr);
        }
    }
}

這是HTML來源

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:Label ID="Label1" runat="server" Text="Student Name:"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="Label2" runat="server" Text="Course:"></asp:Label>
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Add Course" />
        <br />
        <asp:Label ID="Label3" runat="server" Text="Grade:"></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:Button ID="Button4" runat="server" OnClick="Button4_Click" Text="Add" />
        <br />
        <br />
        <asp:Panel ID="Panel1" runat="server" GroupingText="Course Settings" Visible="False">
            <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
            <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Add" />
            <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="Close" />
        </asp:Panel>
        <br />
        <br />
        <asp:Table ID="Table1" runat="server" BorderStyle="Solid">
            <asp:TableRow runat="server">
                <asp:TableCell runat="server">Name</asp:TableCell>
                <asp:TableCell runat="server">Course</asp:TableCell>
                <asp:TableCell runat="server">Grade</asp:TableCell>
            </asp:TableRow>
        </asp:Table>

    </div>
    </form>
</body>
</html>

這是它的外觀預覽:

在此處輸入圖片說明

如果您動態添加任何內容,則必須在任何后續回發中將其重新添加。 他們不會自動在下一次回發后就“呆在那里”。

這要求您存儲已經創建並添加了動態控件的事實,因此允許您在回發后檢測到該情況並再次添加回控件。

最好(如果可能)在生命周期init階段添加動態控件,因為這將意味着它們將在load階段之前獲取視圖狀態信息。


編輯

回應OP的評論...

我在哪里存儲這些控件? 我又該在哪里添加它們? 在Page_Load內部?

您不存儲控件...而是存儲已添加控件的事實。

“最簡單”的方法是使用ViewState ,但是這會使您陷入22級的局面 ...因為ViewState在生命周期的init階段不可用,因此最好將其重新添加控制。

另一種選擇是將事實存儲在<asp:HiddenField> ,您可以在init階段中進行檢查。 但是,您必須直接從Request對象獲取值,因為該字段在init階段將沒有正確的.Value 你會那樣做...

if (!Page.IsPostBack)
{
   string hdnValue = Request[hdnFieldCtrl.UniqueId];
   // Recreate controls based on the details you need
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM