Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ALL,

i have one textbox and one GridView,
here if i have enter rollno 1st time, display in related GridView, here 1st time proper work. But if 2nd time and 3rd Time Different rollno Entered textbox, Then i Want Hold Previous Data in Grid(All Data 1st to 3rd).
How i Hold All Data in Grid??
Please Help Me.
Posted
Comments
DamithSL 16-Dec-14 9:56am    
update the question with your code

1 solution

It seems that as your page is being refreshed so you lose previous data .
You can use asp:UpdatePanel and ViewState to hold previous data and place a asyn triggger for insert button event.
Here is a your solution

In .Aspx
ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication8._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div><asp:ScriptManager runat="server"></asp:ScriptManager>
        <asp:TextBox ID="txtRoll" runat="server" ></asp:TextBox>
        <asp:Button ID="btnAdd" runat="server" Text="Push" OnClick="btnAdd_Click" />
        <asp:UpdatePanel ID="up" runat="server">
        <ContentTemplate>
        <asp:gridview ID="Gridview1" runat="server"></asp:gridview>
        </ContentTemplate>
        <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btnAdd" EventName="Click" />
        </Triggers>
        </asp:UpdatePanel>

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


Code behind
C#
public  List<string> alstroll;
       protected void Page_Load(object sender, EventArgs e)
       {
           if (!this.IsPostBack)
           {
               alstroll = new List<string>();
               ViewState["RollList"] = alstroll;


           }
           else
               alstroll = (List<string>)ViewState["RollList"];
           Gridview1.DataSource = alstroll;
           Gridview1.DataBind();
       }

       protected void btnAdd_Click(object sender,EventArgs e)
       {
           if (txtRoll.Text != null)
           {
               alstroll.Add(txtRoll.Text);
               ViewState["RollList"] = alstroll;

               Gridview1.DataBind();
           }

       }
 
Share this answer
 
Comments
Avadhesh yadav 19-Dec-14 12:52pm    
You are right sir, and thanks a lot for help, Anisuzzaman
Anisuzzaman Sumon 20-Dec-14 7:14am    
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900