您现在的位置: 无忧电子商务网 >> 信息学院 >> 程序开发 >> asp.net >> 正文

分享个极好的无刷新二级联动下拉列表,同样适用与firefox

作者:作者:未…    信息学院来源:网络收集    点击数:    更新时间:2006-8-28 我要参与讨论

  </HTML>

后台webform1.aspx.cs:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Data.SqlClient;

namespace drop
{
 /// <summary>
 /// WebForm1 的摘要说明。
 /// </summary>
 public class WebForm1 : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.DropDownList dListParent;
  protected System.Web.UI.WebControls.Button Button1;
  protected System.Web.UI.WebControls.DropDownList dListChild;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   //if(!IsPostBack)
   //{
    BindDrop();//如果不是提交回来就绑定列表框
   //}
  }

  protected void BindDrop()
  {
   //首先我想父dropdownlist也绑定数据库,后面想没必要
   //if(!IsPostBack)
   //{
    //绑定父dListParent
   // BindParent();
   //}
     //获得传递过来的parent_id值,如果是第一次请求他为null
   string str = Request.QueryString["parent_id"];
   string str1 = dListParent.SelectedValue;;
   Response.Write(str1);
   //如果str加个字符串!=原来的字符串则说明触发过dListParent的onchange事件
   if((str+"abc")!="abc")
   {
      //绑定 dListChild控件
    BindChild(str);//把传来的父DropDownList的value做为参数
      }
   else
    BindParent(str1);
  }


  protected void BindParent(string str)
  {
   //如果是第一次请求或者是刷新这个页面则根据dListParent的值来选择
   //把参数转化成int
   int i = Convert.ToInt32(str);
   dListChild.Items.Clear();
   dListChild.Items.Add(new ListItem("全部型号","0"));
   //得到数据库连接字符串
   string connStr = ConfigurationSettings.AppSettings["ConnString"].ToString();
   //初始化个conn对象
   SqlConnection conn = new SqlConnection(connStr);
   //数据库语句
   string commStr = string.Format("select type_value,type_text from phone_type where parent_id={0}",i);
   //建立数据库命令对象
   SqlCommand comm = new SqlCommand(commStr,conn);
   //打开数据库
   conn.Open();
   //执行命令
   SqlDataReader dr = comm.ExecuteReader();
   //循环dr,给dListParent添加条目
   while(dr.Read())
   {
      dListChild.Items.Add(new ListItem(dr[1].ToString(),dr[0].ToString()));
    //也可以这样
    //dListParent.Items.Add(new

上一页  [1] [2] [3] [4] [5] 下一页

在google里搜索更多分享个极好的无刷新二级联动下拉列表,同样适用与firefox

Google
Web www.51ec.org
【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
我来说两句 对此文章发表了评论
  昵 称: *必填    ·注册用户·
  评 分: 1分 2分 3分 4分 5分     严禁发表危害国家安全、政治、黄色淫秽等内容的评论,用户需对自己在使用本网站服务过程中的行为承担法律责任。本站管理员有权保留或删除评论内容,评论内容只代表机友个人观点,与本网站立场无关。  
评 论
内 容

 
评论列表 (最新 评论仅限网友观点!)

供求信息




| 设为首页 | 加入收藏 | 关于我们 | 广告服务 | 联系方式 | 友情链接 | 版权申明