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

解决方案 »

  1.   

    直接拷贝执行下面的代码即可
    http://dotnet.aspx.cc/article/d94323a7-e322-4ead-9f25-6e6629c8012e/read.aspx
      

  2.   

    也可以
    <%@ 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>
      

  3.   

     联动表单一样。,那控件不是有text和vale那个值么?在后台写一个数据绑定的方法,参数是vale
    把vlae的那个做成你想要查询的条件,
      

  4.   

    恩,这个可以绑定数据库里面的一张表吧。我的意思是不同的dropdownlist.selectedvalue值调用不同的查询数据库函数,然后在gridview里面显示。注意:是点击dropdownlist的时候获取值,然后gridview显示对应的查询函数所查询的表里面存储的信息。
      

  5.   

    恩,这个可以绑定数据库里面的一张表吧。我的意思是不同的dropdownlist.selectedvalue值调用不同的查询数据库函数,然后在gridview里面显示。注意:是点击dropdownlist的时候获取值,然后gridview显示对应的查询函数所查询的表里面存储的信息。
      

  6.   

    他的意思是让你根据不同的selectValue绑定不同的表
    比如你的DropDownList的值有 学生和老师两个选项,那么你的代码就是<%@ 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;
      if(s == "老师")
      {
        string="select * from 老师";
        DataTable table = new DataTable();
        /*然后把这个SQL语句查询到的数据绑定到table里面*/
        this.gridview.datasouce = table;
        this.gridview.databind();
      }
       else if(s == "学生")
       {
         string="select * from 学生";
         DataTable table = new DataTable();
         /*然后把这个SQL语句查询到的数据绑定到table里面*/
         this.gridview.datasouce = table;
         this.gridview.databind();
       }
      }
    </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>
    有更多的选项就写更多的条件分支
      

  7.   

    或者这样写成方法,也跟根据你自己的问题写不同的条件
    <%@ 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>
      

  8.   

    哦,明白了。就是你说的这点没看懂!你写的这段很有帮助,学生受教了。我用三层架构做的,经过大家的指点,终于做出来了。非常感谢shyhacker老师,也谢谢net_lover老师!
      

  9.   

    写错了,是这样 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
      {
         string name = this.dropdownlist.selectValue;
         BindView(name);
         this.dataview.DataBind();
      }有什么不懂的你说