0

在带有下拉列表和 Ajax 更新面板的 Web 表单上。我还使用了脚本管理器。更新面板内有一个网格视图。我使用数据源将下拉列表选定值与网格视图绑定.这些值将从数据库的指定表中提取。每次我选择一个下拉列表值时,网格视图中应该有相应的数据。但它不起作用,没有给出错误,而是给出了错误的输出,显示只有一个下拉列表选定值的网格视图数据。这是 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>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:GridView ID="GridView1" runat="server">
            </asp:GridView>
            &nbsp;&nbsp;
            <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();
}

}

4

1 回答 1

3

您的页面加载有问题。在 Pageload 中添加 ispostback 检查。

protected void Page_Load(object sender, EventArgs e)
{
   if(!Page.IsPostBack)
   {
      Add_Dep();
   }
}
于 2013-09-07T14:14:46.497 回答