比较郁闷的textbox(asp.net 2.0)控件

news/2024/7/4 13:00:26

今天转一个asp.net程序从vs2003到vs2005,老报错,postback后取不到textbox控件的改变的值,在vs2003下完全正常,在vs2005下就是不行,搞了一上午都不知为啥,于是上网查查,原来是textbox控件的readonly属性做的怪,真的郁闷.asp.net2.0 的这种改动让人无法理解。解决方法详见如下:

(http://blog.joycode.com/saucer/archive/2006/05/11/75741.aspx)

有时候,我们不希望用户直接编辑TextBox,而是希望通过客户端脚本的方式来设置内容,一般的做法是设置TextBox的属性ReadOnly为true。但在ASP.NET 2.0里有了变化,设置了ReadOnly为true的TextBox,在服务器端不能通过Text属性获取在客户端设置的新内容,在Reflector里比较一下LoadPostData的实现

.NET 1.1中,

bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
{
      string text1 = this.Text;
      string text2 = postCollection[postDataKey];
      if (!text1.Equals(text2))
      {
            this.Text = text2;
            return true;
      }
      return false;
}

.NET 2.0中,

protected virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)
{
      base.ValidateEvent(postDataKey);
      string text1 = this.Text;
      string text2 = postCollection[postDataKey];
      if (!this.ReadOnly && !text1.Equals(text2, StringComparison.Ordinal))
      {
            this.Text = text2;
            return true;
      }
      return false;
}

就可以看出,如果设置了ReadOnly为true,从客户端传回的新的值是不被设置到Text属性的。

想要保持.NET 1.*中的行为,建议的做法是设置客户端属性ContentEditable=false,参考

SYSK 118: ReadOnly or ContentEditable?
http://blogs.msdn.com/irenak/archive/2006/05/03/589085.aspx


 

SYSK 118: ReadOnly or ContentEditable?
Consider this:  you want a text box on a web page to be not editable by the user,
but you want to be able to change the text box’s contents in client side script
and see the updated text on the server.

Did you know that if you set TextBox1.ReadOnly = true, the value
set by the client side script will not be visible on the server? 
  Try it for yourself… Here is the code:

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

    <div>

        <input id="Button2" type="button"
value="Change Text via Client-Side Script" οnclick="ChangeText();" />

    </div>

    <asp:TextBox ID="TextBox1" runat="server">initial text</asp:TextBox>

    <asp:Button ID="Button1" runat="server"
OnClick="Button1_Click" Text="See Value on the Server-Side" />

</form>

<script language="javascript" type="text/javascript">

<!--

function ChangeText()

{   

    form1["TextBox1"].setAttribute("innerText", "abc");

}   

-->   

</script>

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

{

    protected void Page_Load(object sender, EventArgs e)

    {           

        TextBox1.ReadOnly = true;

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        Response.Write(TextBox1.Text + "<br>");

    }

}

However, if instead of setting TextBox1.ReadOnly property
you set ContentEditable attribute to false, you’ll get the behavior you’re looking for:

TextBox1.Attributes["contentEditable"] = "false";

Special thanks to Vinay Kumar Baliyan who replied
with the information used in this post to Les Cardinal‘s question.

其实如果是设置客户端属性的话,设置客户端的readonly属性应该也是可以的:

TextBox1.Attributes["readonly"] = "true";


http://www.niftyadmin.cn/n/3693613.html

相关文章

JavaScript本地存储的几种方式?区别和应用场景?cookie,localstorage,sessionStorage

参考这个 1.相同点是都是保存在浏览器端、且同源的 2.cookie数据始终在同源的http请求中携带&#xff08;即使不需要&#xff09;&#xff0c;即cookie在浏览器和服务器间来回传递&#xff0c;而sessionStorage和localStorage不会自动把数据发送给服务器&#xff0c;仅在本地…

遍历页面上的所有控件

/// /// 采用递归的方法来遍历页面控件 /// /// protected void ErgodicChildrenControls(Control parent){foreach (Control c in parent.Controls){//此处写有关控件的代码&#xff0c;例如 //if (c is HtmlInputButton) // { // HtmlInputButt…

JS办法处理CheckBoxList生成的br

昨天在项目发现CheckBoxList 也有些问题RepeatDirection"Horizontal" <asp:CheckBoxList ID"CheckBoxList" runat"server" RepeatLayout"Flow" RepeatColumns"5" RepeatDirection"Horizontal" CellSpacing&q…

创建控件时出错/lm/w3svc/... 不是一个有效的iis应用程序 的解决办法

环境&#xff1a;vs2005&#xff0c;asp.net web 应用程序&#xff08;项目&#xff09;&#xff0c;IIS作为发布服务器 导致原因&#xff1a;是项目的属性配置中&#xff0c;“服务器”的设置中与IIS中的虚拟目录不一致导致。 解决办法&#xff1a;按项目属性的中的“服务器”…

onscroll事件导致flash叠影效果--setTimeout的妙用

飘动对联广告加到页面中&#xff0c;出现flash有叠影的效果&#xff0c;最终原因是因为onscroll事件的问题&#xff0c;加了一个setTimeout 延时解决问题了原先&#xff1a;function scall(){.......}window.οnresizescall;window.οnlοadscall;现在&#xff1a;function sca…

无限分级数量查询优化

无限分级的数据查询是个头痛的问题&#xff0c;递归查询类别&#xff0c;再组合成字符串&#xff0c;用 in 来解决子类所有产品的问题&#xff0c;但是这个效率太低&#xff0c;低的让人无法接受&#xff0c;在此&#xff0c;有一个SQL的方法&#xff0c;可让我们提高效率。---…

Asp.Net Web项目打包

1、打开你的项目&#xff0c;在<解决方案管理器>中用鼠标右击你的<解决方案>&#xff0c;选择<添加>-<新建项目>。2、<添加新项目>对话框中选择<安装和部署项目>-<web安装项目>。&#xff08;注意&#xff1a;<web安装项目>的…