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

时间:2018-03-15 13:49:42

标签: c# asp.net json

这是我关于堆栈溢出的第一个问题,所以请提前感谢您提供任何帮助/建议。

我目前正在使用ASP.Net和C#制作“图书馆数据库”。这是一项大学任务,我们仅限于此,必须使用JSON保存和撤回数据。

虽然我已经能够添加图书列表并在网格视图中显示列表,但我需要能够使用标准表单显示和编辑信息,使用下拉列表和文本框显示每个部分。 下面的代码片段显示了用于保存JSON文件的所有变量的Book类

 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;       
    }
}

以下代码显示了用于从上述变量生成列表的Other类。

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

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

以下代码显示了我目前在编辑图书页面上的内容。

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 = "";
    }

现在,我已经设法填充下拉列表,但是几个小时的研究和绞尽脑汁,我找不到将其翻译到文本框的方法。目前我无法使用Textboxes a)确定下拉列表使用的值 b)使用该值确定它使用的JSON文件的哪个变量,以及从哪个组。

现在我意识到我很可能无法使用此方法填充文本框,并且可能需要从头开始。我不需要递交给我的代码,但如果有人可以给我一个示例代码段,或者将我推向写作轨道以了解如何处理此任务,我将不胜感激。

请不要使用Ajax,Jquery或java。

如果你到最后再次感谢你。

3 个答案:

答案 0 :(得分:0)

我没有时间测试此代码。但意识形态很简单。您获得sender变量并将其转换为DropDownList类并使用其SelectedItem属性。

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

希望有所帮助:)

答案 1 :(得分:0)

我认为你在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;

答案 2 :(得分:0)

我不确定这是否有效,但这通常对我有用,所以我希望它对你也有帮助。

 protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
    {

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

    }

另外,我可以问为什么你将下拉列表数据文本字段加载为id而不是书名?