0

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 ???

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by john_beginner
0

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.

0

>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();
 
    }
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.