要在页面加载方法里加上
if(!IsPostBack)
{
}
这样判断才能实现选哪个值时取的是哪个值,否则只能取到第一行的值
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection myConnection = new SqlConnection("Data Source=ZL4306;Initial Catalog=kk;User ID=sa");//Data Source=这里是数据库服务器的主机名,Initial Catalog=这里是数据库名,ID=这里是登陆用户名
SqlDataAdapter myCommand1 = new SqlDataAdapter("select dmsm1,dmz from kk_code where dmlb='6' ", myConnection);//连接数据库得到SqlDataAdapter
DataSet ds1 = new DataSet();
myCommand1.Fill(ds1, "dmsm1");//填充数据集,追加,把dmsm1的值追加到ds1里
DropDownList1.DataSource = ds1.Tables["dmsm1"].DefaultView;//绑定
DropDownList1.DataTextField = "dmz";//设置文本显示字段
DropDownList1.DataValueField = "dmz";//设置值显示
DropDownList1.DataBind(); //绑定后必须加的一句
DropDownList1.Items.Insert(0, "--请选择号牌种类--"); //这个是设置插入静态的数据
SqlDataAdapter myCommand2 = new SqlDataAdapter("select dmsm1,dmz from kk_code where dmlb='5' ", myConnection);//连接数据库得到SqlDataAdapter
DataSet ds2 = new DataSet();
myCommand2.Fill(ds2, "dmsm1");//填充数据集,追加,把dmsm1的值追加到ds2里
DropDownList2.DataSource = ds2.Tables["dmsm1"].DefaultView;//绑定
DropDownList2.DataTextField = "dmz";//设置文本显示字段
DropDownList2.DataValueField = "dmz";//设置值显示
DropDownList2.DataBind(); //绑定后必须加的一句
DropDownList2.Items.Insert(0, "--请选择违法类型--"); //这个是设置插入静态的数据
SqlDataAdapter myCommand3 = new SqlDataAdapter("select dmsm1,dmz from kk_code where dmlb='9' ", myConnection);//连接数据库得到SqlDataAdapter
DataSet ds3 = new DataSet();
myCommand3.Fill(ds3, "dmsm1");//填充数据集,追加,把dmsm1的值追加到ds3里
DropDownList3.DataSource = ds3.Tables["dmsm1"].DefaultView;//绑定
DropDownList3.DataTextField = "dmz";//设置文本显示字段
DropDownList3.DataValueField = "dmz";//设置值显示
DropDownList3.DataBind(); //绑定后必须加的一句
DropDownList3.Items.Insert(0, "--请选择车辆类型--"); //这个是设置插入静态的数据
SqlDataAdapter myCommand4 = new SqlDataAdapter("select dmsm1,dmz from kk_code where dmlb='8' ", myConnection);//连接数据库得到SqlDataAdapter
DataSet ds4 = new DataSet();
myCommand4.Fill(ds4, "dmsm1");//填充数据集,追加,把dmsm1的值追加到ds4里
DropDownList4.DataSource = ds4.Tables["dmsm1"].DefaultView;//绑定
DropDownList4.DataTextField = "dmz";//设置文本显示字段
DropDownList4.DataValueField = "dmz";//设置值显示
DropDownList4.DataBind(); //绑定后必须加的一句
DropDownList4.Items.Insert(0, "--请选择车身颜色--"); //这个是设置插入静态的数据
//myCommand4.Fill(ds4, "dmsm1");//填充数据集,追加,把dmsm1的值追加到ds4里
DropDownList5.DataSource = ds4.Tables["dmsm1"].DefaultView;//绑定
DropDownList5.DataTextField = "dmz";//设置文本显示字段
DropDownList5.DataValueField = "dmz";//设置值显示
DropDownList5.DataBind(); //绑定后必须加的一句
DropDownList5.Items.Insert(0, "--请选择车身颜色--"); //这个是设置插入静态的数据
}
}
protected void insert_ServerClick(object sender, EventArgs e)
{
//for(int i=1;i<13;i++)
//{
// if(new System.Web.UI.Control("CheckBox"+i)==true)
// {
// Response.Write("2222");
// }
//}
//string cc = this.DropDownList1.SelectedValue;
//string d = this.DropDownList1.SelectedItem.Text;
//string ee = this.DropDownList1.SelectedItem.Value;
//string gg = this.DropDownList1.SelectedItem.ToString();//这4个方式都可以取到值
string t_txt_yxq = "";
string v_txt_yxq = "";
string t_txt_wfxz = "";
string v_txt_wfxz = "";
string t_DropDownList1 = "";
string v_DropDownList1 = "";
string t_txt_hphm = "";
string v_txt_hphm = "";
string t_DropDownList2 = "";
string v_DropDownList2 = "";
string t_txt_fdjh = "";
string v_txt_fdjh = "";
string t_txt_clpp = "";
string v_txt_clpp = "";
string t_txt_cjh = "";
string v_txt_cjh = "";
string t_txt_syr = "";
string v_txt_syr = "";
string t_DropDownList3 = "";
string v_DropDownList3 = "";
string t_DropDownList4 = "";
string v_DropDownList4 = "";
string t_txt_bz = "";
string v_txt_bz = "";
string t_bm = "";
string v_bm = "";
string t_lrdw = "";
string v_lrdw = "";
string t_czy = "";
string v_czy = "";
string t_larq = "";
string v_larq = "";
//"+bm+""+lrdw+""+czy+""+larq+"
if (this.CheckBox1.Checked == true)
{
t_txt_yxq = "yxq,";
v_txt_yxq = "'"+this.txt_yxq.Value+"',";
}
if (this.CheckBox2.Checked == true)
{
t_txt_wfxz = "wfxz,";
v_txt_wfxz = "'"+this.txt_wfxz.Text + "',";
}
if (this.CheckBox3.Checked == true)
{
t_DropDownList1 = "hpzl,";
v_DropDownList1 = "'"+this.DropDownList1.SelectedValue.ToString() + "',";
}
if (this.CheckBox4.Checked == true)
{
t_txt_hphm = "hphm,";
v_txt_hphm = "'"+this.txt_hphm.Text + "',";
}
if (this.CheckBox5.Checked == true)
{
t_DropDownList2 = "bz,";
v_DropDownList2 = "'" + this.DropDownList2.SelectedValue.ToString() + "',";
}
if (this.CheckBox6.Checked == true)
{
t_txt_fdjh = "fdjh,";
v_txt_fdjh = "'" + this.txt_fdjh.Text + "',";
}
if (this.CheckBox7.Checked == true)
{
t_txt_clpp = "clpp,";
v_txt_clpp = "'" + this.txt_clpp.Text + "',";
}
if (this.CheckBox8.Checked == true)
{
t_txt_cjh = "cjh,";
v_txt_cjh = "'" + this.txt_cjh.Text + "',";
}
if (this.CheckBox9.Checked == true)
{
t_txt_syr = "jdcsyr,";
v_txt_syr = "'" + this.txt_syr.Text + "',";
}
if (this.CheckBox10.Checked == true)
{
t_DropDownList3 = "cllx,";
v_DropDownList3 = "'" + this.DropDownList3.SelectedValue.ToString() + "',";
}
if (this.CheckBox11.Checked == true)
{
t_DropDownList4 = "csys,";
string DropDownList4 = "" + this.DropDownList4.SelectedValue.ToString() + "";
string DropDownList5 = "" + this.DropDownList5.SelectedValue.ToString() + "";
v_DropDownList4 = "'" + DropDownList4 + DropDownList5 + "',";
}
if (this.CheckBox12.Checked == true)
{
t_txt_bz = "tjbz";
v_txt_bz = "'" + this.txt_bz.Text+"'";
}
string t_bm = "";
string v_bm = "";
string t_lrdw = "";
string v_lrdw = "";
string t_czy = "";
string v_czy = "";
string t_larq = "";
string v_larq = "";
if (Session["bm"] != null)
{
t_bm = "bm,";
v_bm = "'" + Session["bm"] + "',";
}
if (Session["dw"] != null)
{
t_bm = "lrdw,";
v_bm = "'" + Session["dw"] + "',";
}
if (Session["userName"] != null)
{
t_bm = "czy,";
v_bm = "'" + Session["userName"] + "',";
}
string t_larq = "";
string v_larq = "";
if (this.CheckBox1.Checked == false && this.CheckBox2.Checked == false &&this.CheckBox3.Checked == false && this.CheckBox4.Checked == false && this.CheckBox5.Checked == false && this.CheckBox6.Checked == false && this.CheckBox7.Checked == false && this.CheckBox8.Checked == false && this.CheckBox9.Checked == false && this.CheckBox10.Checked == false && this.CheckBox11.Checked == false && this.CheckBox12.Checked == false)
{
// Response.Write("3");
Response.Redirect("Register.aspx");
}
//"+bm+""+lrdw+""+czy+""+larq+"
string sql = "INSERT INTO KKHMD(FZ,ip," + t_txt_yxq + "" + t_txt_wfxz + "" + t_DropDownList1 + "" + t_txt_hphm + "" + t_DropDownList2 + "" + t_txt_fdjh + "" + t_txt_clpp + "" + t_txt_cjh + "" + t_txt_syr + "" + t_DropDownList3 + "" + t_DropDownList4 + "" + t_txt_bz + ") VALUES('0','" + Request.UserHostAddress + "'," + v_txt_yxq + "" + v_txt_wfxz + "" + v_DropDownList1 + "" + v_txt_hphm + "" + v_DropDownList2 + "" + v_txt_fdjh + "" + v_txt_clpp + "" + v_txt_cjh + "" + v_txt_syr + "" + v_DropDownList3 + "" + v_DropDownList4 + "" + v_txt_bz + ") ";
Response.Write(sql);
SqlDataAdapter cmd = new SqlDataAdapter(sql, conn);
DataTable dt = new DataTable();
cmd.Fill(dt);
//if(this.CheckBox2.Checked==true)
//{
// string sql = "insert into kkhmd(fz,yxq,wfxz,hphm,fdjh,clpp,cjh,jdcsyr,bz) values('0','" + this.txt_yxq.Value + "','" + this.txt_wfxz.Text + "','" + txt_hphm.Text + "','" + txt_fdjh.Text + "','" + txt_clpp.Text + "','" + txt_cjh.Text + "','" + txt_syr.Text + "','" + txt_bz.Text + "')";
// SqlDataAdapter cmd = new SqlDataAdapter(sql, conn);
// DataTable dt = new DataTable();
// cmd.Fill(dt);
//}
}
黑色头发 http://heisetoufa.iteye.com