PostBack后所有数据都消失了

时间:2014-01-22 08:26:37

标签: c# asp.net gridview postback

因此,在文本框中,我将编写多少下拉列表,文本框,标签等以及页面ia AutoPostBack。但是当我在gridview上选择一个项目后,页面就会自动回复并且所有数据都消失了。我希望gridview成为AutoPostBack但是如何才能让它消除我从第一个文本框中添加的所有其他项目?

有关详细信息,请参阅源代码:

Default.aspx的

<%@ 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">
        .auto-style1 {
            width: 248px;
        }
        .auto-style2 {
            width: 253px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <div>
        <table style="width:100%;">
            <tr>
                <td class="auto-style2">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style1">&nbsp;</td>
                <td class="auto-style2">
        <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style1">&nbsp;</td>
                <td class="auto-style2">
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style1">&nbsp;</td>
                <td class="auto-style2">
        <asp:PlaceHolder ID="PlaceHolder2" runat="server"></asp:PlaceHolder>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style1">&nbsp;</td>
                <td class="auto-style2">
        <asp:PlaceHolder ID="PlaceHolder3" runat="server"></asp:PlaceHolder>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style1">&nbsp;</td>
                <td class="auto-style2">
        <asp:PlaceHolder ID="PlaceHolder4" runat="server"></asp:PlaceHolder>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style1">&nbsp;</td>
                <td class="auto-style2">
        <asp:PlaceHolder ID="PlaceHolder5" runat="server"></asp:PlaceHolder>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style1">&nbsp;</td>
                <td class="auto-style2">
                    &nbsp;</td>
                <td>&nbsp;</td>
            </tr>
        </table>
        <br />
    </div>
    </form>
</body>
</html>

和Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class Default2 : System.Web.UI.Page
{
    DropDownList artikulli;
    TextBox cmimi;
    Label tregoCmimi;
    TextBox sasia;
    Label cmimiGjithsej;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {

        }
    }

    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(TextBox1.Text))
        {
            int a = int.Parse(TextBox1.Text);
            for (int j = 1; j <= a; j++)
            {
                Guid IDUnik = new Guid();

                artikulli = new DropDownList();
                cmimi = new TextBox();
                tregoCmimi = new Label();
                sasia = new TextBox();
                cmimiGjithsej = new Label();

                artikulli.ID = j.ToString(IDUnik.ToString("N").Substring(31));
                artikulli.AutoPostBack = true;
                cmimi.ID = j.ToString(IDUnik.ToString("D").Substring(30));
                tregoCmimi.ID = j.ToString(IDUnik.ToString("P"));
                sasia.ID = j.ToString(j.ToString(IDUnik.ToString("X")));
                cmimiGjithsej.ID = j.ToString(j.ToString(IDUnik.ToString("B"))); ;

                PlaceHolder1.Controls.Add(artikulli);
                PlaceHolder2.Controls.Add(cmimi);
                PlaceHolder3.Controls.Add(tregoCmimi);
                PlaceHolder4.Controls.Add(sasia);
                PlaceHolder5.Controls.Add(cmimiGjithsej);

                artikulli.Items.Insert(0, new ListItem("<Select Subject>", "0"));
                artikulli.Items.Insert(1, new ListItem("<Select Subject>", "1"));
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

你可以这样做

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack)
    {
        AddItems();
    }
}

private void AddItems()
{
    if (!string.IsNullOrEmpty(TextBox1.Text))
    {
        int a = int.Parse(TextBox1.Text);
        for (int j = 1; j <= a; j++)
        {
            Guid IDUnik = new Guid();

            artikulli = new DropDownList();
            cmimi = new TextBox();
            tregoCmimi = new Label();
            sasia = new TextBox();
            cmimiGjithsej = new Label();

            artikulli.ID = j.ToString(IDUnik.ToString("N").Substring(31));
            artikulli.AutoPostBack = true;
            cmimi.ID = j.ToString(IDUnik.ToString("D").Substring(30));
            tregoCmimi.ID = j.ToString(IDUnik.ToString("P"));
            sasia.ID = j.ToString(j.ToString(IDUnik.ToString("X")));
            cmimiGjithsej.ID = j.ToString(j.ToString(IDUnik.ToString("B"))); ;

            PlaceHolder1.Controls.Add(artikulli);
            PlaceHolder2.Controls.Add(cmimi);
            PlaceHolder3.Controls.Add(tregoCmimi);
            PlaceHolder4.Controls.Add(sasia);
            PlaceHolder5.Controls.Add(cmimiGjithsej);

            artikulli.Items.Insert(0, new ListItem("<Select Subject>", "0"));
            artikulli.Items.Insert(1, new ListItem("<Select Subject>", "1"));
        }
    }
}

您的文本框中不需要OnTextChanged="TextBox1_TextChanged"

答案 1 :(得分:0)

   protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            Populate();
        }
    }

    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {

        Populate();
    }

    void Populate()
    {
        PlaceHolder1.Controls.Clear();
        PlaceHolder2.Controls.Clear();
        PlaceHolder3.Controls.Clear();
        PlaceHolder4.Controls.Clear();
        PlaceHolder5.Controls.Clear();
        if (!string.IsNullOrEmpty(TextBox1.Text))
        {
            int a = int.Parse(TextBox1.Text);
            for (int j = 1; j <= a; j++)
            {
                Guid IDUnik = new Guid();

                artikulli = new DropDownList();
                cmimi = new TextBox();
                tregoCmimi = new Label();
                sasia = new TextBox();
                cmimiGjithsej = new Label();

                artikulli.ID = j.ToString(IDUnik.ToString("N").Substring(31));
                artikulli.AutoPostBack = true;
                cmimi.ID = j.ToString(IDUnik.ToString("D").Substring(30));
                tregoCmimi.ID = j.ToString(IDUnik.ToString("P"));
                sasia.ID = j.ToString(j.ToString(IDUnik.ToString("X")));
                cmimiGjithsej.ID = j.ToString(j.ToString(IDUnik.ToString("B"))); ;

                PlaceHolder1.Controls.Add(artikulli);
                PlaceHolder2.Controls.Add(cmimi);
                PlaceHolder3.Controls.Add(tregoCmimi);
                PlaceHolder4.Controls.Add(sasia);
                PlaceHolder5.Controls.Add(cmimiGjithsej);

                artikulli.Items.Insert(0, new ListItem("<Select Subject>", "0"));
                artikulli.Items.Insert(1, new ListItem("<Select Subject>", "1"));
            }
        }
    }