我有下表:
| product_no | product_name | price |
=================================================
| 10000 | teapot | 5.00 |
| 10001 | grasspot | 2.00 |
| 10002 | glasspot | 10.00 |
| 10003 | jackpot | 15.00 |
我有两个文本字段 -m_pname
和m_pprice
.
当用户从下拉列表中选择产品名称时,这两个字段将显示产品名称和价格 -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;
}
}