UpdatePanel中DropDownList无反应

水晶烟灰缸 水晶烟灰缸 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
前台代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestDroplist.aspx.cs" Inherits="TestDroplist" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:PostBackTrigger ControlID ="cbDep" />
</Triggers>
<ContentTemplate>
<asp:DropDownList ID="cbDep" runat="server" AutoPostBack ="true"
onselectedindexchanged="cbDep_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="cbMaj" runat="server" AutoPostBack ="true">
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>



后台代码

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

public partial class TestDroplist : System.Web.UI.Page
{
DataTable GetDataTable(string sql)
{
DataTable dt = null;
try
{
SqlConnection Conn = new SqlConnection("Data Source=127.0.0.1;database=olpv;uid=sa;pwd=hxyw801102");
Conn.Open();
SqlCommand Cmd = new SqlCommand(sql, Conn);
dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(Cmd);
da.Fill(dt);
Conn.Close();
}
catch (Exception ex)
{
Response.Write("<script>alert('" + ex.StackTrace + "')</script>");
}
return dt;
}

void FillDropList(DropDownList obj, string sql)
{
obj.Items.Clear();
obj.Items.Add("");
DataTable dt = GetDataTable(sql);
if (dt != null)
{
if (dt.Rows.Count > 0)
{
foreach (DataRow row in dt.Rows)
{
obj.Items.Add(row[0].ToString());
}
}
}
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillDropList(cbDep, "select DepartmentName from Info_Department");
}
}
protected void cbDep_SelectedIndexChanged(object sender, EventArgs e)
{
FillDropList(cbMaj, "select Info_Major.Major from Info_Department,Info_Major where Info_Department.DepartmentID=Info_Major.DepartmentID and Info_Department.DepartmentName='" + cbDep.Text + "'");
}
}


点击cbDep是不触发cbDep_SelectedIndexChanged事件
...全文
评论 17
收藏 已收藏
分享
返回顶部
水晶烟灰缸
.NET社区 62,275 成员 668,968 内容