【问题标题】:How to Call WebMethod and Show Alert message in asp.net如何在 asp.net 中调用 WebMethod 并显示警报消息
【发布时间】:2017-12-19 02:06:33
【问题描述】:

我正在尝试使用“WebMethod”发出警报消息,我的条件如下

1.我试图限制用户在“星期一”申请休假,因为他/她已经在上周五休假。 2.我正在从员工留下详细信息的数据库中获取详细信息并尝试在 WebMethod 中对此进行编码

我的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 "";       
}

我的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>

【问题讨论】:

    标签: c# asp.net asp.net-mvc c#-4.0 c#-2.0


    【解决方案1】:

    Webmethod 仅适用于静态函数,因此请尝试

    public static  string GetCurrentTime()
    

    而不是

    public  string GetCurrentTime() 
    

    请注意,如果您这样做,那么通常在事件驱动功能(IE 服务器端点击事件)中出现的事件驱动功能将不起作用

    迈克在

    上有一篇很棒的帖子

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

    【讨论】:

    • 嗨,您能否使用我的代码给我示例。我的连接字符串中出现错误“非静态字段需要对象引用”。请向我显示警报消息
    【解决方案2】:

    示例 //只是一个汽车类

     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); }
            });
    

    【讨论】:

    • 而不是使用存储的 proc 位置,只需添加您常用的 sql 查询字符串
    • 嗨 Rahul,它在整个代码中都显示错误,“非静态字段需要对象引用”
    • 感谢您的快速回复。
    • 我猜是因为 datetime.tryparse 调用尝试注释代码并重试
    • 兄弟你能修改我的代码并发送。以便我分析
    猜你喜欢
    • 1970-01-01
    • 2022-07-27
    • 2019-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多