Crdown's学习笔记

ASP.NET,Report...

博客园 首页 新随笔 联系 订阅 管理

给你参考一下: 
protected void Page_Load(object sender, EventArgs e) 
    { 
        if (!IsPostBack) 
        { 
            SqlConnection con = new SqlConnection("server=;database=province;uid=sa;pwd=sa;"); 
            con.Open(); 
            SqlCommand cmd = new SqlCommand("select * from province", con); 
            SqlDataReader sdr = cmd.ExecuteReader(); 
            DropDownList1.DataSource = sdr; 
            DropDownList1.DataTextField = "proname"; 
            DropDownList1.DataValueField = "proid"; 
            DropDownList1.DataBind(); 
            con.Close(); 
            sdr.Close(); 
        } 
        
        
    } 
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
      
            SqlConnection con = new SqlConnection("server=;database=province;uid=sa;pwd=sa;"); 
            con.Open(); 
            SqlCommand cmd2 = new SqlCommand("select * from city where proid=" + DropDownList1.SelectedValue, con); 
            SqlDataReader sdr2 = cmd2.ExecuteReader(); 
            DropDownList2.DataSource = sdr2; 
            DropDownList2.DataTextField = "cityname"; 
            DropDownList2.DataValueField = "cityid"; 
            DropDownList2.DataBind(); 
            DropDownList2.Visible = true; 
            sdr2.Close(); 
            con.Close(); 
        
    } 

 

 

前台:
<td class="td_page">新闻类型:</td>
<td><asp:DropDownList ID="dropNewType" runat="server"></asp:DropDownList></td>
 
后台:
DataTable dt = CDB.FillTable("select TypeId, TypeContent from [newsTypeTable]");
dropNewType.DataSource = dt;
dropNewType.DataTextField = "typeContent";
dropNewType.DataValueField = "TypeId";
dropNewType.DataBind();

posted on 2010-03-27 16:44  Crdown  阅读(2114)  评论(0编辑  收藏  举报