Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6197003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:44:39+00:00 2026-05-24T03:44:39+00:00

I have an ASP.Net-Button which, when clicked, executes client side and server side code.

  • 0

I have an ASP.Net-Button which, when clicked, executes client side and server side code.
Under certain conditions, the execution of the latter should be prevented.

<asp:LinkButton OnClientClick="if(CheckItems() == false) return false;" runat="server" 
       ID="Button" onclick="Button_Click">Insert</asp:LinkButton>

The method CheckItems calls a web-service. If the response from the web-service is “DataFound”, the method CheckItems should return false.

function CheckItems() {

        PageMethods.CheckItems($('#<%= txtField.ClientID  %>').val(), function(response) {

            if (response == "DataFound") {
                alert("The text you entered does already exist.");
                return false;
            }
        });
    }

With this code, CheckItems does not return false. How can this be achieved?

The web-method:

[WebMethod]
    public static string CheckItems(string name)
    {
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CS"].ConnectionString);

        try
        {
            conn.Open();

            var selectCommand = conn.CreateCommand();

            selectCommand.CommandText = @"SELECT COUNT(*) FROM [Table] WHERE Name = @Name";
            selectCommand.Parameters.Add(new SqlParameter("Name", name));

            int results = (int)selectCommand.ExecuteScalar();

            if (results > 0)
                return "DataFound";
            else
                return "NoDataFound";
        }
        finally
        {
            conn.Close();
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:44:40+00:00Added an answer on May 24, 2026 at 3:44 am

    Since your javascript function is making an asynchronous call to the server, it cannot return the result immediately to your click event. You’ll need to separate out your stuff into separate javascript functions, like:

    <a onclick="buttonClicked();">Insert</a>
    
    function buttonClicked() {
        PageMethods.CheckItems($('#<%= txtField.ClientID  %>').val(), function(response) {
            if (response == "DataFound") {
                alert("The text you entered does already exist.");
            } else {
                performPostback();
            }
        });
    }
    
    function performPostback() {
        // Actually do your form post, maybe using __doPostBack
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the javascript code below which disables an asp.net button during page postback.
I have an ASP.NET page which has a button it it. The button click
I have an asp.net server control (with the asp: in its definition). The button
I have a button on an ASP.NET wep application form and when clicked goes
In my ASP.Net page, i have a few link button controls which are placed
I have a ASP.NET page with an asp:button that is not visible. I can't
I have an asp.net page with a button. This button generates and inserts a
I have an ASP.NET MVC application, when a user clicks on the submit button
I have a button on an ASP.Net page that will call Response.Redirect back to
I have to display yes/no button on some condition on my asp.net page ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.