1

I am trying to check if the specific value exist in the data base that is working perfect but what i want is

  1. I want to call a C# function from JavaScript
  2. as now in my code after JavaScript clicks button it response but page also refreshes i don't Want page refresh to be happen
  3. better way to call C# function with out using button from javascript

    protected void Check_exam_id(object sender, EventArgs e)
    {         
        string DDSelected_Class = DD_class.SelectedValue;// store it in some variable;
        string DD__Method = DD_Method.SelectedValue;// store it in some variable;
    
        if (DD__Method == "THEORY")
    
            {
        using (MySqlConnection myConnection = new MySqlConnection(constr))
        {
            string oString = "Select * from score_master WHERE Class=@DDSelected_Class ";
            MySqlCommand oCmd = new MySqlCommand(oString, myConnection);
            oCmd.Parameters.AddWithValue("@DDSelected_Class", DDSelected_Class);
    
            myConnection.Open();
            using (MySqlDataReader oReader = oCmd.ExecuteReader())
            {
    
                if (oReader == null || !oReader.HasRows)
                {
                    ScriptManager.RegisterStartupScript(this, typeof(Page), "alert", "alert('No Student Found')", true);
    
    
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, typeof(Page), "alert2", "alert('Exist')", true);
    
                    myConnection.Close();
                }
            }
    
        }
    
    
        }
    
    }
    

asp button

 <asp:Button runat="server" id="Check_examid"  AutoPostback = "false" onclick='Check_exam_id'   style="  display:none;   float: right;    width: 22%;    height: 41px;    text-align: center;    background: #EAEAEA;    border: none;    border-color: #EAEAEA;    margin-top: 2%;" Text="Submit"></asp:Button>

Javascript

    document.getElementById("BodyHolder_Check_examid").click()
2
  • 2
    Make an ajax call to your method using jquery ajax. Commented Dec 22, 2015 at 10:06
  • @RahulSingh am not sure about it if u give me an example that would be more helpfull Commented Dec 22, 2015 at 10:06

4 Answers 4

2

As mentioned in the comment, you can easily do it with jquery ajax. For this you will have to define a WebMethod something like this:-

Please include using System.Web.Services;.

[WebMethod]
public static bool Check_exam_id(string className, string MethodName)
{
    bool examIdExist = false;
    if (DD__Method == "THEORY")
    {
        \\Your logic here
    }
    \\Based on DB operation return value
    examIdExist 
}

Finally call it from your client side like this:-

$("#Check_examid").click(function (e) {
    e.preventDefault(); //To avoid postback
    //Get these values from dropdwon
    var className = $("#DD_class").val();
    var methodName = $("#DD_Method").val();
    var data = { "className": className , "MethodName": methodName };  
    $.ajax({
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        url: "Default.aspx/Check_exam_id",
        data: JSON.stringify(data),
        success: function (response) {
            if(response.d)  //If method returned true.
               alert('Exist');
            else
               alert('No Student Found')
        },
        error: function (msg) { alert(msg.d); }
    });
});

Also, include jquery base library file.

Sign up to request clarification or add additional context in comments.

10 Comments

Thnaks for the reply [WebMethod] there is a error it says the name space WebMethod does not exist
@Shaik - Please include using System.Web.Services; in your file.
I did that is working fine i have a quick question the string className if i use it then it is showing me an error
@Shaik - What error? Are you passing the value from your dropdown as parameters as I have shown?
Passing From dropdown it is, Check_exam_id giving me the error not all code path returns the value string, string,string,string,
|
2

Make it as a webmethod and call the method from jquery using ajax. Please find the below link, it contains the example for calling webmethods in asp.net using jquery ajax. http://www.aspsnippets.com/Articles/Calling-ASPNet-WebMethod-using-jQuery-AJAX.aspx

Hope this helps!

Comments

0

Use Jquery ajax method or use UpdatePanels

Create your method Check_exam_id as public static and decorate it as [WebMethod] And call the javascript function on click event of button as you call any other function.

    function DoWork(){
     $.ajax({
          url: 'YourPage.aspx/Check_exam_id',
          data: {}, //blank since you are sending nothing
          dataType:'JSON',
          type: 'POST',
          contentType:'application/json;charset=utf-8',
          success:function(){
          alert('Success');
          //Do whatever you want with the HTML
          },
         error:function()
         {
          alert('Error');
            }
         });
        }

Comments

-1
$("#Check_examid").click(function (e) {
    e.preventDefault(); //To avoid postback
    //Get these values from dropdwon
    var className = $("#DD_class").val();
    var methodName = $("#DD_Method").val();
    var data = { "className": className , "MethodName": methodName };  
    $.ajax({
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        url: "Default.aspx/Check_exam_id",
        data: JSON.stringify(data),
        success: function (response) {
            if(response!=null)  //If method returned true.
               alert('Exist');
$("DivId").html(response.d)
            else
               alert('No Student Found')
        },
        error: function (msg) { alert(msg.d); }
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.