将存储过程数据绑定到asp.net标签

时间:2018-05-30 09:12:31

标签: c# asp.net

所以我有一个存储过程,我想添加到标签..我如何做到这一点,甚至我需要打击...即

 protected void label_DataBinding(object sender, EventArgs e)
        {

        }

我的数据是从班级中提取的。

数据类

 public DataSet TotalPacked(int itemSeriesMasterId, DateTime shiftstart, DateTime shiftend)
        {
            object[] args = new object[3] {itemSeriesMasterId, shiftstart, shiftend};
            return CallSp(MethodBase.GetCurrentMethod(), args) as DataSet;
        }
    }
}

商务舱

 public DataTable GetTotalPacked(DateTime shiftStart, DateTime shiftEnd, int seriesMasterId)
        {
            using (DataManager dmgr = new DataManager())
            {
                dmgr.Connect(ConfigurationManager.AppSettings["ProductionKey"]);
                DataSet dset = dmgr.TotalPacked(seriesMasterId,shiftStart,shiftEnd);
                return CreatePackingStats(dset);

那么如何从标签中调用存储的proc?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

因此,为了让label每隔几秒刷新一次数据,您可以尝试创建WebMethod来获取数据,然后通过调用WebMethod来调用来自javascript的setInterval()

有关WebMethods WebMethods via Web Service&的更多信息 Paramterized WebMethods

  

代码示例:

所以,例如,我有一个名为GetTime()的方法,我必须从中获取DateTime并将其显示在我的.aspx页面中label,每5秒一次。为了做到这一点,我必须从JavaScript(客户端)调用WebMethods。首先,我们在您的调用页面(.aspx)或主页面上(如果您需要在其他页面中)添加ScriptManager(这可以从工具栏中拖动或者可以简单地编写) EnablePageMethods="true"的选项。

<强>的ScriptManager

<asp:ScriptManager ID="scriptManager1" EnablePageMethods="true" runat="server"></asp:ScriptManager>

服务器端

    [System.Web.Services.WebMethod]
    public static string GetTime()
    {
        return System.DateTime.Now.ToString();
    }

要在客户端绑定它,我有标签,并设置了标签ClientIdMode="Static"的属性,所以,我们可以轻松地在javascript中获取控件:

<asp:Label runat="server" ID="lbltime" ClientIDMode="Static">time will be displayed here</asp:Label>

这是调用Webmethod的javascript函数:

function getTime()
{
  PageMethods.GetTime(ongetTimePass, ongetTimefail);            
}

PageMethods将methodname与参数以及onSuccess()和onFailure函数一起使用,语法如下: PageMethods.<WebMethodName>(Paramter1,Paramter2....,OnSucess(),OnFailure())

如果您的项目附有JQuery库,可以通过ajax调用该函数:

function getTime()
    {
        $.ajax({
            type: "POST",
            url: "default.aspx/GetTime",
            data: '',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: ongetTimePass,
            failure: ongetTimefail
        });

    }

以下是ongetTimePass()ongetTimefail()功能:

 function ongetTimePass(result)
 {
     //here we pass the result if the method has a success response.            
     document.getElementById('lbltime').innerHTML = result;
 }

 function ongetTimefail(error)
 {
     alert(error);
 }

现在要调用它,我们使用[setInterval()]函数(load())将其添加到页面的https://www.w3schools.com/jsref/met_win_setinterval.asp事件中:

window.onload = function () {
        var st = setInterval(getTime, 1000);
    };