对设置成ReadOnly的文本框,JS无法改变其值

对设置成ReadOnly的文本框,后台无法取得JS设置的值

究其原因,从MSDN中得知

"发生回发时,TextBox 控件(其 ReadOnly 属性设置为 true)的 Text 值被发送到服务器,但是服务器不处理只读文本框。这样可以防止恶意用户更改只读的 Text 值。在回发之间,Text 属性的值保留在视图状态中,除非经过服务器端代码修改。"


看看(Button的源代码):

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


解决方法是用JS设置文本框为ReadOnly

Jquery:

$('#txtEPullDate').attr("readonly", "readonly");

这样后台便可取到了

你可能感兴趣的:(服务器,button,textbox)