Question

So on a textbox I will write how many dropdownlists, textboxes, label etc and the page ia AutoPostBack. But after I choose an item on a gridview, the page ia autopostback and all the data dissapear. I want gridview to be AutoPostBack but how can I make it not to dissapear all the other item I have added from the first textbox?

For more info here is the source code:

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>

and 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"));
            }
        }
    }
}
Was it helpful?

Solution

you can do as below

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"));
        }
    }
}

you don't need OnTextChanged="TextBox1_TextChanged" in your textbox

OTHER TIPS

   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"));
            }
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top