我是编程的新手,只能从视频教程中学习。我已经使用C#在ASP.NET Web表单中创建了一个报告卡程序。在我的Web窗体中,我需要一个下拉列表,该列表将显示数据库中学生的姓名,以及一个文本框,该文本框应显示学生相应的ID#。我能够在数据库的下拉列表中显示学生列表。现在,我的问题是,当从下拉列表中选择学生姓名时,如何自动显示学生ID#?如果您能向我展示一个逐步的过程,我将不胜感激。
将DropDownList和TextBox添加到Default.aspx。在下拉列表中,我们将使用DropDownList控件中的DataTextField和DataValueField属性存储学生姓名和学生ID。当我们绑定数据时,它将被映射。
<div class="jumbotron">
<h1>Student Report Card Application</h1>
<p> </p>
<p>
Select a Student:
<asp:DropDownList ID="ddl_StudentName" AutoPostBack="true" DataTextField="student_name" DataValueField="student_id" runat="server">
</asp:DropDownList>
<asp:TextBox ID="Student_ID" AutoPostBack="true" runat="server" MaxLength="40"></asp:TextBox>
</p>
</div>
<div class="row">
</div>
</asp:Content>
接下来,我们需要在后面添加一些代码来处理数据的检索。通过Default.aspx.cs后面的代码。本示例将连接到SqlServer数据库。还要注意,每当我们更改学生的下拉值时,它将通过名为SelectedIndexChanged的onchange事件绑定学生的ID。也可以查看评论。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
namespace WebApplication_Test1
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
//connect to the database now
if (Page.IsPostBack == false)
{
//we store the database connect information in Web.Config
//so we retrieve the connection string from the Web.Config
String mydatabaseconnection = ConfigurationManager.ConnectionStrings["DBConnection"].ToString();
SqlConnection con = new SqlConnection(mydatabaseconnection);
//select all records from the grades table via
//here you can replace this table 'Grades' with your table's schema
String myquery = "Select * From Grades";
SqlCommand command = new SqlCommand(myquery);
command.CommandType = System.Data.CommandType.Text;
command.Connection = con;
try
{
//open the connection to the database
con.Open();
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet ds = new DataSet("Grades");
//populate the data into a DataSet
adapter.Fill(ds);
//ddl_StudentName.DataSource = ds.Tables[0];
ddl_StudentName.DataSource = ds;
ddl_StudentName.DataBind(); // bind the data from the table now
// this is were DataTextField and DataValueField will get mapped
// to database fields student_name and student_id
//to handle the drop down change use event SelectedIndexChanged
ddl_StudentName.SelectedIndexChanged += Ddl_StudentName_SelectedIndexChanged;
//gets the first student from the database and populate the textbox
Student_ID.Text = ds.Tables[0].Rows[0]["student_id"].ToString();
//close connection to database
con.Close();
}
catch (Exception ex)
{
}
}else
{
ddl_StudentName.SelectedIndexChanged += Ddl_StudentName_SelectedIndexChanged
}
}
private void Ddl_StudentName_SelectedIndexChanged(object sender, EventArgs e)
{
//when we change the dropdownlist we need to get the student id and set it to the textbox
DropDownList mydropdownlist = sender as DropDownList;
Student_ID.Text = mydropdownlist.SelectedValue;
}
}
}
示例Web.config代码段。在这里,您可以看到连接字符串参数server = DESKTOP-CPJ3R2K23 \ SQLEXPRESS,数据库= UniversityDB,用户名= sa,密码= test1,提供程序是SqlClient。在此示例中,连接到SqlServer Express数据库是必需的。
<connectionStrings>
<add name="DBConnection" connectionString="server=DESKTOP-CPJ3R2K23\SQLEXPRESS;database=UniversityDB;Integrated Security=True;uid=sa;pwd=test1" providerName="System.Data.SqlClient"/>
</connectionStrings>
来自UniversityDB数据库的样本表方案。
USE [UniversityDB]
GO
CREATE TABLE [dbo].[Grades](
[grade] [varchar](10) NULL,
[student_id] [int] NULL,
[student_name] [varchar](40) NULL
) ON [PRIMARY]
希望这可以帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句