循环网络方法,以便 2 ajax 调用检索 2 个不同的调用
Loop the webmethod so 2 ajax calls retrieve 2 different calls
我混合了很多信息,但效果很好,直到我发现我想要 2 个甜甜圈图,然后我遇到了问题,我越界了我试图找到一种循环网络方法的方法,所以它收到了2 ajax 调用,它确实收到它们,但它没有像我想要的那样在 webmethod 中循环,所以它检索不同的东西,比如 chart1,应该是关于船舶的东西,而 chart2 应该是关于别的东西。
就像,它应该是一个循环,其中查询字符串正在更改,因此它将从查询中获得 2 个不同的 Select 命令。
WebMethod 背后的代码
[WebMethod]
public static string GetChart ( string chart )
{
string constr1 = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
using ( SqlConnection con = new SqlConnection ( constr1 ) )
{
string query = string.Format("SELECT Value1,Color1,Highlight1,Label1 FROM Tuning WHERE FK_Chart1_ID = '{0}'", chart);
string query2 = string.Format("SELECT Value2,Color2,Highlight2,Label2 FROM Tuning WHERE FK_Chart2_ID = '{0}'", chart);
using ( SqlCommand cmd = new SqlCommand ( ) )
{
cmd.CommandText = query;
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open ( );
using ( SqlDataReader sdr = cmd.ExecuteReader ( ) )
{
StringBuilder sb1 = new StringBuilder();
sb1.Append ( "[" );
while ( sdr.Read ( ) )
{
sb1.Append ( "{" );
System.Threading.Thread.Sleep ( 50 );
sb1.Append ( string.Format ( "value:{0}, color: '{1}', highlight :'{2}', label :'{3}'" , sdr [ 0 ] , sdr [ 1 ] , sdr [ 2 ] , sdr [ 3 ] ) );
sb1.Append ( "}," );
}
sb1 = sb1.Remove ( sb1.Length - 1 , 1 );
sb1.Append ( "]" );
con.Close ( );
return sb1.ToString ( );
}
}
}
}
jQuery
$( function ()
{
$( '#<%= DropDownList_1.ClientID %>' ).bind( "change", function ()
{
LoadChart();
} );
} );
function LoadChart()
{
//setup an array of AJAX options, each object is an index that will specify information for a single AJAX request
var ajaxes = [{ url: 'CS.aspx/GetChart', data: "{chart: '" + $( '#<%= DropDownList_1.ClientID %>' ).val() + "'}", contentType: "application/json; charset=utf-8", dataType: 'json', chart: '#dvChart1' }, { url: 'CS.aspx/GetChart', data: "{motor: '" + $( '#<%= DropDownList_1.ClientID %>' ).val() + "'}", contentType: "application/json; charset=utf-8", dataType: 'json', chart: '#dvChart2' }],
current = 0;
//declare your function to run AJAX requests
function loadCharts()
{
//check to make sure there are more requests to make
if ( current < ajaxes.length )
{
//make the AJAX request with the given data from the `ajaxes` array of objects
$.ajax( {
type: "POST",
url: ajaxes[current].url,
data: ajaxes[current].data,
contentType: ajaxes[current].contentType,
dataType: ajaxes[current].dataType,
success: function ( r )
{
$( ajaxes[current].chart ).html( "" );
var data = eval( r.d );
var el = document.createElement( 'canvas' );
$( ajaxes[current].chart )[0].appendChild( el );
//Fix for IE 8
if ( $.browser.msie && $.browser.version === "8.0" )
{
G_vmlCanvasManager.initElement( el );
}
var ctx = el.getContext( '2d' );
var chartoptions =
{
animateScale: true,
animationEasing: "linear",
showTooltips: true,
animationSteps: 120
}
var userStrengthsChart = new Chart( ctx).Doughnut( data, chartoptions );
//increment the `current` counter and recursively call this function again
current++;
loadCharts();
},
failure: function ( response )
{
alert( 'There was an error.' );
}
} );
}
}
//run the AJAX function for the first time once `document.ready` fires
loadCharts();
}
.ASPX代码
<table>
<tr>
<td>Choose:
<asp:DropDownList ID="DropDownList_1" runat="server">
<asp:ListItem Text="Choose" Value="0" />
<asp:ListItem Text="TestList" Value="1" />
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<div id="dvChart1">
</div>
</td>
<td>
<div id="dvChart2">
</div>
</td>
</tr>
</table>
就像顶部所说的那样,代码运行良好,在我想要 2 个图表之前,意味着..它只是 webmethod,我需要修复,如果可能的话..循环它,所以它会循环 2 次,并且有arraylist 或类似的东西,所以 ajax 调用发送一个额外的数字,比如 "index:0" 然后我检查 "index" 是否为“0”,然后执行此操作或类似的操作,我试着那样做。
我试着在 "data" 中给它索引,然后通过:
接收它
public static string GetChart (string chart, int index)
然后它停止工作,而我尝试这样做,我不确定我是否做对了,这就是为什么我提到它,如果有人知道如何那样做的话。
我试着这样做,就像 jQuery 代码,变量 "array",所以我检查 if/else,"index" 是 0 还是 1,因为我有 2电话。但它无法处理它,实际上什么也没有发生。
WebMethod 背后的代码(包括索引)
[WebMethod]
public static string GetChart ( string chart, int index )
{
string constr1 = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
using ( SqlConnection con = new SqlConnection ( constr1 ) )
{
string query;
if (index == 0)
{
query = string.Format("SELECT Value1,Color1,Highlight1,Label1 FROM Tuning WHERE FK_Resultat_ID = '{0}'", chart);
}
else
{
query = string.Format("SELECT Value2,Color2,Highlight2,Label2 FROM Tuning WHERE FK_Resultat_ID = '{0}'", chart);
}
//string query = string.Format("SELECT Value1,Color1,Highlight1,Label1 FROM Tuning WHERE FK_Resultat_ID = '{0}'", chart);
//string query2 = string.Format("SELECT Value2,Color2,Highlight2,Label2 FROM Tuning WHERE FK_Resultat_ID = '{0}'", chart);
using ( SqlCommand cmd = new SqlCommand ( ) )
{
cmd.CommandText = query;
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open ( );
using ( SqlDataReader sdr = cmd.ExecuteReader ( ) )
{
StringBuilder sb1 = new StringBuilder();
sb1.Append ( "[" );
while ( sdr.Read ( ) )
{
sb1.Append ( "{" );
System.Threading.Thread.Sleep ( 50 );
sb1.Append ( string.Format ( "value:{0}, color: '{1}', highlight :'{2}', label :'{3}'" , sdr [ 0 ] , sdr [ 1 ] , sdr [ 2 ] , sdr [ 3 ] ) );
sb1.Append ( "}," );
}
sb1 = sb1.Remove ( sb1.Length - 1 , 1 );
sb1.Append ( "]" );
con.Close ( );
return sb1.ToString ( );
}
}
}
}
jQuery(包括索引)
var ajaxes = [{ url: 'CS.aspx/GetChart', data: "{chart: '" + $( '#<%= DropDownList_Motor.ClientID %>' ).val() + "'},{index:0}", contentType: "application/json; charset=utf-8", dataType: 'json', chart: '#dvChart1' }, { url: 'CS.aspx/GetChart', data: "{motor: '" + $( '#<%= DropDownList_Motor.ClientID %>' ).val() + "'},{index:1}", contentType: "application/json; charset=utf-8", dataType: 'json', chart: '#dvChart2' }],
current = 0;
我发现,我只需要将它们正确地分成 2 个独立的方法,我之前做错了。
我混合了很多信息,但效果很好,直到我发现我想要 2 个甜甜圈图,然后我遇到了问题,我越界了我试图找到一种循环网络方法的方法,所以它收到了2 ajax 调用,它确实收到它们,但它没有像我想要的那样在 webmethod 中循环,所以它检索不同的东西,比如 chart1,应该是关于船舶的东西,而 chart2 应该是关于别的东西。
就像,它应该是一个循环,其中查询字符串正在更改,因此它将从查询中获得 2 个不同的 Select 命令。
WebMethod 背后的代码
[WebMethod]
public static string GetChart ( string chart )
{
string constr1 = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
using ( SqlConnection con = new SqlConnection ( constr1 ) )
{
string query = string.Format("SELECT Value1,Color1,Highlight1,Label1 FROM Tuning WHERE FK_Chart1_ID = '{0}'", chart);
string query2 = string.Format("SELECT Value2,Color2,Highlight2,Label2 FROM Tuning WHERE FK_Chart2_ID = '{0}'", chart);
using ( SqlCommand cmd = new SqlCommand ( ) )
{
cmd.CommandText = query;
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open ( );
using ( SqlDataReader sdr = cmd.ExecuteReader ( ) )
{
StringBuilder sb1 = new StringBuilder();
sb1.Append ( "[" );
while ( sdr.Read ( ) )
{
sb1.Append ( "{" );
System.Threading.Thread.Sleep ( 50 );
sb1.Append ( string.Format ( "value:{0}, color: '{1}', highlight :'{2}', label :'{3}'" , sdr [ 0 ] , sdr [ 1 ] , sdr [ 2 ] , sdr [ 3 ] ) );
sb1.Append ( "}," );
}
sb1 = sb1.Remove ( sb1.Length - 1 , 1 );
sb1.Append ( "]" );
con.Close ( );
return sb1.ToString ( );
}
}
}
}
jQuery
$( function ()
{
$( '#<%= DropDownList_1.ClientID %>' ).bind( "change", function ()
{
LoadChart();
} );
} );
function LoadChart()
{
//setup an array of AJAX options, each object is an index that will specify information for a single AJAX request
var ajaxes = [{ url: 'CS.aspx/GetChart', data: "{chart: '" + $( '#<%= DropDownList_1.ClientID %>' ).val() + "'}", contentType: "application/json; charset=utf-8", dataType: 'json', chart: '#dvChart1' }, { url: 'CS.aspx/GetChart', data: "{motor: '" + $( '#<%= DropDownList_1.ClientID %>' ).val() + "'}", contentType: "application/json; charset=utf-8", dataType: 'json', chart: '#dvChart2' }],
current = 0;
//declare your function to run AJAX requests
function loadCharts()
{
//check to make sure there are more requests to make
if ( current < ajaxes.length )
{
//make the AJAX request with the given data from the `ajaxes` array of objects
$.ajax( {
type: "POST",
url: ajaxes[current].url,
data: ajaxes[current].data,
contentType: ajaxes[current].contentType,
dataType: ajaxes[current].dataType,
success: function ( r )
{
$( ajaxes[current].chart ).html( "" );
var data = eval( r.d );
var el = document.createElement( 'canvas' );
$( ajaxes[current].chart )[0].appendChild( el );
//Fix for IE 8
if ( $.browser.msie && $.browser.version === "8.0" )
{
G_vmlCanvasManager.initElement( el );
}
var ctx = el.getContext( '2d' );
var chartoptions =
{
animateScale: true,
animationEasing: "linear",
showTooltips: true,
animationSteps: 120
}
var userStrengthsChart = new Chart( ctx).Doughnut( data, chartoptions );
//increment the `current` counter and recursively call this function again
current++;
loadCharts();
},
failure: function ( response )
{
alert( 'There was an error.' );
}
} );
}
}
//run the AJAX function for the first time once `document.ready` fires
loadCharts();
}
.ASPX代码
<table>
<tr>
<td>Choose:
<asp:DropDownList ID="DropDownList_1" runat="server">
<asp:ListItem Text="Choose" Value="0" />
<asp:ListItem Text="TestList" Value="1" />
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<div id="dvChart1">
</div>
</td>
<td>
<div id="dvChart2">
</div>
</td>
</tr>
</table>
就像顶部所说的那样,代码运行良好,在我想要 2 个图表之前,意味着..它只是 webmethod,我需要修复,如果可能的话..循环它,所以它会循环 2 次,并且有arraylist 或类似的东西,所以 ajax 调用发送一个额外的数字,比如 "index:0" 然后我检查 "index" 是否为“0”,然后执行此操作或类似的操作,我试着那样做。
我试着在 "data" 中给它索引,然后通过:
接收它public static string GetChart (string chart, int index)
然后它停止工作,而我尝试这样做,我不确定我是否做对了,这就是为什么我提到它,如果有人知道如何那样做的话。 我试着这样做,就像 jQuery 代码,变量 "array",所以我检查 if/else,"index" 是 0 还是 1,因为我有 2电话。但它无法处理它,实际上什么也没有发生。
WebMethod 背后的代码(包括索引)
[WebMethod]
public static string GetChart ( string chart, int index )
{
string constr1 = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
using ( SqlConnection con = new SqlConnection ( constr1 ) )
{
string query;
if (index == 0)
{
query = string.Format("SELECT Value1,Color1,Highlight1,Label1 FROM Tuning WHERE FK_Resultat_ID = '{0}'", chart);
}
else
{
query = string.Format("SELECT Value2,Color2,Highlight2,Label2 FROM Tuning WHERE FK_Resultat_ID = '{0}'", chart);
}
//string query = string.Format("SELECT Value1,Color1,Highlight1,Label1 FROM Tuning WHERE FK_Resultat_ID = '{0}'", chart);
//string query2 = string.Format("SELECT Value2,Color2,Highlight2,Label2 FROM Tuning WHERE FK_Resultat_ID = '{0}'", chart);
using ( SqlCommand cmd = new SqlCommand ( ) )
{
cmd.CommandText = query;
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open ( );
using ( SqlDataReader sdr = cmd.ExecuteReader ( ) )
{
StringBuilder sb1 = new StringBuilder();
sb1.Append ( "[" );
while ( sdr.Read ( ) )
{
sb1.Append ( "{" );
System.Threading.Thread.Sleep ( 50 );
sb1.Append ( string.Format ( "value:{0}, color: '{1}', highlight :'{2}', label :'{3}'" , sdr [ 0 ] , sdr [ 1 ] , sdr [ 2 ] , sdr [ 3 ] ) );
sb1.Append ( "}," );
}
sb1 = sb1.Remove ( sb1.Length - 1 , 1 );
sb1.Append ( "]" );
con.Close ( );
return sb1.ToString ( );
}
}
}
}
jQuery(包括索引)
var ajaxes = [{ url: 'CS.aspx/GetChart', data: "{chart: '" + $( '#<%= DropDownList_Motor.ClientID %>' ).val() + "'},{index:0}", contentType: "application/json; charset=utf-8", dataType: 'json', chart: '#dvChart1' }, { url: 'CS.aspx/GetChart', data: "{motor: '" + $( '#<%= DropDownList_Motor.ClientID %>' ).val() + "'},{index:1}", contentType: "application/json; charset=utf-8", dataType: 'json', chart: '#dvChart2' }],
current = 0;
我发现,我只需要将它们正确地分成 2 个独立的方法,我之前做错了。