TextBox的ReadOnly属性

来源:互联网 发布:mac os websphere 编辑:程序博客网 时间:2024/04/23 19:06

如果TextBox服务器控件的ReadOnly属性设置为True,在服务器端将得不到TextBox服务器的值。这是为了安全性考虑,在服务器端不处理只读文本框。

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default8.aspx.cs" Inherits="Default8" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>TextBox控件的ReadOnly属性</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:TextBox ID="TextBox1" runat="server" ReadOnly="true">Ok</asp:TextBox>

        <input id="Button2" type="button" value="修改数据为summer_grass" onclick="TextBox1.value='summer_grass'" />    //见注释1

        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="提交" />

    </div>

    </form>

</body>

</html>

 

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

 

public partial class Default8 : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        Response.Write(TextBox1.Text); //见注释2

    }

}

 

当运行以上代码时,虽然客户端中TextBox1的值已经被修改,但在服务器端得到的仍然是“ok.

 

 

注释1

<input ….>未加runat=”server”属性,是一个普通HTML元素,即客户端控件,它有一个属性叫”onclick”;不同于 <asp:….>web服务器控件,web控件对应的属性是”OnClick”,是针对服务器端的。还有html中的值属性名为value,而对应的web中的值属性名为Text

 

注释2

responseASP的一个内置对象,write是该对象的方法,用於将指定的字符串信息输出到客户端. response.writeASP文件中,一般这样使用: 1.<%response.write("输出内容")%> 2.<%response.write("输出内容")%>相当於<%=输出内容%>

 

要解决这个问题,需要在别的方法来实现,举例如下:

(两者的区别在于定义input控件时没有直接定义其属性ReadOnlytrue,而是在Page_Load()的方法里用Attributes.Add()来添加属性)。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default9.aspx.cs" Inherits="Default9" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    <asp:TextBox ID="TextBox1" runat="server">Ok</asp:TextBox>&nbsp;

        <input id="Button2" type="button" value="修改数据为summer_grass" onclick="TextBox1.value='summer_grass'" />

        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="提交" />

    </div>

    </form>

</body>

</html>

 

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

 

public partial class Default9 : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        TextBox1.Attributes.Add("readonly","true");

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        Response.Write(TextBox1.Text);

    }

}

 

具体为什么通过这两种方法可以实现不同的效果呢,我还不明白。