【问题标题】:Loop the webmethod so 2 ajax calls retrieve 2 different calls循环 web 方法,以便 2 个 ajax 调用检索 2 个不同的调用
【发布时间】:2016-04-29 13:02:29
【问题描述】:

我混合了很多信息,但效果很好,直到我发现我想要 2 个甜甜圈图,然后我遇到了问题,我越界了我试图找到一种方法来循环 webmethod,所以它接收2 ajax 调用,它确实收到了它们,但它没有像我想要的那样在 web 方法中循环,所以它检索不同的,比如 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 个图表之前,意味着.. 它只是 web 方法,我需要修复,如果可能的话.. 循环它,所以它会循环 2 次,并且有一个arraylist或类似的东西,所以ajax调用正在发送一个额外的数字,比如“index:0”,然后我检查“index”是否为“0”,然后这样做,或者类似的东西,我试图这样做。

我试图在“数据”中为其提供索引,然后通过以下方式接收它:

public static string GetChart (string chart, int index) 

然后它停止工作,当我尝试去做时,我不确定我是否做对了,这就是我提到它的原因,如果有人知道怎么做的话。 我试着这样做,就像使用 jQuery 代码一样,变量“数组”,所以我用 if/else 检查“索引”是 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;

【问题讨论】:

    标签: c# jquery asp.net ajax webmethod


    【解决方案1】:

    我发现,我只需要正确地将它们分成 2 个单独的方法,我之前做错了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-08
      • 2018-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 2014-08-14
      相关资源
      最近更新 更多