我试图创建两个从数据库相互连接的下拉列表,这些列表是部门和医生。第一次下拉是部门,另一次是医生。根据医生所属的部门ID,第二个下拉列表应该返回医生。我几乎可以肯定我的代码是正确的,但我收到了这个奇怪的错误:
CS1061:' patient_newappointment_aspx'不包含定义 为部门改变'没有扩展方法' Department_Changed' 接受第一个类型' patient_newappointment_aspx'可以 找到(你错过了使用指令或汇编引用吗?)
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"));
}
}
}
答案 0 :(得分:0)
<asp:DropDownList ID="DDLDoctor" runat="server" AutoPostBack = "true">
在此处删除AutoPostBack解决了这个问题。