usersid = Page.Request["id"]的调用问题
我在page_load()中写入 string usersid = Page.Request[ "id "];绑定员工编号,
为什么会出现usersid不存在呢?
private void BindTextBox()
{
string str = "select * from users "
+ "where users_id=usersid "; //usersid不存在
com=new SqlCommand(str,conn);
conn.Open();
dr=com.ExecuteReader();
while(dr.Read())
{
users_id.Text = Convert.ToString(dr[ "users_id "]);
username.Text = Convert.ToString(dr[ "username "]);
if(dr[ "sex "].ToString()== "男 ")
sex.SelectedValue= "男 ";
else
sex.SelectedValue= "女 ";
}
dr.Close();
conn.Close();
}
private void Button1_Click(object sender, System.EventArgs e)
{
// string usersid = Page.Request[ "id "];
string sqlstr= "update users set ";
sqlstr+= "username= ' "+ username.Text + " ' ";
sqlstr+= "sex= ' " + sex.SelectedItem.Value + " ' ";
sqlstr+= "gra_time= ' " + gra_time.Text + " ' ";
sqlstr+= " update dep set dep_name = ' " + dep_name.SelectedItem.Value + " ' where users_id=usersid ";
sqlstr+= "users.dep_id=dep.dep_id ";
com=new SqlCommand(sqlstr,conn);
conn.Open();
com.ExecuteNonQuery();
conn.Close();
BindTextBox();
}
private void Button2_Click(object sender, System.EventArgs e)
{
// string usersid = Page.Request[ "id "];
string sqlstr;
sqlstr= "delete from users where users_id=usersid ";
com=new SqlCommand(sqlstr,conn);
conn.Open();
com.ExecuteNonQuery();
conn.Close();
Response.Redirect( "default.aspx ");
}
}
}
------解决方案--------------------定義全局變量 public string usersid = Page.Request[ "id "];
string str = "select * from users "
+ " where users_id= "+usersid ;