日期:2014-05-18  浏览次数:20860 次

学生求教:dropdownlist选择不同值时gridview绑定一个数据库里多个表的问题
我用的VS2005,Web窗体上建立了dropdownlist控件和一个gridview控件,dropdownlist控件里面有不同的items项目。我是想在选择dropdownlist不同选项的时候,在同一个gridview里面显示对应数据库表的信息。应该做怎样的绑定?

------解决方案--------------------
也可以
<%@ Page Language="C#" %>

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

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
String s = DropDownList1.SelectedValue;
//查询数据库,然后绑定GridView1
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
<form id="form1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</form>
</body>
</html>

------解决方案--------------------
联动表单一样。,那控件不是有text和vale那个值么?在后台写一个数据绑定的方法,参数是vale
把vlae的那个做成你想要查询的条件,
------解决方案--------------------
或者这样写成方法,也跟根据你自己的问题写不同的条件

C# code

<%@ Page Language="C#" %>

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

  protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
  {
     string name = this.dropdownlist.selectValue;
     BindView(string name);
     this.dataview.DataBind();
  }
   public void BindView(string name)
   {
        if(s == "老师")
        {
          string="select * from 老师";
          DataTable table = new DataTable();
          /*然后把这个SQL语句查询到的数据绑定到table里面*/
          this.gridview.datasouce = table;
        }
        else if(s == "学生")
        {
           string="select * from 学生";
           DataTable table = new DataTable();
           /*然后把这个SQL语句查询到的数据绑定到table里面*/
           this.gridview.datasouce = table;
        }
   }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
  <form id="form1" runat="server">
  <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
  </asp:DropDownList>
  <asp:GridView ID="GridView1" runat="server">
  </asp:GridView>
  </form>
</body>
</html>