如何调用webmethod并显示警报消息 [英] How to call webmethod and show alert message

查看:68
本文介绍了如何调用webmethod并显示警报消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试使用WebMethod发出警告信息,我的条件如下:

1.我正试图限制用户在星期一申请休假时,他/她已经在上个星期五休假。
2.我正在从我的数据库中详细说明员工离开详细信息并尝试在WebMethod中编写代码。





我尝试过:



< pre>我的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 = 选择EmpID,LeaveType,LeaveFromDate,LeaveToDate ,LeaveAppatus来自LeaveApplication其中LeaveFromDate =' + date1 + '和LeaveToDate =' + date2 + ';
scmd = new SqlCommand(Leave,scon);



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

字符串 value ;
if ((dt.Rows [i] [ LeaveStatus]。ToString()== 已接受)|| (dt.Rows [i] [ LeaveStatus]。ToString()== 待定))

{
value = ;

}
else
{

value = ;
}

}


return ;
}




我的aspx:



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

$ .ajax({
type: POST
url:< span class =code-string> LMSEmployee.aspx / GetCurrentTime
data: params
contentType: application / json; charset = utf-8
dataType: json
成功:OnSuccess,
失败:函数(响应){
alert( );
}
});
}
函数OnSuccess(响应){
alert( );

}
< / script >

解决方案

.ajax({
type: POST
url: LMSEmployee.aspx / GetCurrentTime
data: params
contentType: application / JSON; charset = utf-8
dataType: json
成功:OnSuccess,
失败:函数(响应){
alert( );
}
});
}
函数OnSuccess(响应){
alert( );

}
< / script >


< blockquote>参考这个

使用ASP.Net WebMethod调用jQuery AJAX [ ^ ]

尝试使用 static 方法


Hi,
    I am trying to give an alert message using "WebMethod",where my conditions are as follows

1.I am trying to restrict the user applying leave on "Monday" when he/She has already taken Leave on the previous friday.
2.I am geeting the details from my Database where the employees leave Details and trying to code this in WebMethod.



What I have tried:

<pre>My cs page code:

       [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:



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

解决方案

.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>


refer this
Calling ASP.Net WebMethod using jQuery AJAX[^]
try with static method


这篇关于如何调用webmethod并显示警报消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆