2017-11-29 24 views
3

私のデータベースにデータを保存しようとしていますが、ドロップダウンで選択したアイテムのID(主キー)を取得しようとしています。ドロップダウンで選択した値のIDを取得

常に同じIDを保存しています。選択したアイテムの最初のIDのみが保存されます。それは

protected void Page_Load(object sender, EventArgs e) 
{ 
    SqlConnection con = new SqlConnection(
    WebConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString); 
    con.Open(); 
    SqlCommand cmd = new SqlCommand("Select UserRoleID, RoleType from tb_UserRoles", con); 
    DropDownList1.DataSource = cmd.ExecuteReader(); 
    DropDownList1.DataTextField = "RoleType"; 
    DropDownList1.DataValueField = "UserRoleID"; 
    DropDownList1.DataBind(); 
} 

protected void Button1_Click(object sender, EventArgs e) 
{ 
    SqlConnection con = new SqlConnection(
    WebConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString); 
    con.Open(); 
    SqlCommand cmd = new SqlCommand("sp_UserInsertUpdate", con); 
    cmd.CommandType = System.Data.CommandType.StoredProcedure; 
    String ID = DropDownList1.SelectedValue; 
    cmd.Parameters.AddWithValue("UserID", (hfUserID.Value == "" ? 0 : Convert.ToInt32(hfUserID.Value))); 
    cmd.Parameters.AddWithValue("UserRoleID", ID); 
    cmd.Parameters.AddWithValue("Username", TextBox1.Text); 
    cmd.Parameters.AddWithValue("Password", TextBox2.Text); 
    cmd.Parameters.AddWithValue("Email", TextBox3.Text); 
    cmd.Parameters.AddWithValue("DateCreate", TextBox4.Text); 
    cmd.ExecuteNonQuery(); 
    con.Close(); 
} 

答えて

2

たびは、ボタンをクリックするので、これはダウン私はドロップで選択した内容に基づいて異なるIDが保存されますように、どのように私は私のコードを調整することができます。クリックイベントが発生しますが、それ以前にはページロードイベントとページロードイベントが実行され、バインディングドロップダウンのロジックが追加されています。ボタンをクリックするたびに、ドロップダウンリストが最初にロードされるたびにクリックイベントが発生するたびに、そのことを意味します。この目的のためには、IsPostBackを使用して条件を追加するだけです。

は、ページが最初の負荷にあるかどうかを判断しますAsp.Netページのプロパティです。この

IsPostBackプロパティてみ

protected void Page_Load(object sender, EventArgs e) 
{ 
    if(!IsPostBack) //Added 
    { 
    SqlConnection con = new SqlConnection(
    WebConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString); 
    con.Open(); 
    SqlCommand cmd = new SqlCommand("Select UserRoleID, RoleType from tb_UserRoles", con); 
    DropDownList1.DataSource = cmd.ExecuteReader(); 
    DropDownList1.DataTextField = "RoleType"; 
    DropDownList1.DataValueField = "UserRoleID"; 
    DropDownList1.DataBind(); 
    } 
} 
2

以下のように変更を行い、ユーザーの場合あなたのウェブページ上のボタンを実行して、そのページを自分自身にポストバックさせる。ポストバックは、ASPがビューの状態からコントロールのプロパティの値を復元する要求です。

ますので、確認する必要があり!IsPostBack

protected void Page_Load(object sender, EventArgs e) 
{ 
if(!IsPostBack) // Add this If condition 
{ 
    SqlConnection con = new SqlConnection(
    WebConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString); 
    con.Open(); 
    SqlCommand cmd = new SqlCommand("Select UserRoleID, RoleType from tb_UserRoles", con); 
    DropDownList1.DataSource = cmd.ExecuteReader(); 
    DropDownList1.DataTextField = "RoleType"; 
    DropDownList1.DataValueField = "UserRoleID"; 
    DropDownList1.DataBind(); 
} 
} 
protected void Button1_Click(object sender, EventArgs e) 
{ 
    SqlConnection con = new SqlConnection(
    WebConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString); 
    con.Open(); 
    SqlCommand cmd = new SqlCommand("sp_UserInsertUpdate", con); 
    cmd.CommandType = System.Data.CommandType.StoredProcedure; 
    String ID = DropDownList1.SelectedValue; 
    cmd.Parameters.AddWithValue("UserID", (hfUserID.Value == "" ? 0 : Convert.ToInt32(hfUserID.Value))); 
    cmd.Parameters.AddWithValue("UserRoleID", ID); 
    cmd.Parameters.AddWithValue("Username", TextBox1.Text); 
    cmd.Parameters.AddWithValue("Password", TextBox2.Text); 
    cmd.Parameters.AddWithValue("Email", TextBox3.Text); 
    cmd.Parameters.AddWithValue("DateCreate", TextBox4.Text); 
    cmd.ExecuteNonQuery(); 
    con.Close(); 
} 
関連する問題