尝试使用ASP隐藏值和javascript禁用CSS按钮

时间:2014-05-21 15:52:44

标签: javascript asp.net

我正在使用ASP.NET PasswordRecovery方法,并有几个CSS按钮来提交表单或取消请求。

我还抓取用户电子邮件地址传递给PasswordRecovery方法的successtext值,将ASP hiddenvalue设置为成功,然后尝试使用javascript根据此值禁用按钮。

问题是这个隐藏字段值似乎在初始页面加载时设置为“成功”,即使在查看呈现页面的源时显示NO值。

可能有更好的方法,但我尝试了几种不同的方法,这是成功的,我无法改变按钮的视图状态。

  
<script runat="server">
 protected void resetuserpassword_SendingMail(object sender, MailMessageEventArgs e)
{
    e.Message.IsBodyHtml = true;
    e.Message.Subject = "Password Assistance";
    TextBox txtUserName = (TextBox)resetuserpassword.UserNameTemplateContainer.FindControl("UserName");
    string UserEmailID = Membership.GetUser(txtUserName.Text.Trim()).Email;
    resetuserpassword.SuccessText = "Password sent to ";
    resetuserpassword.SuccessText += UserEmailID;
    ValueHiddenField.Value = "Success";

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolderArea" Runat="Server">
    <script type="text/javascript">
    window.onload = disableButtons;
    function disableButtons() {
        var element =  document.getElementById('ContentPlaceHolderArea_ValueHiddenField');
        if (typeof(element) != 'undefined' && element != null) {
                if (document.getElementById('ContentPlaceHolderArea_ValueHiddenField').value = 'Success') {
                    var submitBtnElement = document.querySelector("#submitBtn");
                    var cancelBtnElement = document.querySelector("#cancelBtn");
                    submitBtnElement.style.display = "none";
                    cancelBtnElement.style.display = "none";
                }
        }
    }
    function clickSubmit() {
        document.getElementById("ContentPlaceHolderArea_resetuserpassword_UserNameContainerID_SubmitButton").click();
    }
    function clickCancel() {
        window.location.replace("~/Login.aspx");
    }
</script>
    <asp:hiddenfield id="ValueHiddenField"    value=""    runat="server"/>
    <asp:hiddenfield id="ValueHiddenField"    value=""    runat="server"/>
    <asp:PasswordRecovery ID="resetuserpassword" runat="server" 
        MailDefinition-BodyFileName="~/ResetPasswordEmailTemplate.html" 
        OnSendingMail="resetuserpassword_SendingMail" 
        successtext="Password sent to email address on record."
        Width="300px" Font-Names="Arial" Font-Size="Small" UserNameTitleText="" >
        <InstructionTextStyle Font-Names="Arial" Font-Size="Small" />
        <MailDefinition BodyFileName="~/ResetPasswordEmailTemplate.html"></MailDefinition>

        <UserNameTemplate>
            <div><asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName" style="display:none;"></asp:Label></div>
            <div style="font: arial, verdana, sans-serif;font-size: 13px;padding-bottom: 5px;font-weight: bold;">Please Enter your Username</div>
            <div><asp:TextBox ID="UserName" runat="server" style="width: 180px;"></asp:TextBox></div>
            <div><asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="PasswordRecovery1">*</asp:RequiredFieldValidator></div>
            <div><asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal></div>
            <div style="display: inline;" ><asp:Button ID="SubmitButton" runat="server" CommandName="Submit" Text="Submit" ValidationGroup="PasswordRecovery1" style="display: none;" /></div> 
        </UserNameTemplate>
    </asp:PasswordRecovery>
        <div>
        <div id="submitBtn" onclick="clickSubmit()">Submit</div>
        <div id="cancelBtn" onclick="clickCancel()">Cancel</div>    
    </div>
</asp:Content>

1 个答案:

答案 0 :(得分:1)

您的javascript if语句正在分配值&#39;成功&#39;到隐藏场(单等号)。

if (document.getElementById('ContentPlaceHolderArea_ValueHiddenField').value = 'Success') {
...
}

将其更改为此(双等号):

if (document.getElementById('ContentPlaceHolderArea_ValueHiddenField').value == 'Success') {
...
}