Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

ASP.Net – How to control client-side functionality based on triggered event (i.e. What button was clicked)

Let’s say you have a scenario that whenever a button event or any event for the matter is raised on the client-side you want to display text in a label on the screen that says something like, “PROCESSING… PLEASE WAIT.” At the end of processing, you then would clear out the text to that label at the end of the server-side event routine.

To do this on the client-side, you add a handler for the OnClientClick event, let’s say for a SEARCH button like this:

<asp:Button ID="btnSearch" runat="server" Text="Search" Width="81px"  
    OnClientClick="SetProcessLabelFromClient" TabIndex="14" 
    CausesValidation="False"/>

At the top of your aspx, you then add the javascript like this:

function SetProcessLabelFromClient
{
  document.getElementById('<%= txtProcessing.ClientID %>').value = 'PROCESSING.... PLEASE WAIT.';
}

This works fine and all, but what if you have a SAVE button and you are using validation controls? The problem? Your label says, “PROCESSING… PLEASE WAIT.” and won’t go away because your…let’s say… telephone textbox error validation says “INVALID FORMAT”. The page never got submitted to the server in order to clear the label.

The solution? Pass the button control ID to the javascript like this:

<asp:Button ID="btnDSave" runat="server" Text="Save" Width="81px"  
    OnClientClick="SetProcessLabelFromClient(this.id)" TabIndex="30" 
    ValidationGroup="DetailGroup" UseSubmitBehavior="False" />

The difference is your are now passing the script a parameter called, “this.id”

Last but not least, modify your javascipt to except the parameter and interrogate whether you are dealing with the SAVE button. If the SAVE button, then execute the Page_ClientValidate method. If the page is valid, set the label. If not valid, then don’t set it. If the button is not SAVE, then set the label.

The modified script looks like this:

function SetProcessLabelFromClient(tid)
  {
    if (tid == "btnDSave")
       {
         Page_ClientValidate();
         if (Page_IsValid)
             document.getElementById('<%= txtProcessing.ClientID %>').value = 'PROCESSING.... PLEASE WAIT.';
    } else 
    {
        document.getElementById('<%= txtProcessing.ClientID %>').value = 'PROCESSING.... PLEASE WAIT.';
    }
  }

Leave a Reply