Visual Studio 2017, Asp.net, c# DropDownList 控件,如何在选择新值后刷新文本框

Visual Studio 2017, Asp.net, c# DropDownList Control, How to Refresh Text Boxes After a New Value is Selected

如何在用户 select 从下拉列表中输入新值后执行 sql 语句并刷新文本框。我有一个 asp 页面,允许用户从下拉列表控件中 select 另一门课程。用户更改 selection 后,我想从数据库中获取课程 ID 和 select 记录。然后我想刷新用于显示课程的文本框。该页面仅用于显示课程并生成唯一代码以验证出席情况。

Visual Studio 2017 社区,Asp.net 4.6,Ado,c#,Microsoft SQL Server 2016

该页面仅用于显示课程并生成唯一代码以验证出勤率。我试过索引更改事件,但我不需要 post 数据返回数据库。

instructorcourse.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="instructorcourse.aspx.cs" Inherits="updatecourses" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head3" runat="server">
    <title>Update Course</title>
    <style type="text/css">
        .auto-style1 {
            width: 660px;
        }
        .auto-style2 {
            width: 132px;
        }
    </style>
</head>
<body>
    <form id="form3" runat="server">
   <h2>
        &nbsp;</h2>
        <h2>
        Attendance On Grounds</h2>
        <h2>
        Instructor</h2>
        <h2>
        Next Course</h2>
        <table>
             <tr>
                <td>Record ID</td>
                <td class="auto-style2">
                    <asp:TextBox ID="txtRecordID" runat="server"></asp:TextBox>
                    &nbsp;</td>
                <td class="auto-style1">
                    <asp:Button ID="btnGetDetails" runat="server" Text="Get RecordID" OnClick="btnGetDetails_Click" Width="131px" />
                    &lt;-will select next course on form load, by hostid, will go in form load</td>
            </tr>

            <tr>
                <td>
                    Insctructor ID:</td>
                <td class="auto-style2">
                    <asp:TextBox ID="txtIntructorid" runat="server"></asp:TextBox></td>
                <td class="auto-style1">
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    Instructor Name:</td>
                <td class="auto-style2">
                    <asp:TextBox ID="txtInstructorName" runat="server"></asp:TextBox></td>
                <td class="auto-style1">
                    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSourceDropDownList" DataTextField="InstructorName" DataValueField="RecordID">
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="SqlDataSourceDropDownList" runat="server" ConnectionString="<%$ ConnectionStrings:AdoNetCrudWebSite %>" SelectCommand="SELECT DISTINCT [RecordID], [InstructorName] FROM [attendance]"></asp:SqlDataSource>
                    &lt;-drop down, if change, new name then lookup id, and refill textboxes</td>
            </tr>
            <tr>
                <td>
                    Course Code:</td>
                <td class="auto-style2">
                    <asp:TextBox ID="txtCourseCode" runat="server"></asp:TextBox></td>
                <td class="auto-style1">
                    &nbsp;</td>
            </tr>

            <tr>
                <td>
                    Course Title:</td>
                <td class="auto-style2">
                    <asp:TextBox ID="txtCourseTitle" runat="server"></asp:TextBox></td>
                <td class="auto-style1">
                    &nbsp;</td>
            </tr>


            <tr>
                <td>
                    Attendance Code: </td>
                <td class="auto-style2">
                    <asp:TextBox ID="txtAttendanceCode" runat="server"></asp:TextBox>
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Generate Code" />
                </td>
                <td class="auto-style1">
                    &nbsp;</td>
            </tr>


        </table>
        <br />
        <asp:Button ID="btnUpdate" runat="server" Text="Update Course" Enabled="false" OnClick="btnUpdate_Click" Visible="False" /><br />
        <br />
        <asp:Label ID="Label2" runat="server" EnableViewState="False"></asp:Label><br />
        <p />
        <a href="homepage.aspx">Go Back To Menu</a>
    </form>
</body>
</html>

instructorcourse.aspx.cs
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class updatecourses : System.Web.UI.Page
{
    protected void btnGetDetails_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(Database.ConnectionString);
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from courses", con);
            cmd.Parameters.AddWithValue("@RecordID", txtRecordID.Text);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                // display data in textboxes
                txtIntructorid.Text = dr["instructorid"].ToString();
                txtInstructorName.Text = dr["InstructorName"].ToString();
                txtCourseCode.Text = dr["CourseCode"].ToString();
                txtCourseTitle.Text = dr["CourseTitle"].ToString();
                txtAttendanceCode.Text = dr["AttendanceCode"].ToString();
                btnUpdate.Enabled = true;
            }
            else
            {
                //lblMsg.Text = "Sorry! Invalid attendance Id";
                btnUpdate.Enabled = false;
            }
            dr.Close();
        }
        catch (Exception ex)
        {
            //lblMsg.Text = "Error --> " + ex.Message;
        }
        finally
        {
            con.Close();
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection(Database.ConnectionString);
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("update courses set InstructorID=@InstructorID, InstructorName = @InstructorName, CourseCode = @CourseCode, CourseTitle = @CourseTitle, AttendanceCode = @AttendanceCode where RecordID = @RecordID", con);
            //cmd.Parameters.AddWithValue("@CourseID", txtCourseID.Text);
            cmd.Parameters.AddWithValue("@InstructorID", txtIntructorid.Text);
            cmd.Parameters.AddWithValue("@InstructorName", txtInstructorName.Text);
            cmd.Parameters.AddWithValue("@CourseCode", txtCourseCode.Text);
            cmd.Parameters.AddWithValue("@CourseTitle", txtCourseTitle.Text);
            cmd.Parameters.AddWithValue("@AttendanceCode", txtAttendanceCode.Text);

            if (cmd.ExecuteNonQuery() == 1)
            {
                //lblMsg.Text = "Updated Successfully!";
            }
            else
            {
                //lblMsg.Text = "Sorry! Could not update";
            }
        }
        catch (Exception ex)
        {
            //lblMsg.Text = "Error --> " + ex.Message;
        }
        finally
        {
            con.Close();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

    }
}

database create script
USE [master]
GO
/****** Object:  Database [AdoNetCrudWebSite]    Script Date: 1/25/2019 9:49:34 AM ******/
CREATE DATABASE [AdoNetCrudWebSite]
 CONTAINMENT = NONE
 ON  PRIMARY 
( NAME = N'AdoNetCrudWebSite', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER2016\MSSQL\DATA\AdoNetCrudWebSite.mdf' , SIZE = 8192KB , MAXSIZE = UNLIMITED, FILEGROWTH = 65536KB )
 LOG ON 
( NAME = N'AdoNetCrudWebSite_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER2016\MSSQL\DATA\AdoNetCrudWebSite_log.ldf' , SIZE = 8192KB , MAXSIZE = 2048GB , FILEGROWTH = 65536KB )
GO
ALTER DATABASE [AdoNetCrudWebSite] SET COMPATIBILITY_LEVEL = 130
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [AdoNetCrudWebSite].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [AdoNetCrudWebSite] SET ANSI_NULL_DEFAULT OFF 
GO
ALTER DATABASE [AdoNetCrudWebSite] SET ANSI_NULLS OFF 
GO
ALTER DATABASE [AdoNetCrudWebSite] SET ANSI_PADDING OFF 
GO
ALTER DATABASE [AdoNetCrudWebSite] SET ANSI_WARNINGS OFF 
GO
ALTER DATABASE [AdoNetCrudWebSite] SET ARITHABORT OFF 
GO
ALTER DATABASE [AdoNetCrudWebSite] SET AUTO_CLOSE OFF 
GO
ALTER DATABASE [AdoNetCrudWebSite] SET AUTO_SHRINK OFF 
GO
ALTER DATABASE [AdoNetCrudWebSite] SET AUTO_UPDATE_STATISTICS ON 
GO
ALTER DATABASE [AdoNetCrudWebSite] SET CURSOR_CLOSE_ON_COMMIT OFF 
GO
ALTER DATABASE [AdoNetCrudWebSite] SET CURSOR_DEFAULT  GLOBAL 
GO
ALTER DATABASE [AdoNetCrudWebSite] SET CONCAT_NULL_YIELDS_NULL OFF 
GO
ALTER DATABASE [AdoNetCrudWebSite] SET NUMERIC_ROUNDABORT OFF 
GO
ALTER DATABASE [AdoNetCrudWebSite] SET QUOTED_IDENTIFIER OFF 
GO
ALTER DATABASE [AdoNetCrudWebSite] SET RECURSIVE_TRIGGERS OFF 
GO
ALTER DATABASE [AdoNetCrudWebSite] SET  DISABLE_BROKER 
GO
ALTER DATABASE [AdoNetCrudWebSite] SET AUTO_UPDATE_STATISTICS_ASYNC OFF 
GO
ALTER DATABASE [AdoNetCrudWebSite] SET DATE_CORRELATION_OPTIMIZATION OFF 
GO
ALTER DATABASE [AdoNetCrudWebSite] SET TRUSTWORTHY OFF 
GO
ALTER DATABASE [AdoNetCrudWebSite] SET ALLOW_SNAPSHOT_ISOLATION OFF 
GO
ALTER DATABASE [AdoNetCrudWebSite] SET PARAMETERIZATION SIMPLE 
GO
ALTER DATABASE [AdoNetCrudWebSite] SET READ_COMMITTED_SNAPSHOT OFF 
GO
ALTER DATABASE [AdoNetCrudWebSite] SET HONOR_BROKER_PRIORITY OFF 
GO
ALTER DATABASE [AdoNetCrudWebSite] SET RECOVERY FULL 
GO
ALTER DATABASE [AdoNetCrudWebSite] SET  MULTI_USER 
GO
ALTER DATABASE [AdoNetCrudWebSite] SET PAGE_VERIFY CHECKSUM  
GO
ALTER DATABASE [AdoNetCrudWebSite] SET DB_CHAINING OFF 
GO
ALTER DATABASE [AdoNetCrudWebSite] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF ) 
GO
ALTER DATABASE [AdoNetCrudWebSite] SET TARGET_RECOVERY_TIME = 60 SECONDS 
GO
ALTER DATABASE [AdoNetCrudWebSite] SET DELAYED_DURABILITY = DISABLED 
GO
EXEC sys.sp_db_vardecimal_storage_format N'AdoNetCrudWebSite', N'ON'
GO
ALTER DATABASE [AdoNetCrudWebSite] SET QUERY_STORE = OFF
GO
USE [AdoNetCrudWebSite]
GO
ALTER DATABASE SCOPED CONFIGURATION SET MAXDOP = 0;
GO
ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET MAXDOP = PRIMARY;
GO
ALTER DATABASE SCOPED CONFIGURATION SET LEGACY_CARDINALITY_ESTIMATION = OFF;
GO
ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET LEGACY_CARDINALITY_ESTIMATION = PRIMARY;
GO
ALTER DATABASE SCOPED CONFIGURATION SET PARAMETER_SNIFFING = ON;
GO
ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET PARAMETER_SNIFFING = PRIMARY;
GO
ALTER DATABASE SCOPED CONFIGURATION SET QUERY_OPTIMIZER_HOTFIXES = OFF;
GO
ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET QUERY_OPTIMIZER_HOTFIXES = PRIMARY;
GO
USE [AdoNetCrudWebSite]
GO
/****** Object:  Table [dbo].[attendance]    Script Date: 1/25/2019 9:49:35 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[attendance](
    [RecordID] [int] IDENTITY(1,1) NOT NULL,
    [InstructorID] [varchar](50) NULL,
    [InstructorName] [varchar](200) NULL,
    [CourseCode] [varchar](50) NULL,
    [CourseTitle] [varchar](50) NULL,
    [AttendanceCode] [varchar](50) NULL,
 CONSTRAINT [PK_attendance] PRIMARY KEY CLUSTERED 
(
    [RecordID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
/****** Object:  Table [dbo].[courses]    Script Date: 1/25/2019 9:49:35 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[courses](
    [RecordID] [int] IDENTITY(1,1) NOT NULL,
    [InstructorID] [varchar](50) NULL,
    [InstructorName] [varchar](200) NULL,
    [CourseCode] [varchar](50) NULL,
    [CourseTitle] [varchar](50) NULL,
    [AttendanceCode] [varchar](50) NULL,
 CONSTRAINT [PK_books] PRIMARY KEY CLUSTERED 
(
    [RecordID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
USE [master]
GO
ALTER DATABASE [AdoNetCrudWebSite] SET  READ_WRITE 
GO

我认为当您更改 DropDownList 的值时,您正在尝试 AutoPostBack="true",为此您可以使用 UpdatePanel 并触发它的 OnSelectedIndexChanged 事件,甚至您可以在其中指定您的逻辑。

<asp:ScriptManager ID="MainScriptManager" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel2" runat="server">
        <ContentTemplate>
            <asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" DataSourceID="SqlDataSourceDropDownList" DataTextField="InstructorName" DataValueField="RecordID">
            </asp:DropDownList>
            <asp:SqlDataSource ID="SqlDataSourceDropDownList" runat="server" ConnectionString="<%$ ConnectionStrings:AdoNetCrudWebSite %>" SelectCommand="SELECT DISTINCT [RecordID], [InstructorName] FROM [attendance]"></asp:SqlDataSource>
            <asp:TextBox ID="TextBox1" runat="server">TextBox you want to change</asp:TextBox>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />
        </Triggers>
</asp:UpdatePanel>