【问题标题】:On ASP.NET textbox losing property after PostBack occursPostBack 发生后,在 ASP.NET 文本框上丢失属性
【发布时间】:2014-07-09 18:55:25
【问题描述】:

在我的 .aspx 页面上,我想要禁用某些字段,因此我使用 carvedinstone 属性标记这些字段,并在代码隐藏中动态应用 disabled="disabled",因此用户无法更改预填充的值。 注意: 这是 10 多年前的遗留代码,所以“为什么?”只能用“这是 2004 年,其他人这样做了,耸耸肩”来回答。

在验证期间发生 PostBack 时,disabled="disabled" 属性将从拥有它的字段中删除。我想防止这种情况发生。

.aspx 页面

<asp:TextBox
    ID="fEE_SSN"
    runat="server"
    alias="fSSN"
    prefix="[0]"
    strip="-"
    stripfor="both"
    width="160px"
    maxlength="11"
    carvedinstone="thirdparty" />

.aspx 页面还具有必填字段和正则表达式验证器:

<asp:RequiredFieldValidator
    ID="rfv_fSSN"
    runat="server"
    display="Dynamic"
    errormessage="Please enter the Employee's SSN"
    controltovalidate="fEE_SSN"
    cssclass="error"
    enabled="false">[*]</asp:RequiredFieldValidator>

<asp:RegularExpressionValidator
    ID="rev_fSSN"
    runat="server" 
    display="Dynamic"
    errormessage="Please enter a valild SSN"
    controltovalidate="fEE_SSN"
    validationexpression="\d{3}-\d{2}-\d{4}"
    cssclass="error"
    enabled="false">[#]</asp:RegularExpressionValidator>

应用disabled="disabled" 的函数正在Page_Load 处调用。 Page_Load 调用 validateValidators() 方法,该方法调用发生禁用字段的验证检查:

Page_Load() 方法

protected virtual void Page_Load(object sender, System.EventArgs e)
{
    SimpleTracer.Write(MethodBase.GetCurrentMethod().Name + " Begin");
    EnsureChildControls();
    validateValidators();
}

validateValidators() 方法

protected virtual void validateValidators()
{
    foreach (BaseValidator validator in Validators)
    {
        if (!validator.Enabled)
            continue; // validator has already been disabled

        bool enable = validateValidator(validator, form);
        validator.Enabled = enable;
    }
}

validateValidator() 方法

protected virtual bool validateValidator(BaseValidator validator, Control parent)
{
    WebControl control = null;

    if (validator is CustomValidator)
    {
        string controlId = validator.Attributes["checkboxgroupprefix"];
        if (!String.IsNullOrEmpty(controlId))
            control = parent.FindControl(controlId + "1") as WebControl;
        else
            control = parent.FindControl(validator.ControlToValidate) as WebControl;

        if (control != null)
        {
            if (control.GetType().Name == "TextBox")
            {
                if (CheckCarvedInStoneControl(control))
                    return false;
            }
            if (control.GetType().Name == "RequiredFieldValidator")
            {
                if (CheckCarvedInStoneValidator(control))
                    return false;
            }
            if (IsReadOnlyForSubmissionType(control, m_SubmissionType))
                return false;
        }

        if (control == null && parent.HasControls())
        {
            foreach (Control child in parent.Controls)
                if (!validateValidator(validator, child))
                    return false;
        }
        return true;
    }

如果有人对如何在 PostBack 后保留该属性有任何想法,我很乐意听取您的意见。谢谢大家!

【问题讨论】:

  • 为什么不用TextBox自带的属性Enabled = false呢?为什么要如此精心地禁用?
  • @YuriyGalanter 我能给出的唯一答案是,当时有人觉得在代码隐藏中做这件事是一个更好的解决方案。
  • 所以在后面的代码中分配 fEE_SSN.Enabled = false; 而不是 fEE_SSN.Attributes["disabled"] = "disabled"; 这样它将在回发之间持续存在
  • @YuriyGalanter 这就是 disabled="disabled" 属性被应用到字段的方式,在代码隐藏中执行检查:(webControl as TextBox).Enabled = false;
  • 重新阅读问题后,很明显我的答案很离谱。但我很好奇,为什么他们不保存,他们真的应该保存。您在页面生命周期的哪个时间点更新 disabled 属性?

标签: c# asp.net postback viewstate


【解决方案1】:

如果Page_Load 方法中的代码如下所示:

protected void Page_Load(object send, EventArgs e)
{
    if (!PostBack)
    {
        // ... some unknown code here
        (webControl as TextBox).Enabled = false;
        // ... some unknown code here
    }
    // ... some unknown code here
}

然后尝试将设置.Enabled 属性的代码移到PostBack 检查之外:

protected void Page_Load(object send, EventArgs e)
{
    if (!PostBack)
    {
        // ... some unknown code here
    }
    // ... some unknown code here
    (webControl as TextBox).Enabled = false;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-01
    • 2019-10-10
    • 1970-01-01
    • 1970-01-01
    • 2013-07-18
    • 2011-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多