visible = false和asp.net控件值 [英] visible =false and asp.net control values

查看:135
本文介绍了visible = false和asp.net控件值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一些网站上读到,当visible = false时,asp.net控件不能保存它们的值。但是我的代码如下所示正常工作

 <  表格    id   =  form1    runat   =  server < span class =code-keyword>>  
< div >

< asp:TextBox ID = TextBox1 runat = server > < / asp:TextBox >
< asp:按钮 ID = Button1

runat = server 文本 = 按钮 onclick = Button1_Click / >

< asp:TextBox ID = TextBox2 < span class =code-attribute> runat = server 可见 = 错误 > 0 < / asp:TextBox >

< / div >
< / form > ;



和C#代码

  public   partial   class 索引:System.Web.UI.Page 
{
int i;
受保护 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();
}
}



将增加文本框的内容..请一些解释为什么

解决方案

我从一些网站上读到,当visible = false时,asp.net控件没有保存它们的值

嗯,你读错了或解释错了。



当您将控件设置为不可见时,它们不会在UI中呈现。它不在服务器返回给客户端的HTML响应中。由于它是一个服务器控件,您可以随时访问控件及其在服务器端的值。


当您在网页中使用任何asp控件时,它确实意味着对象正在创建,因此对象保留了上述程序中i的值和用于向用户显示控件的可见属性,或

当您使用visible =false时属性控件将像hiddenfields一样它不仅仅显示在你的页面中,而是存在于你的页面中....


为控件设置 visible = false 时,尽管控件未呈现为表格。但它的状态是在 viewstate 的帮助下维持的。所以每次加载表单时,TextBox2的值都保存在页面的ViewState中。



在上面的例子中如果设置 EnableViewState =false 对于控件,您将无法获得更新的值。

 <   asp:textbox     id   =  TextBox2    runat   =  server   可见  = 错误       EnableViewState   =  false    >  0 <   / asp:textbox  >  





希望这会有所帮助。 :)


i read from some website that when visible =false asp.net control does not hold their value.but my code shown below work normally

<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

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

解决方案

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.


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


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.

<asp:textbox id="TextBox2" runat="server" visible="False"  EnableViewState="false">0</asp:textbox>



Hope this helps. :)


这篇关于visible = false和asp.net控件值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆