Click here to Skip to main content
15,886,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,



I have a web method that is used to creat a a chart using chart.js... I want to pass a string value to a label... I will give the detailed code for the web method...



VB
<WebMethod()> _
        Public Shared Function GetZoneData(ByVal strMode As String) As String()
        Return GetZoneSalesData(strMode)
    End Function

    Private Shared Function GetZoneSalesData(ByVal strMode As String) As String()
        Dim clsSales As New TS.Corporate.SalesGenerateBarChart
        Dim dTable As New DataTable
        dTable = clsSales.ZoneSalesValue(strMode)
        Dim resultdata = From d In dTable.AsEnumerable()
        Dim data(dTable.Rows.Count - 1) As String
        For i As Integer = 0 To dTable.Rows.Count - 1
            Dim item As DataRow = dTable.Rows(i)
            data(i) = (String.Format("{0}-{1}", item("zone"), item("zoneTotal")))
        Next

        Return data
    End Function




I want to add another set of values to be passed on to the client side...

It will be as follows



VB
Dim clsSales As New TS.Corporate.SalesGenerateBarChart
        Dim strHead As String = clsSales.ZoneSalesString(strMode)


This is a Heading for the chart generated and the value is generated from the "strMode" parameter. I want to pass that strHead value and display it on a label... How can I pass this value to a label using Ajax...



Regards,

Sajin
Posted
Updated 11-Dec-15 18:26pm
v2

1 solution

As you know we can not access controls in web method, so You need to use JavaScript/Jqury to set label value.
//first you need to call javascript and then call web method from it, see below snippet

JavaScript
//IN ASPX
//from javascript call webmethod and pass parameters if exist and give call method
function callwebmthod()
{
PageName.MathodName(Param1, Param2, MathodName_CallBack);
}


function MathodName_CallBack(response) 
{
   //here response object contains return result
   var szReturnFromWebmethod = response.value;
   //store return value in label
   document.getElementById("label1").innerHTML = szReturnFromWebmethod;

}


C#
//in .CS page define webmethod
[Ajax.AjaxMethod()]
public string MathodName(string Param1, string Param2)
{
    //your method code goes here
}



'value' property is not able to set value to label, you nee to use 'innerHTML' property, see below snippet
JavaScript
//for IE
 document.getElementById("label1").innerHTML = "change in text or whatever";

//other than IE
 document.all("label1").innerHTML ="change in text or whatever"
 
Share this answer
 
v2
Comments
SajinAboo 12-Dec-15 1:32am    
Hi,

I want to know how can I pass that above value... The value of the label is generated from using the same parameters as the chart function...

Regards,
Sajin
koolprasad2003 12-Dec-15 1:49am    
You have call 'GetZoneData' web method from javascript right ? then In javascript you need to write a call back method which will return webmethod result to javascript and then call my above code to assign label
SajinAboo 12-Dec-15 1:51am    
Can you please explain with a sample code using the above method?
koolprasad2003 12-Dec-15 2:02am    
I have update my solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900