简体   繁体   English

需要帮助在C#中使用JSON数据源填充ASP.net网格视图

[英]Need help populating an ASP.net gridview using JSON datasource in c#

I have to create a gridview that is automatically populated with JSON. 我必须创建一个自动用JSON填充的gridview。

I have created the JSON folder, it works fully, i can save data to it no problem. 我已经创建了JSON文件夹,它可以正常运行,我可以将数据保存到它没有问题。

The issue im having is now making my gridview in my ASP.net to automatically populate the form USING my JSON file. 我现在遇到的问题是使我的ASP.net中的gridview自动填充使用JSON文件的表单。

I have tried using visual studios inbuilt LINQ wizard... It populated the headers of the gridview but not the fields itself. 我尝试使用Visual Studio内置的LINQ向导...它填充了gridview的标题,但没有填充字段本身。

Its a very simple gridview with 2 columns. 它是一个非常简单的带有2列的gridview。

My current asp gridview code: 我当前的asp gridview代码:

<asp:GridView ID="GridView1" AutoGenerateColumns="False
           " ShowHeaderWhenEmpty="True" EmptyDataText="No records Found" runat="server"  DataSourceID="Grid" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
            <Columns>
                <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id" />
                <asp:BoundField DataField="Title" HeaderText="Title" ReadOnly="True" SortExpression="Title" />
            </Columns>
        </asp:GridView>
        <asp:LinqDataSource ID="Grid" runat="server" ContextTypeName="BookCollection" EntityTypeName="" OrderBy="id, Title" Select="new (id, Title)" TableName="Books">
        </asp:LinqDataSource>

short sample of my JSON below (has one entry only atm, just showing the attributes)(Two total: id, and Title.) 下面是我的JSON的简短示例(只有一个atm,仅显示属性)(总共两个:id和Title。)

{"Books":[{"id":1,"Title":"Harry Potter"}]}

My gridview does NOT load when opened in a browser, but I'm assuming this is because the datasource is not linked properly yet or have a page load function because i don't know what to do. 我的gridview在浏览器中打开时不会加载,但是我认为这是因为数据源尚未正确链接或具有页面加载功能,因为我不知道该怎么做。

As you can tell i am very new to coding so sorry if i left anything out. 如您所知,我对编码非常陌生,如果遗漏任何内容,请对不起。

Any help would be GREATLY appreciated. 任何帮助将不胜感激。

Create a class that represent your json structure, like field names and data types, string, integer, etc. 创建一个表示您的json结构的类,例如字段名称和数据类型,字符串,整数等。

Then in the page load event tead rhe json file, as you normally do with a text file. 然后,就像通常使用文本文件一样,在页面加载事件中获取rhe json文件。

Serialize the json string you have read to a list of classes previously created and assign it as datasource to the gridview. 将您已阅读的json字符串序列化为先前创建的类的列表,并将其作为数据源分配给gridview。

Remove the linq datasource you don't need it 删除不需要的linq数据源

I agree other answers on this page, just wanted to add code sample on how to achieve that. 我同意此页面上的其他答案,只是想添加有关如何实现此目的的代码示例。 I'm removing DataSource from your aspx control as I will set it in code behind. 我正在从aspx控件中删除DataSource,因为我将在后面的代码中对其进行设置。

Webform 网络表格

<asp:GridView ID="GridView1" AutoGenerateColumns="False"
        ShowHeaderWhenEmpty="True" EmptyDataText="No records Found" runat="server" 
        OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="id" />
        <asp:BoundField DataField="Title" HeaderText="Title" ReadOnly="True"
                   SortExpression="Title" />
    </Columns>
</asp:GridView>

Code behind 后面的代码

protected void Page_Load(object sender, EventArgs e)
{
    string jsonString = "{\"Books\":[{\"id\":1,\"Title\":\"Harry Potter\"}, {\"id\":2,\"Title\":\"Bible\"}]}";
    var json = (new JavaScriptSerializer()).Deserialize(jsonString, typeof(BooksSource));
    GridView1.DataSource = ((BooksSource)json).Books;
    GridView1.DataBind();
}

Note: In your case you'll be reading json from a text file instead, with something like File.ReadAllText(path); 注意:在您的情况下,您将使用类似File.ReadAllText(path);类的文本文件读取json File.ReadAllText(path); or using streams if you work with large files. 或使用流(如果您使用大文件)。

Data source classes 数据源类

public class BooksSource
{
    public List<Book> Books { get; set; }
}

public class Book
{
    public string Id { get; set; }
    public string Title { get; set; }
}

I believe instead of directly assigning your json data as the data source, you can instead create a new list or a datable, if you want a table like datatype. 我相信,如果您想要像数据类型这样的表,则可以直接创建一个新列表或一个数据表,而不是直接将json数据分配为数据源。 You can populate this new list or datatable with your json data and assign it as the GridView's datasource. 您可以使用json数据填充此新列表或数据表,并将其分配为GridView的数据源。

Depending on how big your json file is, this method should be work fine. 根据您的json文件的大小,此方法应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM