【问题标题】:Error for dropdown list : OnSelectedIndexChange下拉列表错误:OnSelectedIndexChange
【发布时间】:2018-01-04 21:29:04
【问题描述】:

我正在尝试从数据库中创建两个相互连接的下拉列表,它们是部门和医生。第一个下拉列表是部门,另一个是医生。根据医生所属的科室id,第二个下拉列表应该返回医生。我几乎可以确定我的代码是正确的,但我收到了这个奇怪的错误:

CS1061:“patient_newappointment_aspx”不包含定义 对于“Department_Changed”且没有扩展方法“Department_Changed” 接受“patient_newappointment_aspx”类型的第一个参数可以 找到(您是否缺少 using 指令或程序集引用?)

asp.x 文件

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>

                    <table>
            <tr>
                <td>Select Department</td>
                <td><asp:DropDownList ID="DDLDepartment" runat="server" AutoPostBack="true" OnSelectedIndexChanged="Department_Changed"> 
                    </asp:DropDownList> 
                </td>
            </tr>
            <tr>
                <td>Select Doctor:</td>
                <td>
                    <asp:DropDownList ID="DDLDoctor" runat="server" AutoPostBack = "true">
                    </asp:DropDownList>
                </td>
            </tr>

                        </table>
                </ContentTemplate>
                    </asp:UpdatePanel>

背后的代码

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CareAndCureFull.Patient
{
    public partial class NewAppointment : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CareandCure"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack) {

                string query = "SELECT ID, Name FROM Department";
                BindDropDownList(DDLDepartment, query, "Name", "ID", "Select Department");
                DDLDoctor.Enabled = false;
                DDLDoctor.Items.Insert(0, new ListItem("Select Doctor", "0"));

            }
        }

        protected void Department_Changed(object sender, EventArgs e)
        {

            DDLDoctor.Enabled = false;
            DDLDoctor.Items.Clear();
            DDLDoctor.Items.Insert(0, new ListItem("Select Doctor", "0"));
            int DepartmentId = int.Parse(DDLDepartment.SelectedItem.Value);
            if (DepartmentId > 0)
            {
                string query = string.Format("SELECT ID, FirstName FROM Doctor WHERE DepartmentID = {0}", DepartmentId);
                BindDropDownList(DDLDoctor, query, "FirstName", "ID", "Select Doctor");
                DDLDoctor.Enabled = true;
            }

        }

        private void BindDropDownList(DropDownList ddl, string query, string text, string value, string defaultText)
        {

            SqlCommand cmd = new SqlCommand(query);
            using (conn)
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = conn;
                    conn.Open();
                    ddl.DataSource = cmd.ExecuteReader();
                    ddl.DataTextField = text;
                    ddl.DataValueField = value;
                    ddl.DataBind();
                    conn.Close();
                }
            }
            ddl.Items.Insert(0, new ListItem(defaultText, "0"));
        }
    }
}

【问题讨论】:

    标签: c# asp.net visual-studio webforms dropdown


    【解决方案1】:
    <asp:DropDownList ID="DDLDoctor" runat="server" AutoPostBack = "true">
    

    在此处删除 AutoPostBack 解决了问题。

    【讨论】:

      猜你喜欢
      • 2013-05-22
      • 1970-01-01
      • 2012-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多