前台代码:
<html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> <script src="../js/jquery-1.4.1.min.js" type="text/javascript"></script> <link href="../../Content/main.css" rel="stylesheet" type="text/css" /> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:UpdatePanel runat="server" ID="UpdatePannel1"> <ContentTemplate> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> </asp:DropDownList> <asp:DropDownList ID="DropDownList2" runat="server"> </asp:DropDownList> <%-- <asp:DropDownList ID="DropDownList3" runat="server"> </asp:DropDownList>--%> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" /> <%--<asp:AsyncPostBackTrigger ControlID="DropDownList2" EventName="SelectedIndexChanged" />--%> </Triggers> </asp:UpdatePanel> </form> </body> </html>
下边这是后台:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using System.Data.OleDb; 8 using System.Data; 9 using System.Configuration; 10 11 namespace TaovoAll.JiLian 12 { 13 public partial class WebForm1 : System.Web.UI.Page 14 { 15 16 17 private BLL.UserServer userServer = new BLL.UserServer(); 18 protected void Page_Load(object sender, EventArgs e) 19 { 20 21 22 if (!IsPostBack) 23 { 24 25 bindDropDownList1(); 26 27 } 28 29 30 } 31 32 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 33 { 34 IList<Models.Userinfo> user = userServer.GetAllPlayType(); 35 DropDownList2.DataSource = user; 36 DropDownList2.DataBind(); 37 DropDownList2.DataTextField = "plty_playtypename"; 38 DropDownList1.DataValueField = "plty_lotteryid"; 39 DropDownList2.Items.Insert(0, new ListItem("--请选择--")); 40 41 42 } 43 44 45 46 public void bindDropDownList1() 47 { 48 49 IList<Models.Userinfo> user = userServer.GetAllLottery(); 50 DropDownList1.DataSource = user; 51 DropDownList1.DataTextField = "loty_lotteryname"; 52 DropDownList1.DataValueField = "loty_id"; 53 DropDownList1.DataBind(); 54 DropDownList1.Items.Insert(0, new ListItem("--请选择--")); 55 56 } 57 58 } 59 }
这个是数据访问层代码
//获得彩票类型 public IList<Models.Userinfo> GetAllLottery() { string sql = "select loty_id,loty_lotteryname from Tab_LotteryType"; List<Models.Userinfo> list = new List<Models.Userinfo>(); using (SqlDataReader reader = DBUtility.SQLHelper.ExecuteQuery(System.Data.CommandType.Text, sql, null)) { while (reader.Read()) { Models.Userinfo users = new Models.Userinfo( reader.GetByte(0),reader.GetString(1) ); list.Add(users); } return list; } } //获得玩法 public IList<Models.Userinfo> GetAllPlayType() { string sql = "select plty_playtypeid,plty_playtypename,plty_lotteryid from Tab_PlayType"; List<Models.Userinfo> list = new List<Models.Userinfo>(); using (SqlDataReader reader = DBUtility.SQLHelper.ExecuteQuery(System.Data.CommandType.Text, sql, null)) { while (reader.Read()) { Models.Userinfo users = new Models.Userinfo( reader.GetInt32(0), reader.GetString(2) , reader.GetInt32(1) ); list.Add(users); } return list; } }
为什么点击DropDownList1下拉数据回报错 ,那位高手指点下谢谢
那个是错误页面
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 33 { 34 IList<Models.Userinfo> user = userServer.GetAllPlayType(); 35 DropDownList2.DataSource = user; 36 DropDownList2.DataBind(); 37 DropDownList2.DataTextField = "plty_playtypename"; 38 DropDownList1.DataValueField = "plty_lotteryid"; 39 DropDownList2.Items.Insert(0, new ListItem("--请选择--")); 42 }
你看一下标红的地方。这里估计有错误。你先修改了再看看
已经实现了,为 什么不联动啊
@yujinchao: 参看 http://q.cnblogs.com/q/15813
你绑定数据源是,没有作关联查询。肯定不联动