使用DataTableReader的变量“datatable”名称

时间:2016-02-02 02:37:56

标签: c# jquery asp.net

我有点像菜鸟,所以请你好。我搜索了google / stackoverflow /其他地方并没有找到任何答案,但我不确定我是否使用正确的条款。

首先是背景:

我制作了一个在仪表板上显示mysql数据的网页。一切都正常。

仪表板上的每个“磁贴”都有不同的数据显示在我的aspx页面中使用mysql查询制作的谷歌图表。每个磁贴还具有“刷新”按钮,以便能够再次运行查询以更新图表。

刷新按钮对于每个磁贴具有相同的类,但具有唯一ID:

<input type="button" id="DailyTotal" class="rebind"/>

单击此按钮将运行执行以下操作的javascript函数:

  • 使用AJAX调用iHttpHandler来读取DataTable并检索新的查询数据
  • 将数据格式化为JSON
  • 重绘图表

到目前为止,这一切都正常。

现在这是我的问题:

如何将磁贴的ID和AJAX调用一起传递给iHttpHandler,以便我可以选择在代码隐藏上运行的正确查询?

每个磁贴的ID与相应的DataTable的名称相同。

目前我的代码是这样的:

private static DataTable ProgrammingTable(string chart)
        {
        var rand = new System.Random(DateTime.Now.Millisecond);
        var dt = new DataTable();

        DataTableReader reader = new DataTableReader(Dashboard.DailyTotal());
        try
        {
            dt.Load(reader);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        return dt;
    }

有什么方法可以让“DailyTotal())”成为一个变量,这样我就可以拥有一段能够更新每个磁贴的代码?

我尝试通过使用ASP隐藏字段来传递ID,但我仍然不确定如何构造代码,以便它知道使用变量的值作为DataTable的名称而不是认为变量名是DataTable名称。

这就是我对我的尝试 ASPX页面:

<asp:HiddenField ID="ChartID" runat="server" value=""/>

JS:

var chartName = $(this).attr("id");
$("input[id=ChartID]").val(chartName);

Codebehind:

        Page page = (Page)HttpContext.Current.Handler;
        HiddenField ChartID = (HiddenField)page.FindControl("ChartID");
        var aa = ChartID.Value;

然后我尝试这样做:

            DataTableReader reader = new DataTableReader(Dashboard.aa());

但它显然只是寻找一个名为aa的DataTable。

我能想到的唯一解决方案是为每个tile提供自己的ajax调用和iHttpHandler,但这显然不是优选的。

请帮助,如果有任何不清楚的地方,请告诉我。

感谢。

1 个答案:

答案 0 :(得分:0)

I want to give you a more in depth answer when I have time, but I've been meaning to respond to your question since you posted it, just busy. Quickly though I will say one approach is to use a switch statement or if/else blocks to call different methods based on the ID you get from the client. This is a brute force method and therefore not very elegant, nor does it scale well or lend itself to manageable code.

A better approach might be to use reflection, since your IDs match the method name you want to call.

I will add details when I have time, but you can start your own research with that maybe, as you seem quite competent for a self-proclaimed noob.

As for passing the ID from the client to the server via asp:HiddenField, that will not work because a synchronous post-back is required in order to update the server's view of the value that you just set on the client. Think of the hidden field sort of like a mailbox that you just put a letter in, and then the ajax call is like you just made a telephone call to the letter's recipient, but the postman (post back) hasn't even come to pick the letter up yet. This analogy starts to fall apart after that, and is not perfect but hopefully it sheds some light. So what you want to do is pass the ID To the server in the ajax call itself. This will become the parameter value (chart) that the sever side method receives. If you have multiple pieces of data to pass to the server you can serialize all the data as JSON and pass a single object to the server where it can be deserialized. Again, I will provide details and an example when I have time and something more than a phone with which to respond. Can you please post the details of the ajax call itself so I can try to tailor the example to your code?