开发者

Disabled button refreshes

开发者 https://www.devze.com 2023-03-31 03:27 出处:网络
I Have an aspx page that contains 2 buttons \"Update\" and \"Default\". I have a dropdown list which contains a few values, say 1 to 10. When I click on Default button, the dropdown is set to a defaul

I Have an aspx page that contains 2 buttons "Update" and "Default". I have a dropdown list which contains a few values, say 1 to 10. When I click on Default button, the dropdown is set to a default value, say 4. If I wish to set the dropdown value to 3, I choose 3 and click on Update button and the changes are saved somewhere, maybe a DB.

Initially, Update button is disabled. Only if any changes are made to the dropdown, the Update button is enabled. Assuming that the Update button is initially disabled, I click the Default button to set the dropdown to its initial value. When I do that, a postback happens during which the Update button suddenly becomes enabled and then disabled. How do I avoid this? During page refresh, I don't want the disabled Update button to become enabled and then disabled. ALl this happens in a millisecond but its still visible.

is there any way out of this?

Design code is as follows: ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LogSettings.aspx.cs" Inherits="Settings_LogSettings" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Diagnostic Server Configuration Tool</title>
    <link rel="stylesheet" type="text/css" href="../css/style001.css" />
    <style type="text/css">
        a.info
        {
            position: relative; /*this is the key*/
            z-index: 24; /*background-color:#ccc;*/
            color: #000;
            border-width: 0px;
            border-style: none;
            text-decoration: none;
        }

        a.info:hover
        {
            z-index: 25;
            background-color: #ff0;
        }

        a.info span
        {
            display: none;
        }

        a.info:hover span
        {
            /*the span will display just on :hover state*/
            display: block;
            position: absolute;
            bottom: 2em;
            right: 2em;
            width: 15em;
            border: 1px solid #0cf;
            background-color: #cff;
            color: #000;
            text-align: left;
            padding: 5px;
        }
    </style>
    <script language="javascript" type="text/javascript">
        function setDefaults() {
            if (document.getElementById("dlLoggingLevel").value != document.getElementById("dlLoggingLevel_Def").value) {
                document.getElementById("dlLoggingLevel").value = document.getElementById("dlLoggingLevel_Def").value;
                document.getElementById("imgLoggingLevel").src = "../images/field_ok.png";
                document.getElementById("imgLoggingLevelUndo").style.display = "inline";
                document.getElementById("btnUpdate").disabled = false;
            }
            if (document.getElementById("txtMaxFileSize").value != document.getElementById("txtMaxFileSize_Def").value) {
                document.getElementById("txtMaxFileSize").value = document.getElementById("txtMaxFileSize_Def").value;
                document.getElementById("imgMaxSize").src = "../images/field_ok.png";
                document.getElementById("imgMaxSizeUndo").style.display = "inline";
                document.getElementById("btnUpdate").disabled = false;
            }
        }
        function isNumberKey(evt) {
            var charCode = (evt.which) ? evt.which : event.keyCode
            if (charCo开发者_如何学编程de > 31 && (charCode < 48 || charCode > 57))
                return false;

            return true;
        }
    </script>
    <script language="javascript" type="text/javascript" src="../Css/wcf_validate.js"></script>
</head>
<body>
    <form id="form1" runat="server">

    <div>
        <br />
        <div class="divEditHeader" id="EditHeader">
            <h1>
                Logging Configuration
            </h1>
            <table width="100%">
                <tr>
                    <td align="left">
                        <asp:Button CssClass="formEditBtn" runat="server" ID="btnUpdate" Text="Update" OnClick="btnUpdate_Click"
                            Enabled="false" />
                        <button class="formEditBtn" onclick="javascript:setDefaults();" causesvalidation="false">
                            Default</button>
                    </td>
                    <td align="right">
                    </td>
                </tr>
            </table>
        </div>
        <br />
        <table class="InputTable">
            <tr class="Prompt">
                <td class="Prompt">
                    Logging Level
                </td>
                <td>
                    <asp:DropDownList runat="server" ID="dlLoggingLevel">
                        <asp:ListItem Text="NONE" Value="none"></asp:ListItem>
                        <asp:ListItem Text="FATAL" Value="fatal"></asp:ListItem>
                        <asp:ListItem Text="ERROR" Value="error"></asp:ListItem>
                        <asp:ListItem Text="WARNING" Value="warning"></asp:ListItem>
                        <asp:ListItem Text="INFO" Value="info"></asp:ListItem>
                        <asp:ListItem Text="DEBUGLOW" Value="debuglow"></asp:ListItem>
                        <asp:ListItem Text="DEBUGMEDIUM" Value="debugmedium"></asp:ListItem>
                        <asp:ListItem Text="DEBUGHIGH" Value="debughigh"></asp:ListItem>
                        <asp:ListItem Text="DEBUGALL" Value="debugall"></asp:ListItem>
                    </asp:DropDownList>
                    <img id="imgLoggingLevel" src="../images/blank.png" />
                    <asp:TextBox runat="server" ID="dlLoggingLevel_Init" Style="display: none"></asp:TextBox>
                    <asp:TextBox runat="server" ID="dlLoggingLevel_Def" Style="display: none"></asp:TextBox>
                    <img id="imgLoggingLevelUndo" src="../images/restore.png" style="display: none; cursor: hand"
                        onmouseover="this.src='../Images/restore_hov.png'" onmouseout="this.src='../Images/restore.png'"
                        onclick="restoreValue('dlLoggingLevel','dlLoggingLevel_Init','imgLoggingLevel','imgLoggingLevelUndo')" />
                </td>
                <td>
                    <a href="javascript: void 0" class="info">
                        <img src="../images/help.png" border="0">
                        <span><font size="2">Enter the desired level of diagnostic data logging. Default: INFO.
                        </font></span></a>
                </td>
            </tr>
            <tr class="Prompt">
                <td class="Prompt">
                    Maximum Log File Size(MB)
                </td>
                <td>
                    <asp:TextBox runat="server" ID="txtMaxFileSize" Width="36px" MaxLength="3"></asp:TextBox>
                    <asp:TextBox runat="server" ID="txtMaxFileSize_Init" Style="display: none"></asp:TextBox>
                    <asp:TextBox runat="server" ID="txtMaxFileSize_Def" Style="display: none"></asp:TextBox>
                    <img id="imgMaxSize" src="../images/blank.png" />
                    <asp:CustomValidator runat="server" ID="valMaxSize" ControlToValidate="txtMaxFileSize"
                        Display="Dynamic" ErrorMessage="" ClientValidationFunction="MaxSize_Validate"></asp:CustomValidator>
                    <img id="imgMaxSizeUndo" src="../images/restore.png" style="display: none; cursor: hand"
                        onmouseover="this.src='../images/restore_hov.png'" onmouseout="this.src='../images/restore.png'"
                        onclick="restoreValue('txtMaxFileSize','txtMaxFileSize_Init','imgMaxSize','imgMaxSizeUndo')" />
                </td>
                <td>
                    <a href="javascript: void 0" class="info">
                        <img src="../images/help.png" border="0">
                        <span><font size="2">Enter the maximum log file size in MB. Default: 2 MB. Range: 1
                            - 100 MB. </font></span></a>
                </td>
            </tr>
        </table>
        <br />
        <asp:Label runat="server" ID="lblMessage" Font-Bold="true"></asp:Label>
        <br />
    </div>
    </form>
</body>
</html>

Code-Behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DiagnosticCommon;
using System.Drawing;

public partial class Settings_LogSettings : System.Web.UI.Page
{
    const string EnvVariable = "DIAGNOSTICSERVER";
    const string ConfigFileName = "DiagnosticService.exe.config";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Security.EnforceSecurity())
            Response.Redirect("Login.aspx");

        if (!IsPostBack)
        {
            DebugHelper.MaxDebugLevel = DebugHelper.Parse(ConfigReader.GetValue("LoggingLevel"));
            DebugHelper.MaxLogFileSize = long.Parse(ConfigReader.GetValue("LogFileSize"));

            txtMaxFileSize.Attributes.Add("onchange", "javascript:MaxSize_Validate('',this);");
            txtMaxFileSize.Attributes.Add("onkeypress", "return isNumberKey(event)");

            dlLoggingLevel.Attributes.Add("onchange", "javascript:Logging_Validate('',this);");

            BindData();
            BindInitData();
            BindDefaults();
        }

    }

    private void BindData()
    {
        string installPath = Environment.GetEnvironmentVariable(EnvVariable);
        try
        {
            dlLoggingLevel.SelectedValue = ConfigReader.GetValue("LoggingLevel");
            txtMaxFileSize.Text = ConfigReader.GetValue("LogFileSize");
        }
        catch (Exception ex)
        {
            lblMessage.Text += ex.Message + "<br>" + installPath;
            lblMessage.ForeColor = Color.Red;
        }

    }

    private void BindInitData()
    {
        string installPath = Environment.GetEnvironmentVariable(EnvVariable);
        try
        {
            dlLoggingLevel_Init.Text = ConfigReader.GetValue("LoggingLevel");
            txtMaxFileSize_Init.Text = ConfigReader.GetValue("LogFileSize");
        }
        catch (Exception ex)
        {
            lblMessage.Text += ex.Message + "<br>" + installPath;
            lblMessage.ForeColor = Color.Red;
        }

    }

    protected void btnCancel_Click(object sender, EventArgs e)
    { 
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    { 
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            lblMessage.Text = "";
            ConfigReader.SetValue("LoggingLevel", dlLoggingLevel.SelectedValue);
            ConfigReader.SetValue("LogFileSize", txtMaxFileSize.Text);

            lblMessage.Text = "Configuration updated.";
            lblMessage.ForeColor = Color.Green;
            btnUpdate.Enabled = false;
            BindInitData();

        }
        catch (Exception ex)
        {
            lblMessage.Text += ex.Message;
            lblMessage.ForeColor = Color.Red;
        }
    }

    private void BindDefaults()
    {
        try
        {
            dlLoggingLevel_Def.Text = ConfigReader.GetDefault("LoggingLevel");
            txtMaxFileSize_Def.Text = ConfigReader.GetDefault("LogFileSize");
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message;
            lblMessage.ForeColor = Color.Red;
            btnUpdate.Enabled = false;
        }

    }


}


Since the button has no type defined, the default type is used which is a submit button.
This means that when you click the Default button, the JS code is running but then the form is submitted.

To avoid the submission simply make the button be ordinary button:

<button type="button" class="formEditBtn" onclick="javascript:setDefaults();" causesvalidation="false">Default</button>


Following is the line in setDefaults() method that enables the update button for a while, a post back occurs and update button again disabled.

document.getElementById("btnUpdate").disabled = false;

Either comment this line or set it to true

0

精彩评论

暂无评论...
验证码 换一张
取 消