将下拉列表中的结果绑定到ASP.NET Web表单中的文本框

坚持,稍等

我是编程的新手,只能从视频教程中学习。我已经使用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>&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,数据库= 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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何基于ASP.NET Web表单中的数据库表中的行动态创建文本框?

来自分类Dev

如何基于ASP.NET Web表单中的数据库表中的行动态创建文本框?

来自分类Dev

如何将数据表绑定到ASP.net MVC中的下拉列表?

来自分类Dev

阻止表单中的ASP.NET文本框提交表单

来自分类Dev

将ASP.NET Core剃须刀页面中的下拉列表绑定到ado.net存储过程

来自分类Dev

如何将Excel工作表绑定到ASP.NET C#中的级联下拉列表

来自分类Dev

从原始表单(vb.net 2013)的Datagridview中自动填充对话框表单上的文本框

来自分类Dev

文本框数据到asp.net中的xml

来自分类Dev

Asp.net MVC多行表单控件/文本框

来自分类Dev

以asp.net Web形式将文本从文本框添加到列表框

来自分类Dev

如何根据asp.net中下拉列表的选择绑定文本框?

来自分类Dev

在VB.Net的文本框中输入数字时,如何更改表单中my.settings中的值

来自分类Dev

在VB.Net的文本框中输入数字时,如何更改表单中my.settings中的值

来自分类Dev

启用“多行文本框”以在asp.net Webforms中的Enter键上汇总表单

来自分类Dev

在.net中,将数据存储在文本框中

来自分类Dev

在asp.net文本框或其他文本框中显示txt文件的内容

来自分类Dev

asp.net,将项目添加到带有文本框的列表中

来自分类Dev

ASP.NET MVC文本框中的TDD

来自分类Dev

Asp.net MVC中的“自动完成”文本框

来自分类Dev

在JavaScript中引用ASP.net文本框数据

来自分类Dev

在GridView Asp.net中添加搜索文本框

来自分类Dev

文本框值在asp.net中未更新

来自分类Dev

异步输出到asp.net中的文本框

来自分类Dev

同步文本框(在表单中输入)

来自分类Dev

表单文本框中的JavaScript VAR

来自分类Dev

使用ASP.NET Core 3.1 MVC根据下拉列表中的选定值填充文本框

来自分类Dev

是否可以在asp.net的下拉复选框文本框中显示选定的项目

来自分类Dev

使文本框绑定到VB.Net中的Double接受点

来自分类Dev

将数组中的JSON列表绑定到ASP.NET模型类

Related 相关文章

  1. 1

    如何基于ASP.NET Web表单中的数据库表中的行动态创建文本框?

  2. 2

    如何基于ASP.NET Web表单中的数据库表中的行动态创建文本框?

  3. 3

    如何将数据表绑定到ASP.net MVC中的下拉列表?

  4. 4

    阻止表单中的ASP.NET文本框提交表单

  5. 5

    将ASP.NET Core剃须刀页面中的下拉列表绑定到ado.net存储过程

  6. 6

    如何将Excel工作表绑定到ASP.NET C#中的级联下拉列表

  7. 7

    从原始表单(vb.net 2013)的Datagridview中自动填充对话框表单上的文本框

  8. 8

    文本框数据到asp.net中的xml

  9. 9

    Asp.net MVC多行表单控件/文本框

  10. 10

    以asp.net Web形式将文本从文本框添加到列表框

  11. 11

    如何根据asp.net中下拉列表的选择绑定文本框?

  12. 12

    在VB.Net的文本框中输入数字时,如何更改表单中my.settings中的值

  13. 13

    在VB.Net的文本框中输入数字时,如何更改表单中my.settings中的值

  14. 14

    启用“多行文本框”以在asp.net Webforms中的Enter键上汇总表单

  15. 15

    在.net中,将数据存储在文本框中

  16. 16

    在asp.net文本框或其他文本框中显示txt文件的内容

  17. 17

    asp.net,将项目添加到带有文本框的列表中

  18. 18

    ASP.NET MVC文本框中的TDD

  19. 19

    Asp.net MVC中的“自动完成”文本框

  20. 20

    在JavaScript中引用ASP.net文本框数据

  21. 21

    在GridView Asp.net中添加搜索文本框

  22. 22

    文本框值在asp.net中未更新

  23. 23

    异步输出到asp.net中的文本框

  24. 24

    同步文本框(在表单中输入)

  25. 25

    表单文本框中的JavaScript VAR

  26. 26

    使用ASP.NET Core 3.1 MVC根据下拉列表中的选定值填充文本框

  27. 27

    是否可以在asp.net的下拉复选框文本框中显示选定的项目

  28. 28

    使文本框绑定到VB.Net中的Double接受点

  29. 29

    将数组中的JSON列表绑定到ASP.NET模型类

热门标签

归档