2016-03-20 31 views
0

我是一個僅從視頻教程進行編程和學習的新手。我使用C#在ASP.NET Web窗體中創建了一個報告卡程序。在我的Web窗體中,我需要一個下拉列表,它將顯示數據庫中學生的姓名和一個文本框,用於顯示學生對應的ID#。我能夠設法從數據庫的下拉列表中顯示學生名單。現在,我的問題是,如何從下拉列表中選擇學生姓名時自動顯示學生ID#?如果您能向我展示一步一步的過程,我非常感激。將下拉列表的結果綁定到ASP.NET web表單中的文本框中

+1

歡迎來到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)。 –

回答

1

將DropDownList和TextBox添加到Default.aspx中。 在下拉列表中,我們將使用DropDownList控件的DataTextField和DataValueField屬性來存儲學生姓名和學生ID。它將在我們綁定數據時稍後映射。

<div class="jumbotron"> 
     <h1>Student Report Card Application</h1> 
     <p>&nbsp;</p> 
     <p> 
      Select a Student: 
      <asp:DropDownList ID="ddl_StudentName" AutoPostBack="true" DataTextField="student_name" DataValueField="student_id" runat="server"> 

      </asp:DropDownList> 
      &nbsp;&nbsp;&nbsp; <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] 

希望這會有所幫助。

相關問題