我是一個僅從視頻教程進行編程和學習的新手。我使用C#在ASP.NET Web窗體中創建了一個報告卡程序。在我的Web窗體中,我需要一個下拉列表,它將顯示數據庫中學生的姓名和一個文本框,用於顯示學生對應的ID#。我能夠設法從數據庫的下拉列表中顯示學生名單。現在,我的問題是,如何從下拉列表中選擇學生姓名時自動顯示學生ID#?如果您能向我展示一步一步的過程,我非常感激。將下拉列表的結果綁定到ASP.NET web表單中的文本框中
0
A
回答
1
將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,database = UniversityDB,username = sa,password = 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]
希望這會有所幫助。
相關問題
- 1. asp.net:將Web服務中的XML綁定到下拉列表中
- 2. 綁定文本框像下拉列表
- 3. 將下拉列表值綁定到文本框
- 4. 如何將文本框綁定到下拉列表?
- 5. 將ASP.NET Web API響應綁定到下拉列表
- 6. 將通用列表綁定到MVC3中的下拉列表中
- 7. 將文本從文本框列表框在asp.net web表單
- 8. 將文本框綁定到列表框
- 9. 將Json結果綁定到下拉列表
- 10. 將JSON結果綁定到下拉列表
- 11. 如何將excel表綁定到ASP.NET C#中的級聯下拉列表中#
- 12. 將兩列綁定到下拉列表
- 13. 將簡單的排序下拉列表綁定到MVC中的列表
- 14. 填寫下拉列表中的文本框在asp.net中選擇
- 15. ASP.NET綁定列表框與下拉列表
- 16. 如何將數據表綁定到ASP.net MVC中的下拉列表?
- 17. WP7綁定列表框到WCF結果
- 18. 將下拉列表綁定到SQLDataSource
- 19. 將網格綁定到下拉列表
- 20. 將數據綁定到列表框中包含文本塊的列表框
- 21. 將數據動態綁定到mvc中的下拉列表中
- 22. 在ASP.Net C中綁定下拉與另一個下拉列表#
- 23. 將下拉列表選定值及其文本框值綁定到Model MVC5
- 24. 如何將SharePoint文本字段綁定到下拉列表?
- 25. 將多個字段綁定到ASP.NET中的列表框中
- 26. 將下拉列表中的選定值填充到文本框中
- 27. 如何將列表綁定到ItemsControl中的文本框
- 28. 在asp.net web表單中搜索列表框中的特定值
- 29. 下拉列表中的數據綁定
- 30. ASP.Net下拉框中的數據綁定
歡迎來到Stack Overflow。首先請檢查Stack Overflow的[請求問題幫助](http://stackoverflow.com/help/asking)。關注[我可以在這裏詢問哪些主題](http://stackoverflow.com/help/on-topic),[我應該避免詢問什麼類型的問題?](http://stackoverflow.com/help/dont (如何創建一個最小,完整和可驗證的示例)(http://stackoverflow.com/help/how-to-ask),http:// stackoverflow。 com/help/mcve)和[Stack Overflow問題清單](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist)。 –