Click here to Skip to main content
15,912,507 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    List<string> d= new List<string>();
    protected void Button1_Click1(object sender, EventArgs e)
    {
        d.Add(TextBox1.Text);
        d.Add(TextBox2.Text);
        d.Add(TextBox3.Text);
        d.Add(TextBox4.Text);
    }   
    protected void Button2_Click(object sender, EventArgs e)
    {
        d.Add(TextBox5.Text);
        d.Add(TextBox6.Text);
    }
    protected void Button3_Click1(object sender, EventArgs e)
    {
        foreach (string i in d)   //i am unable to get values of textboxes at button3 click.
        {
            Response.Write(i);   //how can i get.
        }
    }
} 
Posted
Updated 16-Jun-13 18:16pm
v2
Comments
Sergey Alexandrovich Kryukov 17-Jun-13 0:23am    
Please, don't re-post; this is considered as abuse. You should work on the page of your original question: add comments to answers, use "Improve question"...
—SA

Yes,
Amit is correct.

It is a basic thing in web Technology that between post back request, all the data will
be lost and the each request is considered as a new request.

So, each variable will be in it's original stag.

In this code , the variavle "List<string> d" is defined out side the all the methods,
so it will considered as a Golbal Variable for that page.

And to have the link with the older data for the variable before the post back ,

save the data in the viewstate or in Session is good idea.
If any one has another way , please post it here.

Thanks ..

From,
Dhiraj Luhana
 
Share this answer
 
C#
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        List<string> d = new List<string>();
        d.Add(TextBox1.Text);
        d.Add(TextBox2.Text);
        d.Add(TextBox3.Text);
        d.Add(TextBox4.Text);
        ViewState["d"] = d;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        List<string> d;
        d = (List<string>)ViewState["d"];
        d.Add(TextBox5.Text);
        d.Add(TextBox6.Text);
    }
    protected void Button3_Click1(object sender, EventArgs e)
    {
        List<string> d;
        d = (List<string>)ViewState["d"];
        foreach (string i in d)
        {
            Response.Write(i);
        }
    }
}
 
Share this answer
 
Since, List<string> d is a global variable. Every variable defined in a class inheriting Web.UI.Page will be destroyed at the end of the Page-Lifecycle, hence it will be null in a Postback. You can store it in the Session or the ViewState.
Try like this:
C#
List<string> d= new List<string>();
protected void Button1_Click1(object sender, EventArgs e)
{
    d.Add(TextBox1.Text);
    d.Add(TextBox2.Text);
    d.Add(TextBox3.Text);
    d.Add(TextBox4.Text);
    ViewState["Value"] = d; //Store the value in viewstate
}
protected void Button2_Click(object sender, EventArgs e)
{
    d.Add(TextBox5.Text);
    d.Add(TextBox6.Text);
}
protected void Button3_Click1(object sender, EventArgs e)
{
    List<string> str = ViewState["Value"] as List<string>;
    foreach (string i in str)   //i am unable to get values of textboxes at button3 click.
    {
        Response.Write(i);   //how can i get.
    }
}



--Amit
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 17-Jun-13 0:24am    
This is the OP's re-post. Basically, you explained the problem, but there is no such thing as "global variable"...
—SA
_Amy 17-Jun-13 0:28am    
I mean to say "variable defined in a class". :)
Sergey Alexandrovich Kryukov 17-Jun-13 1:24am    
It's called "member", either property of field.
(By the way: I didn't vote.)
—SA
_Amy 17-Jun-13 4:12am    
:) I know.

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