c# - ReadOnly=true 和 TextBox1.Attributes.Add ("readonly", "readonly") 在 ASP.NET 中有什么区别?

标签 c# asp.net

我有一个只读的文本框,但想回发它的值。之前我将代码设置为:

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

它不工作。我删除了 ReadOnly="true" 并在代码中添加了以下行

txtContractStartDate.Attributes.Add("readonly", "readonly");

为什么它开始回发数据?

最佳答案

ReadOnly 属性设置为 true includes the attribute being set :

if (ReadOnly) {
    writer.AddAttribute(HtmlTextWriterAttribute.ReadOnly, "readonly");
}

但是,该属性也会影响回发行为。它只会在 not readonly 时将文本保存在 View 状态中(原始代码缩短):

private bool SaveTextViewState {
    get {
        if (TextMode == TextBoxMode.Password) {
            return false;
        }
        if (Events[EventTextChanged] != null || !IsEnabled || !Visible || (ReadOnly) || this.GetType() != typeof(TextBox)) {
            return true;
        }
        return false;
    }
}

并且控件也只尝试to restore it在这种情况下:

protected virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection) {
    // …

    if (!ReadOnly && !current.Equals(postData, StringComparison.Ordinal)) {
        Text = postData;
        return true;
    }
    return false;
}

关于c# - ReadOnly=true 和 TextBox1.Attributes.Add ("readonly", "readonly") 在 ASP.NET 中有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36007304/

相关文章:

c# - MVC 6 中的 POCO Controller

带有javascript问题的asp.net文本框

javascript - ScriptManager.RegisterStartupScript 代码不起作用 - 为什么?

c# - 如何在 .NET 中修复 'Authentication to host for user using method ' mysql_native_password' failed with message : Reading from the stream has failed.'

c# - 简单的一般性误解

没有强制转换的 C# 隐式转换

c# - 在 finally block 中尝试

c# - jsreport - C# 中的访问被拒绝消息

asp.net - 适用于所有网络浏览器的 CSS 渐变

asp.net - ConfigurationManager.AppSettings 缓存