DataBind练着玩呢

 

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected string GetStyleOfbody()
    {
        return this.DropDownList1.SelectedValue;
       
    }
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack)
        {
              NewMethod();
              NewMethod1();

        }
       
     
    }

    private void NewMethod1()
    {
        string str1 = "server=.;database=DataBind;integrated security=sspi";
        DataSet ds1 = new DataSet();
        using (SqlConnection sqlcnn1 = new SqlConnection())
        {
            sqlcnn1.ConnectionString = str1;
            SqlCommand sqlcmm1 = sqlcnn1.CreateCommand();
            sqlcmm1.CommandText = "select id,name from province";
            SqlDataAdapter da1 = new SqlDataAdapter(sqlcmm1);
            da1.Fill(ds1);
        }
        this.RadioButtonList1.DataSource = ds1.Tables[0];
        this.RadioButtonList1.DataTextField = "name";
        this.RadioButtonList1.DataValueField = "id";
        this.DataBind();
    }

    private void NewMethod()
    {
        string str = "server=.;database=DataBind;integrated security=sspi";
        DataSet ds = new DataSet();
        using (SqlConnection sqlcnn = new SqlConnection())
        {
            sqlcnn.ConnectionString = str;
            SqlCommand sqlcmm = sqlcnn.CreateCommand();
            sqlcmm.CommandText = "select id, name from province";
            SqlDataAdapter da = new SqlDataAdapter(sqlcmm);
            da.Fill(ds);
        }
        this.DropDownList1.DataSource = ds.Tables[0];
        this.DropDownList1.DataTextField = "name";
        this.DropDownList1.DataValueField = "id";
        this.DataBind();
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string str = "server=.;database=DataBind;integrated security=sspi";
        DataSet ds = new DataSet();
        using (SqlConnection sqlcn = new SqlConnection())
        {
            sqlcn.ConnectionString = str;
            SqlCommand sqlcm = sqlcn.CreateCommand();
            sqlcm.CommandText = "select id,myid,name from city where id=@id";
            sqlcm.Parameters.Add(new SqlParameter("@id", this.DropDownList1.SelectedValue));
            sqlcn.Open();
            SqlDataAdapter sda = new SqlDataAdapter(sqlcm);
            sda.Fill(ds);
          
          
        }
        this.DropDownList2.DataSource = ds.Tables[0];
        this.DropDownList2.DataTextField = "name";
        this.DropDownList2.DataValueField = "id";
        this.DataBind();
    }
    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string str = "server=.;database=DataBind;integrated security=sspi";
        DataSet ds = new DataSet();
        using (SqlConnection sqlcn = new SqlConnection())
        {
            sqlcn.ConnectionString = str;
            SqlCommand sqlcm = sqlcn.CreateCommand();
            sqlcm.CommandText = "select id,myid,name from city where id=@id";
            sqlcm.Parameters.Add(new SqlParameter("@id", this.RadioButtonList1.SelectedValue));
            sqlcn.Open();
            SqlDataAdapter sda = new SqlDataAdapter(sqlcm);
            sda.Fill(ds);
          
          
        }
        this.RadioButtonList2.DataSource = ds.Tables[0];
        this.RadioButtonList2.DataTextField= "name";
        this.RadioButtonList2.DataValueField = "id";
        this.DataBind();
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值