Click here to Skip to main content
15,895,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i read from some website that when visible =false asp.net control does not hold their value.but my code shown below work normally
XML
<form id="form1" runat="server">
    <div>

        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1"
            runat="server" Text="Button" onclick="Button1_Click" />

        <asp:TextBox ID="TextBox2" runat="server" Visible="False">0</asp:TextBox>

    </div>
    </form>

and C# code
C#
public partial class Index : System.Web.UI.Page
{
    int i;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox1.Text = TextBox2.Text;
        i = Convert.ToInt32(TextBox2.Text);
        i++;
        TextBox2.Text = i.ToString();
    }
}

will increment walue of text box ..Plese some one explain why
Posted
Comments
Richard C Bishop 14-Mar-13 9:16am    
Well your code in the Button1_Click event makes no sense at all. All you are doing is converting the "0" in your TextBox2 to an Int32 and incrementing it and then setting the same TextBox2 back to the i. That code will always show just the incrementing of i in TextBox2.

i read from some website that when visible =false asp.net control does not hold their value
Well, you read wrong or interpreted wrong.

When you set a control as not visible, they are not rendered in UI. It's not in the HTML response returned by server to client. Since it is a server control, you can access the control and it's value on server side at any time.
 
Share this answer
 
when you use any asp control in your web page it does means an object is creating , so object is retaining the value of i in your above program and visible property on use for showing control to user or,
when you use visible= "false" property control will act like hiddenfields its not showing to uers only but exist in your page....
 
Share this answer
 
When you set visible = false for a control, although the control is not rendered into the form. But its state is maintained with the help of viewstate. So each time the form is loaded the value of TextBox2 is maintained in ViewState of the page.

in the above example if you set EnableViewState = "false" for the control, you will not get the updated value.
XML
<asp:textbox id="TextBox2" runat="server" visible="False"  EnableViewState="false">0</asp:textbox>


Hope this helps. :)
 
Share this answer
 
v4
I think it is wrong that control does not contain value when its visibility is false.

I have tested this code on my own PC and its result is same.
 
Share this answer
 
v2
When You set ""visible = false"",it still posses the value assign to it,as text box is server side control ,even if that is ""visible = false"", it's value is still accessible in code behind.
so above mentioned answers are fine.
 
Share this answer
 

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