提问人:Simply 提问时间:3/14/2023 更新时间:3/14/2023 访问量:30
如何更新此多层程序中的文本框,以便正确显示每个方法获取的值?C# ASP.NET
How would I update the Textboxes in this multi-tiered program in order to properly display the values which are gotten by each method? C# ASP.NET
问:
我被要求编写一个多层程序,从 xml 文件加载和解析 24 个学生成绩,然后使用图形用户界面表单(而不是控制台输出)将所有成绩、最低成绩、最高成绩和平均成绩显示到 Web 表单中。我正在使用文本框在网页上显示值,并且我的代码成功执行,但是,当我运行程序时,它不会在文本框中显示任何值 - 它们是空的。我应该如何解决这个问题/我做错了什么?我对 C# 非常陌生,因此任何帮助将不胜感激,因为我想学习如何为未来的程序解决问题。此程序必须使用 VS 中的 ASP.NET Web 应用程序模板来完成。下面是我的 web表单.aspx文件、aspx.cs文件、StudentGrade.cs文件和 xml 文件。
学生等级.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Web;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Linq;
using System.Text;
using System.IO;
using System.Net;
namespace WebApplication5
{
class StudentGrade
{
public static TextBox AllGrades;
public static TextBox AverageGrade;
public static TextBox LowestGrade;
public static TextBox HighestGrade;
public static void LoadAndParseStudentGrades()
{
//Load XML doc
XmlDocument doc = new XmlDocument();
doc.Load("C:\\Program Files (x86)\\IIS Express\\studentGrades.xml");
//Display all grades
XmlNodeList grades = doc.SelectNodes("Students/Student/Grade");
foreach (XmlNode grade in grades)
{
AllGrades.Text = grade.InnerText;
}
}
public static void Average()
{
//Get directory and load xml document with XMLpath
string XMLpath = Directory.GetCurrentDirectory() + @"\studentGrades.xml";
XDocument xmlDoc = XDocument.Load(XMLpath);
IEnumerable<XElement> gradeElements = xmlDoc.Root.Descendants("Grade");
//Set the count for grade elements
int totalCount = gradeElements.Count();
//Take all grade elements and take the average of them
double averageGrade = gradeElements.Select(x => (double)x).Average();
AverageGrade.Text = averageGrade.ToString();
}
public static void Lowest()
{
//Load XML doc to string
var xmlString = File.ReadAllText("C:\\Program Files (x86)\\IIS Express\\studentGrades.xml");
var xDoc = XDocument.Parse(xmlString);
var grade = xDoc.Descendants("Grade");
decimal minGrade = decimal.MaxValue;
//Loop to determine what grade has the lowest value
for (int i = 0; i < grade.Count(); i++)
{
var gr = decimal.Parse(grade.ElementAt(i).Value);
if (gr < minGrade)
{
minGrade = gr;
LowestGrade.Text = gr.ToString();
}
}
}
public static void Highest()
{
//Load XML doc to string
var xmlString = File.ReadAllText("C:\\Program Files (x86)\\IIS Express\\studentGrades.xml");
var xDoc = XDocument.Parse(xmlString);
var grade = xDoc.Descendants("Grade");
decimal maxGrade = decimal.MinValue;
//Loop to determine what grade has the highest value
for (int i = 0; i < grade.Count(); i++)
{
var gr = decimal.Parse(grade.ElementAt(i).Value);
if (gr > maxGrade)
{
maxGrade = gr;
HighestGrade.Text = gr.ToString();
}
}
}
}
}
网络表单.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace WebApplication5 {
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
StudentGrade.AllGrades = new TextBox();
StudentGrade.AverageGrade = new TextBox();
StudentGrade.LowestGrade = new TextBox();
StudentGrade.HighestGrade = new TextBox();
}
}
}
网络窗体:.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication5.WebForm1" %>
<%@ Import Namespace="WebApplication5" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Student Grades</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="All Grades: "></asp:Label>
<asp:TextBox ID="AllGrades" runat="server"></asp:TextBox>
<asp:Label ID="Label4" runat="server" Text="Highest Grade: "></asp:Label>
<asp:TextBox ID="HighestGrade" runat="server"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text="Lowest Grade: "></asp:Label>
<asp:TextBox ID="LowestGrade" runat="server"></asp:TextBox>
<asp:Label ID="Label3" runat="server" Text="Average Grade: "></asp:Label>
<asp:TextBox ID="AverageGrade" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
学生成绩.xml
<?xml version="1.0" encoding="utf-8"?>
<Students>
<Student ID ="1">
<Name>Will</Name>
<Grade>100</Grade>
</Student>
<Student ID ="2">
<Name>Kaylee</Name>
<Grade>93</Grade>
</Student>
<Student ID ="3">
<Name>Rick</Name>
<Grade>77</Grade>
</Student>
<Student ID ="4">
<Name>Carl</Name>
<Grade>85</Grade>
</Student>
<Student ID ="5">
<Name>Hannah</Name>
<Grade>91</Grade>
</Student>
<Student ID ="6">
<Name>Tyler</Name>
<Grade>67</Grade>
</Student>
<Student ID ="7">
<Name>Hunter</Name>
<Grade>87</Grade>
</Student>
<Student ID ="8">
<Name>Claude</Name>
<Grade>96</Grade>
</Student>
<Student ID ="9">
<Name>Taylor</Name>
<Grade>99</Grade>
</Student>
<Student ID ="10">
<Name>Anna</Name>
<Grade>79</Grade>
</Student>
<Student ID ="11">
<Name>Mike</Name>
<Grade>82</Grade>
</Student>
<Student ID ="12">
<Name>Jordan</Name>
<Grade>60</Grade>
</Student>
<Student ID ="13">
<Name>Cam</Name>
<Grade>75</Grade>
</Student>
<Student ID ="14">
<Name>Bob</Name>
<Grade>95</Grade>
</Student>
<Student ID ="15">
<Name>Evan</Name>
<Grade>84</Grade>
</Student>
<Student ID ="16">
<Name>Heather</Name>
<Grade>88</Grade>
</Student>
<Student ID ="17">
<Name>Kristen</Name>
<Grade>82</Grade>
</Student>
<Student ID ="18">
<Name>Monica</Name>
<Grade>58</Grade>
</Student>
<Student ID ="19">
<Name>Brad</Name>
<Grade>76</Grade>
</Student>
<Student ID ="20">
<Name>Carly</Name>
<Grade>95</Grade>
</Student>
<Student ID ="21">
<Name>Tom</Name>
<Grade>69</Grade>
</Student>
<Student ID ="22">
<Name>Marshall</Name>
<Grade>89</Grade>
</Student>
<Student ID ="23">
<Name>Jeff</Name>
<Grade>82</Grade>
</Student>
<Student ID ="24">
<Name>Sydney</Name>
<Grade>91</Grade>
</Student>
</Students>
我尝试使用文本框来正确显示它们各自的值,但是由于 Web 表单没有将任何值加载到文本框中,因此某处似乎存在错误 - 只是空文本框。我在网上研究了无数个小时,观看了许多视频,但无济于事。我还在学习C#,所以这让我有点困惑!什么代码可以解决文本框不显示任何内容的问题?
答:
处理 xml 数据有几种常用方法。
您可以考虑创建一个表示该 xml 中数据的“类”。
或者,您可以使用数据集和所谓的内置 xml 读取器。
因此,假设我们将文件上传到网站,将 xml 保存在名为 UpLoadFiles 的文件夹中,并作为 students.xml。
现在,为了显示这些数据,我们需要在页面上添加一些标记 - 可以显示重复的数据行。网格视图会很好用。
所以,说这个标记:
<asp:GridView ID="GridView1" runat="server" CssClass="table table-hover"
width="20%" >
</asp:GridView>
我们读取 xml 的代码可能是这样的:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadData();
}
}
void LoadData()
{
string sFile = Server.MapPath("~/UpLoadFiles/students.xml");
DataSet ds = new DataSet();
ds.ReadXml(sFile);
DataTable dt = ds.Tables[0];
GridView1.DataSource = dt;
GridView1.DataBind();
}
结果是这样的:
因此,如何处理此类 XML 实际上取决于您要在此处执行的操作。
也许最终目标是将该数据放入数据库(可能是 sql server)中。
但是,作为基本的开始,上面给出了一些关于如何将此类数据读取到数据集中的想法(使用内置的 xml 阅读器)。
从该数据集中,您的 xml 确实会生成一个不错的表,因此我们将该表直接发送到 gridview 进行显示。
编辑:显示最大等级和最小等级
因此,在显示数据之前,我们可以对最小等级和最大等级进行一些控制。
这样说:
<h3>Lowest Grade</h3>
ID:<asp:Label ID="lblLoID" runat="server" Text=""></asp:Label> <br />
Name:<asp:Label ID="lblLoName" runat="server" Text=""></asp:Label> <br />
Grade:<asp:Label ID="lblLoGrade" runat="server" Text=""></asp:Label> <br />
<br />
<h3>Highest Grade</h3>
ID:<asp:Label ID="lblHiID" runat="server" Text=""></asp:Label> <br />
Name:<asp:Label ID="lblHiName" runat="server" Text=""></asp:Label> <br />
Grade:<asp:Label ID="lblHiGrade" runat="server" Text=""></asp:Label> <br />
<h3>Data</h3>
<asp:GridView ID="GridView1" runat="server" CssClass="table table-hover"
etc. etc. etc.
那么,现在,我们获取/获取最小值和最大值的代码?
好吧,该等级列以文本形式出现,并且要排序,那么我们需要一个整数值。
因此,以下代码将起作用:
GridView1.DataSource = dt;
GridView1.DataBind();
// to get max or min value, we need integer value,
// not a string
dt.Columns.Add("iGrade", typeof(int));
foreach (DataRow dr in dt.Rows)
dr["iGrade"] = Convert.ToInt32(dr["Grade"]);
// fill out lowest grade information
DataRow[] LowGrade = dt.Select("iGrade = min(iGrade)");
lblLoName.Text = LowGrade[0]["Name"].ToString();
lblLoGrade.Text = LowGrade[0]["Grade"].ToString();
lblLoID.Text = LowGrade[0]["ID"].ToString();
// fill out highest grade information
DataRow[] HiGrade = dt.Select("iGrade = max(iGrade)");
lblHiName.Text = HiGrade[0]["Name"].ToString();
lblHiGrade.Text = HiGrade[0]["Grade"].ToString();
lblHiID.Text = HiGrade[0]["ID"].ToString();
现在我们看到/得到这个:
除了 Albert 提供的答案之外,你编写的代码中还有一个错误的方法。
基本上,不应在 StudentGrade 类中放置任何文本框实例。定义类时,需要遵循关注点分离规则;因此,在类 StudentGrade 中,您需要定义数值变量来保存计算,而不是为 AllGrades、AverageGrade、LowestGrade 和 HighestGrade 设置 TextBox。然后,在 Web 表单的后面,您应该使用这些计算并将其绑定到您放置在 Web 表单中的控件。
学生年级:
class StudentGrade
{
public static float AllGrades;
public static float AverageGrade;
public static float LowestGrade;
public static float HighestGrade;
public static void LoadAndParseStudentGrades()
{
//Load XML doc
XmlDocument doc = new XmlDocument();
doc.Load("C:\\Program Files (x86)\\IIS Express\\studentGrades.xml");
//Display all grades
XmlNodeList grades = doc.SelectNodes("Students/Student/Grade");
foreach (XmlNode grade in grades)
{
AllGrades = float.Parse(grade.InnerText);
}
}
public static void Average()
{
//Get directory and load xml document with XMLpath
string XMLpath = Directory.GetCurrentDirectory() + @"\studentGrades.xml";
XDocument xmlDoc = XDocument.Load(XMLpath);
IEnumerable<XElement> gradeElements = xmlDoc.Root.Descendants("Grade");
//Set the count for grade elements
int totalCount = gradeElements.Count();
//Take all grade elements and take the average of them
double averageGrade = gradeElements.Select(x => (double)x).Average();
AverageGrade = float.Parse(averageGrade.ToString());
}
public static void Lowest()
{
//Load XML doc to string
var xmlString = File.ReadAllText("C:\\Program Files (x86)\\IIS Express\\studentGrades.xml");
var xDoc = XDocument.Parse(xmlString);
var grade = xDoc.Descendants("Grade");
decimal minGrade = decimal.MaxValue;
//Loop to determine what grade has the lowest value
for (int i = 0; i < grade.Count(); i++)
{
var gr = decimal.Parse(grade.ElementAt(i).Value);
if (gr < minGrade)
{
minGrade = gr;
LowestGrade = float.Parse(gr.ToString());
}
}
}
public static void Highest()
{
//Load XML doc to string
var xmlString = File.ReadAllText("C:\\Program Files (x86)\\IIS Express\\studentGrades.xml");
var xDoc = XDocument.Parse(xmlString);
var grade = xDoc.Descendants("Grade");
decimal maxGrade = decimal.MinValue;
//Loop to determine what grade has the highest value
for (int i = 0; i < grade.Count(); i++)
{
var gr = decimal.Parse(grade.ElementAt(i).Value);
if (gr > maxGrade)
{
maxGrade = gr;
HighestGrade = float.Parse(gr.ToString());
}
}
}
}
WebForm1的
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
StudentGrade.LoadAndParseStudentGrades();
StudentGrade.Average();
StudentGrade.Lowest();
StudentGrade.Highest();
AllGrades.Text = StudentGrade.AllGrades.ToString();
AverageGrade.Text = StudentGrade.AverageGrade.ToString();
LowestGrade.Text = StudentGrade.LowestGrade.ToString();
HighestGrade.Text = StudentGrade.HighestGrade.ToString();
}
}
尽管您定义的类很好,但有更好的方法来实现您的情况。我更喜欢以您的方法返回计算结果的方式实现 StudentGrade,而不是将它们设置为公共静态值。
public class StudentGrade
{
public float Average()
{
//Get directory and load xml document with XMLpath
string XMLpath = Directory.GetCurrentDirectory() + @"\studentGrades.xml";
XDocument xmlDoc = XDocument.Load(XMLpath);
IEnumerable<XElement> gradeElements = xmlDoc.Root.Descendants("Grade");
//Set the count for grade elements
int totalCount = gradeElements.Count();
//Take all grade elements and take the average of them
double averageGrade = gradeElements.Select(x => (double)x).Average();
return float.Parse(averageGrade.ToString());
}
}
评论
下一个:需要阻止浏览器后退按钮
评论