私は次の表を持っています:
| product_no | product_name | price |
=================================================
| 10000 | teapot | 5.00 |
| 10001 | grasspot | 2.00 |
| 10002 | glasspot | 10.00 |
| 10003 | jackpot | 15.00 |
2つのテキストフィールドがあります-m_pname
とm_pprice
。
これらの2つのフィールドには、ユーザーがドロップダウンリストから製品名を選択したときに製品名と価格が表示されます-dropdownlist1
両方のテキストフィールドは編集可能であり、ユーザーがそれらを編集してボタンをクリックするとModify
、Button4
ボタンがデータベースを更新します。
ただし、ここで問題が発生します。データベースは更新されずdropdownlist1
、古い値が反映されたままになります。
チェックボックスdropdownlist1
をオンにします。Autopostback
それで、これはAutopostback
チェックボックスが原因かどうか疑問に思いました。
コードの一部は次のとおりです。
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = ConnectDB("Data Source=WR2\\SQLEXPRESS;Initial Catalog=testbase;Integrated Security=True");
try
{
conn.Open();
SqlDataReader testtable = SQLReadCommand(conn, "Select * from testtable where product_no='" + DropDownList1.SelectedValue + "'");
if (testtable.HasRows)
{
while (testtable.Read())
{
m_pname.Text = testtable["product_name"].ToString();
m_pprice.Text = testtable["price"].ToString();
modify_status.Text = modify_status.Text + "[ Name: " + m_pname.Text + " Price: " + m_pprice.Text + " ]";
}
}
testtable.Close();
//modify_status.Text = "";
conn.Close();
}
catch (Exception err)
{
modify_status.Text = err.Message;
}
}
protected void Button4_Click(object sender, EventArgs e)
{
SqlConnection conn = ConnectDB("Data Source=WR2\\SQLEXPRESS;Initial Catalog=testbase;Integrated Security=True");
try
{
conn.Open();
String vpname = m_pname.Text;
SQLWriteCommand(conn, "UPDATE testtable SET product_name = '" + m_pname.Text + "', price = " + m_pprice.Text + " WHERE product_no = " + DropDownList1.SelectedValue);
modify_status.Text = "Changed " + DropDownList1.SelectedValue + " to " + vpname;
conn.Close();
}
catch (Exception err)
{
modify_status.Text = err.Message;
}
}