C# 循环webmethod,以便2个ajax调用检索2个不同的调用
我混合了很多信息,但效果很好,直到我发现我想要两个油炸圈饼图,然后我遇到了问题,我超出了界限,我试图找到一种方法来循环webmethod,所以它收到了两个ajax调用,它确实收到了它们,但它没有像我想要的那样在webmethod中进行循环,所以它检索不同的,比如chart1,应该是关于船舶的,而图表2应该是关于其他的。 比如,它应该是一个循环,其中querystring正在更改,因此它将从查询中获得两个不同的Select命令。 WebMethod背后的代码 jQuery 然后它停止了工作,当我试着去做的时候,我不确定我是否做对了,这就是为什么我提到它,如果有人知道怎么做的话。 我试着这样做,比如jQuery代码,变量数组,所以我用if/else检查索引是0还是1,因为我有2个调用。但它不能处理它,没有什么真正发生。 包含索引的WebMethod背后的代码 jQuery包含索引C# 循环webmethod,以便2个ajax调用检索2个不同的调用,c#,jquery,asp.net,ajax,webmethod,C#,Jquery,Asp.net,Ajax,Webmethod,我混合了很多信息,但效果很好,直到我发现我想要两个油炸圈饼图,然后我遇到了问题,我超出了界限,我试图找到一种方法来循环webmethod,所以它收到了两个ajax调用,它确实收到了它们,但它没有像我想要的那样在webmethod中进行循环,所以它检索不同的,比如chart1,应该是关于船舶的,而图表2应该是关于其他的。 比如,它应该是一个循环,其中querystring正在更改,因此它将从查询中获得两个不同的Select命令。 WebMethod背后的代码 jQuery 然后它停止了工作,当
我发现,我只需要正确地将它们分成两个不同的方法,我之前做得不对
[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 ( );
}
}
}
}
$( 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();
}
<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>
public static string GetChart (string chart, int index)
[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 ( );
}
}
}
}
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;