簡體   English   中英

在ASP.NET文本框中發生回發后丟失屬性

[英]On ASP.NET textbox losing property after PostBack occurs

在我的.aspx頁面上,我需要禁用某些字段,因此我使用了carvedinstone屬性對其進行了標記,並在carvedinstone動態應用了disabled="disabled" ,因此用戶無法更改預填充的值。 注意: 這是已有10多年歷史的舊代碼,因此“為什么?” 只能用“那是2004年,有人以這種方式聳了聳肩”來回答。

在驗證過程中發生回發時,將從具有該屬性的字段中刪除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>

Page_Load調用了應用disabled="disabled"的功能。 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;
    }

如果有人在回發后對如何保留該財產有任何想法,我希望收到您的來信。 謝謝大家!

如果您的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屬性的代碼.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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM