在Web表单上有一个下拉列表和一个Ajax更新Panel.I还使用了一个脚本管理器。更新面板内部有一个网格视图。我使用数据源绑定下拉列表选定值网格视图。将从数据库的指定表中提取值。 每次我选择一个下拉列表值时,Grid视图中应该有相应的数据。但是它不起作用,没有给出错误但给出错误的输出显示只有一个下拉列表选择值的网格视图数据。 这是HTML代码: -
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<br />
<br />
<br />
<br />
<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<br />
Select Deptt:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged1">
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
后端代码: -
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;
using System.Configuration;
public partial class Default5 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Add_Dep();
}
private void Add_Dep()
{
SqlDataAdapter adp = new SqlDataAdapter("select * from tbdep", ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
DataSet ds = new DataSet();
adp.Fill(ds);
DropDownList1.DataTextField = "dname";
DropDownList1.DataValueField = "dno";
DropDownList1.DataSource = ds;
DropDownList1.DataBind();
}
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
String qry = "select * from tbemployee where edno=" + DropDownList1.SelectedValue;
SqlDataAdapter adp = new SqlDataAdapter(qry, ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
DataSet ds = new DataSet();
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
答案 0 :(得分:3)
您的网页加载存在问题。在页面加载中添加ispostback检查。
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
Add_Dep();
}
}