通过页面外刷新直接从java脚本中点击按钮调用asp.net函数

本文关键字:按钮 调用 函数 net asp 脚本 java 刷新 | 更新日期: 2023-09-27 18:27:39

我正在尝试检查数据库中是否存在工作正常但我想要的是的特定值

  1. 我想从JavaScript调用一个C#函数
  2. 在我的代码中,JavaScript点击按钮后它会做出响应,但是页面也会刷新我不希望出现页面刷新
  3. 用javascript 中的out按钮调用C#函数的更好方法

    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按钮

 <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()

通过页面外刷新直接从java脚本中点击按钮调用asp.net函数

如注释中所述,您可以使用jquery-ajax轻松完成此操作。为此,您必须定义一个类似于以下内容的WebMethod:-

请包括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 
}

最后,从你的客户端这样称呼它:-

$("#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); }
    });
});

此外,还包括jquery基本库文件。

将其作为一个web方法,并使用ajax从jquery调用该方法。请找到下面的链接,它包含了使用jquery ajax在asp.net中调用web方法的示例。http://www.aspsnippets.com/Articles/Calling-ASPNet-WebMethod-using-jQuery-AJAX.aspx

希望这能有所帮助!

使用Jquery ajax方法或使用UpdatePanels

将方法Check_exam_id创建为public static,并将其装饰为[WebMethod]并在按钮的点击事件上调用javascript函数,就像调用任何其他函数一样。

    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');
            }
         });
        }
$("#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); }
    });
});