简体   繁体   English

如何在asp.net中调用WebMethod并显示警报消息

[英]How to Call WebMethod and Show Alert message in asp.net

I am trying to give an alert message using "WebMethod",where my conditions are as follows 我正在尝试使用“ WebMethod”发出警报消息,其中我的情况如下

1.I am trying to restrict the user applying leave on "Monday" when he/She has already taken Leave on the previous friday. 1.我试图限制用户在“星期一”申请休假,因为他/她已经在上一个星期五休了假。 2.I am geeting the details from my Database where the employees leave Details and trying to code this in WebMethod 2.我正在从员工保留“详细信息”的数据库中收集详细信息,并尝试在WebMethod中进行编码

My cs page code: 我的CS页面代码:

   [System.Web.Services.WebMethod]
public  string GetCurrentTime()
{
    SqlConnection con = new SqlConnection(conString);
    con.Open();
    SqlCommand cn = new SqlCommand();
    DateTime date3 = System.DateTime.Now;
    DateTime date4 = System.DateTime.Now;
    DateTime date1 = System.DateTime.Now.AddDays(-6); ;
    DateTime date2 = System.DateTime.Now.AddDays(-6);
    DateTime.TryParse(txtFromDate.Text, out date1);
    DateTime.TryParse(txtToDate.Text, out date2);

   // string val;
   // var EmpID = "SS212";
    SqlDataAdapter da = new SqlDataAdapter(scmd);
        DataTable dt=new DataTable();
        da.Fill(dt);
        sdr = scmd.ExecuteReader();
    if (date1.DayOfWeek == DayOfWeek.Monday && date2.DayOfWeek == DayOfWeek.Monday)
    {
        string Leave = "Select EmpID ,LeaveType,LeaveFromDate,LeaveToDate,LeaveStatus from LeaveApplication Where LeaveFromDate  = '" + date1 + "' and LeaveToDate  = '" + date2 + "'";
        scmd = new SqlCommand(Leave, scon);



    }
    for(int i = 0; i < dt.Rows.Count; i++)
    {

        String value ;
        if ((dt.Rows[i]["LeaveStatus"].ToString() == "Accepted") || (dt.Rows[i]["LeaveStatus"].ToString() == "Pending")) 

    {
      value="";

    }
        else 
    {

        value = "";
    }

    }


    return "";       
}

My aspx: 我的aspx:

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    function ShowCurrentDate() {

        $.ajax({
            type: "POST",
            url: "LMSEmployee.aspx/GetCurrentTime",
            data: params,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function (response) {
                alert("Please");
            }
        });
    }
    function OnSuccess(response) {
        alert("Please");

    }
    </script>

Webmethod works only with static functions so try Webmethod仅适用于静态函数,因此请尝试

public static  string GetCurrentTime()

instead of 代替

public  string GetCurrentTime() 

note that if you do so then event driven features which usually comes in event driven functions(IE server side click event) wont work 请注意,如果您这样做,则通常在事件驱动功能(IE服务器端单击事件)中附带的事件驱动功能将无法工作

there is an awesome post by Mike at 迈克(Mike)有一个很棒的帖子

https://www.mikesdotnetting.com/article/104/many-ways-to-communicate-with-your-database-using-jquery-ajax-and-asp-net https://www.mikesdotnetting.com/article/104/many-ways-to-communicate-with-your-database-using-jquery-ajax-and-asp-net

example //just a car class 示例//只是汽车课

 public class Cars
    {
        public string carName;
        public string carRating;
        public string carYear;
    }


//Your Webmethod*
[WebMethod]
public List<Cars> getListOfCars(List<string> aData)
{
    SqlDataReader dr;
    List<Cars> carList = new List<Cars>();

    using (SqlConnection con = new SqlConnection(conn))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "spGetCars";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = con;
            cmd.Parameters.AddWithValue("@makeYear", aData[0]);
            con.Open();
            dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    string carname = dr["carName"].ToString();
                    string carrating = dr["carRating"].ToString();
                    string makingyear = dr["carYear"].ToString();

                    carList.Add(new Cars
                                    {
                                        carName = carname,
                                        carRating = carrating,
                                        carYear = makingyear
                                    });
                }
            }
        }
    }
    return carList;
}
//*

//Your Client Side code
    $("#myButton").on("click", function (e) {
        e.preventDefault();
        var aData= [];
        aData[0] = $("#ddlSelectYear").val(); 
        $("#contentHolder").empty();
        var jsonData = JSON.stringify({ aData:aData});
        $.ajax({
            type: "POST",
            //getListOfCars is my webmethod   
            url: "WebService.asmx/getListOfCars", 
            data: jsonData,
            contentType: "application/json; charset=utf-8",
            dataType: "json", // dataType is json format
            success: OnSuccess,
            error: OnErrorCall
        });

        function OnSuccess(response) {
          console.log(response.d)
        }
        function OnErrorCall(response) { console.log(error); }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM