简体   繁体   中英

On ASP.NET textbox losing property after PostBack occurs

On my .aspx page I have certain fields that I want disabled so I flag these with a carvedinstone property and dynamically apply disabled="disabled" in the codebehind so the pre-populated value can't be changed by a user. Note: This is legacy code 10+ years old so the "Why's?" can only be answered with "It was 2004, someone else did it this way, shrug".

When a PostBack occurs during validation the disabled="disabled" property is removed from the fields that had it. I want to prevent that from happening.

.aspx page

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

The .aspx page also has required field and regular expression validators:

<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>

The function for applying disabled="disabled" is being called at Page_Load . Page_Load calls the validateValidators() method which calls the validation check where the disabling of the field happens:

Page_Load() method :

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

validateValidators() method :

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() method :

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

If anyone has any ideas how to preserve that property after PostBack I'd love to hear from you. Thanks, all!

If the code inside your Page_Load method looks something like this:

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
}

then try moving the code that sets the .Enabled property outside of the PostBack check:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM