【问题标题】:How can I bind list to gridview?如何将列表绑定到gridview?
【发布时间】:2013-03-06 22:01:15
【问题描述】:

我正在尝试获取 JSON 文档,将其放入列表中,然后将其绑定到网格视图。我尝试了几种不同的方法,但没有任何效果。这是我的代码,问题出在最后 4 行。

   protected void Page_Load(object sender, EventArgs e)
    {


        var from = origin.Text;
        var to = destination.Text;
        var requesturl = @"http://maps.googleapis.com/maps/api/directions/json?origin =" + from + "&alternatives=false&units=imperial&destination=" + to + "&sensor=false;";
        string content = file_get_contents(requesturl); 


        JavaScriptSerializer jss = new JavaScriptSerializer();
        var d = jss.Deserialize<dynamic>(content);
        List<string> sl = d;

        GridView1.DataSource = content;
        GridView1.DataBind();



    }

【问题讨论】:

  • 为什么要使用 GridView 来呈现字符串列表?
  • @Mt.Schneiders,我是菜鸟。我来自数据库背景。返回的 JSON 是一组方向。我想在网格中将每个步骤显示为一行。

标签: c# json gridview


【解决方案1】:

这里有两种列出这些方向的方法:

1 - 使用 GridView:

<asp:GridView ID="grdExample" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField HeaderText="Directions">
            <ItemTemplate>
                <%# Container.DataItem %>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

绑定 GridView:

List<string> strings = new List<string>() { "Ha!", "Hey!", "Yo!" };

grdExample.DataSource = strings;
grdExample.DataBind();

2 - 使用中继器(更简单、更灵活,需要您自己的 HTML):

<asp:Repeater ID="rptExample" runat="server" >
    <ItemTemplate>
        <%# Container.DataItem %>
    </ItemTemplate>
</asp:Repeater>

绑定它:

List<string> strings = new List<string>() { "Ha!", "Hey!", "Yo!" };

rptExample.DataSource = strings;
rptExample.DataBind();

【讨论】:

    猜你喜欢
    • 2014-07-29
    • 2017-09-02
    • 1970-01-01
    • 1970-01-01
    • 2014-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多