简体   繁体   English

使用C#和JSON在ASP.Net中填充下拉列表和文本框

[英]Populate Drop Down List & Text Boxes in ASP.Net using C# and JSON

This is my first question on Stack overflow so thank you in advance for any help/ advice given. 这是我关于堆栈溢出的第一个问题,因此在此先感谢您提供的任何帮助/建议。

I am currently making a "Library Database" using only ASP.Net and C#. 我目前仅使用ASP.Net和C#创建一个“库数据库”。 It is a university assignment and we are limited to this, Data must be saved and withdrawn using JSON. 这是大学的作业,我们仅限于此,必须使用JSON保存和提取数据。

While i have been able to Add a book listing and display the listing in a grid view, I need to be able to display and edit the information using a standard form, using a drop down list and text boxes to display each section. 虽然我已经能够添加书籍清单并在网格视图中显示该清单,但我需要能够使用标准表单,使用下拉列表和文本框显示每个部分来显示和编辑信息。 The below code snippet shows the Book class used to keep all the variables for the JSON File 下面的代码片段显示了Book类,该类用于保留JSON File的所有变量

 public class Book
{
    public string id { get; set; }
    public string title { get; set; }
    public string author { get; set; }
    public string year { get; set; }
    public string publisher { get; set; }
    public string isbn { get; set; }

    public Book(string id, string title, string author, string year, string publisher, string isbn)
    {
        this.id = id;
        this.title = title;
        this.author = author;
        this.year = year;
        this.publisher = publisher;
        this.isbn = isbn;       
    }
}

The below code shows the Other class being used to generate a list from the above variables. 下面的代码显示了Other类,该类用于从上述变量生成列表。

public class BookList
{
    public List<Book> bookList { get; set; }

    public BookList()
    {
        bookList = new List<Book>();
    }
}

The below code shows what i currently have for my Edit Book Page. 下面的代码显示了我目前在“编辑书本”页面上所拥有的内容。

public partial class EditBook : System.Web.UI.Page
{
    public const string FILENAME = @"C:\Users\User\Documents\Assessments\19383038_CSE2ICX_Assignment3\JsonFiles\BookList.Json";
    string jsonText = " ";
    BookList bookList = new BookList();  

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            jsonText = File.ReadAllText(FILENAME);
        }
        catch (FileNotFoundException)
        {

        }
        BookList bookList = JsonConvert.DeserializeObject<BookList>(jsonText);
        JObject jObj = JObject.Parse(jsonText);
        if (!IsPostBack)
        {

            ddl.DataTextField = "id";
            ddl.DataValueField = "id";
            ddl.DataSource = bookList.bookList;
            ddl.DataBind();
        }

    }

    protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
    {

        txtEnterID.Text = "";
    }

Now, I have managed to populate the Drop Down List, but hours of research and racking my brain, i cannot find a way to translate that onto the text boxes. 现在,我已经设法填充了“下拉列表”,但是经过数小时的研究和动脑筋,我找不到将其翻译到文本框中的方法。 Currently I have no way of having the Textboxes a) Identify which value the Drop down list is using b) Using that value to determine which variable of the JSON file it uses, and from which group. 当前,我无法使用文本框a)标识下拉列表使用的是哪个值b)使用该值来确定它使用的JSON文件的哪个变量,以及从哪个组。

Now im aware that its very likely I cannot use this method to populate the text boxes, and probably need to start from scratch. 现在我知道它很可能无法使用此方法填充文本框,并且可能需要从头开始。 I don't need the code handed to me, but i would appreciate if someone could give me an example snippet, or push me on to the write track with how to tackle this task. 我不需要交给我的代码,但是如果有人可以给我提供示例片段,或者将我推向编写轨道以解决该问题,我将不胜感激。

Please no Ajax, Jquery or java. 请不要使用Ajax,Jquery或Java。

Thank you again if you made it to the end. 如果一切顺利,请再次感谢。

I haven't had time to test this code. 我还没有时间测试这段代码。 But the ideia is pretty simple. 但是思想很简单。 You get the sender variable and convert it to DropDownList class and use its SelectedItem property. 您将获得sender变量,并将其转换为DropDownList类,并使用其SelectedItem属性。

  protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
    {
        var selectedValue = (Book)((DropDownList)sender).SelectedItem.Value;
        txtEnterID.Text = selectedValue.title;
    }

Hope it helps :) 希望能帮助到你 :)

I think you are looking for something like this inside the SelectedIndexChanged method: 我认为您正在SelectedIndexChanged方法中寻找这样的东西:

//get your list of books
BookList bookList = JsonConvert.DeserializeObject<BookList>(jsonText);

//select the correct book from the list using Linq and the selected value from ddl
var book = bookList.bookList.Where(x => x.id == ddl.SelectedValue).FirstOrDefault();

//show the book properties in the textboxes
txtEnterID.Text = book.id;
txtTitle.Text = book.title;

I'm not sure if this works, but this usually works for me, so I hope it helps you too. 我不确定这是否有效,但通常对我有用,因此希望对您也有帮助。

 protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
    {

        txtEnterID.Text = ddl.SelectedItem.Value.ToString();

    }

Also, could I ask why you load the dropdownlist data text field as id and not book name? 另外,请问为什么您将下拉列表数据文本字段作为id而不是书名加载?

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

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