在PostBack发生后,ASP.NET文本框丢失属性

时间:2014-07-09 18:55:26

标签: c# asp.net postback viewstate

在我的.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之后保留该属性,我很乐意听取您的意见。谢谢,全部!

1 个答案:

答案 0 :(得分:0)

如果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;
}