hello all of u !

i m begging in c# and making one program in asp.net !

i have very simple query ,

public partial class _Default : System.Web.UI.Page 
{
    public String  str;

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        str = "abcd";
             
        Label1.Text = str;  
    }
    protected void Button2_Click(object sender, EventArgs e)
    {

        Label1.Text = str;
 
    }
}

in above code when i press button1 then label1 text become abcd

but when i press button2 then label1 text become empty !

how can i acess this value in button2 event ???

Recommended Answers

All 3 Replies

hello all of u !

i m begging in c# and making one program in asp.net !

i have very simple query ,

public partial class _Default : System.Web.UI.Page 
{
    public String  str;

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        str = "abcd";
             
        Label1.Text = str;  
    }
    protected void Button2_Click(object sender, EventArgs e)
    {

        Label1.Text = str;
 
    }
}

in above code when i press button1 then label1 text become abcd

but when i press button2 then label1 text become empty !

how can i acess this value in button2 event ???

If you press button1 first than button2 will do anything(it will actually assign the string, but since the string is exactly the same, you wont see any difference). This is because your string value is not changing. If on click2 you change the value you will have a sweet result.

insteand of using String you should use the keyword string. String is the class reference to the keyword string.

Simply redefine your string value in the second click;

str = "dcba";


Hope this helps.

>but when i press button2 then label1 text become empty !

Because HTTP is a stateless protocol. No changes/states are recorded at client or server side. When you hit a button (submit), a page and control objects are created with it's initial state.

Learn the asp.net state management feature.

ViewState example,

public partial class _Default : System.Web.UI.Page 
{
    public String  str;

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        str = "abcd";
        Label1.Text=str;     
        ViewState["key1"]=str;  
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
     if(ViewState["key1"]!=null)
        Label1.Text = ViewState["key1"].ToString();
 
    }
}

Thanx a lot Mr. ADATAPOST

i have tried this code n itz working

once again thank u ! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.