学生求教:dropdownlist选择不同值时gridview绑定一个数据库里多个表的问题 我用的VS2005,Web窗体上建立了dropdownlist控件和一个gridview控件,dropdownlist控件里面有不同的items项目。我是想在选择dropdownlist不同选项的时候,在同一个gridview里面显示对应数据库表的信息。应该做怎样的绑定? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 直接拷贝执行下面的代码即可http://dotnet.aspx.cc/article/d94323a7-e322-4ead-9f25-6e6629c8012e/read.aspx 也可以<%@ 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的那个做成你想要查询的条件, 恩,这个可以绑定数据库里面的一张表吧。我的意思是不同的dropdownlist.selectedvalue值调用不同的查询数据库函数,然后在gridview里面显示。注意:是点击dropdownlist的时候获取值,然后gridview显示对应的查询函数所查询的表里面存储的信息。 恩,这个可以绑定数据库里面的一张表吧。我的意思是不同的dropdownlist.selectedvalue值调用不同的查询数据库函数,然后在gridview里面显示。注意:是点击dropdownlist的时候获取值,然后gridview显示对应的查询函数所查询的表里面存储的信息。 他的意思是让你根据不同的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>有更多的选项就写更多的条件分支 或者这样写成方法,也跟根据你自己的问题写不同的条件<%@ 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> 哦,明白了。就是你说的这点没看懂!你写的这段很有帮助,学生受教了。我用三层架构做的,经过大家的指点,终于做出来了。非常感谢shyhacker老师,也谢谢net_lover老师! 写错了,是这样 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { string name = this.dropdownlist.selectValue; BindView(name); this.dataview.DataBind(); }有什么不懂的你说 C# 问串口问题() ListView1控件中唯一标识某一个项或某一列的是什么属性 TransactionScope超过10分钟出现Unable to get error message (6107) (0) 对文本加密用什么算法好? 给介绍一本c#入门书啊。。。 安装问题,郁闷! VS.Net应用:如何将现有文件夹添加到项目中? 求水晶报表的部署问题,在线等 请问在c#中如何取得驱动器的详细信息啊 NET WebBrowser 模拟点击时页面出现多个相同ID的超链接 优先执行问题 很奇怪的问题,WPF程序,界面假死,操作可以执行,只是界面不显示
http://dotnet.aspx.cc/article/d94323a7-e322-4ead-9f25-6e6629c8012e/read.aspx
<%@ 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>
把vlae的那个做成你想要查询的条件,
比如你的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>
有更多的选项就写更多的条件分支
<%@ 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>
{
string name = this.dropdownlist.selectValue;
BindView(name);
this.dataview.DataBind();
}有什么不懂的你说