问题描述
<select önchange="getCountry('country');" id="country" name = "country"><select>
function getCountry(x)
{
var y=document.getElementById(x).value;
document.getElementById('ctl00_ContentPlaceHolder1_lblCountry').innerHTML =y;
}
function load()
{
document.getElementById('country').value = document.getElementById('ctl00_ContentPlaceHolder1_lblCountry').innerHTML;
}
protected void Page_Load(object sender, EventArgs e)
{
//if(!ISPostBack)
//{
string connectionString = "Data Source = .\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security = True;Connect Timeout=30;User Instance = True";
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectionString);
System.Data.SqlClient.SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT * FROM [product] where ID = '1'";
conn.Open();
System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
this.lblCountry.Text = dr["country"].ToString();
}
//}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
string connectionString = "Data Source = .\\SQLEXPRESS;" + "AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security = True;Connect Timeout=30;User Instance = True";
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectionString);
System.Data.SqlClient.SqlCommand cmd = conn.CreateCommand();
cmd.Parameters.Add("country", SqlDbType.VarChar);
cmd.Parameters["country"].Value = this.lblCountry.Text; //retrieve as 'Japan' Instead of 'Korea'
cmd.CommandText = "UPDATE [product] SET [country] = @country WHERE [ID] = '1'";
conn.Open();
int numberOfRows = cmd.ExecuteNonQuery();
conn.Close();
}
1.在页面加载时,SELECT下拉列表显示为"Japan",如lblCountry.
2.如果我将项目从日本"更改为韩国",lblCountry也将更新为韩国".
3.单击btnUpdate,页面将再次加载,但仍然显示"Japan"而不是"Korea".检查数据库,它仍然保留为日本".在asp:dropdownlist上使用了相同的代码/查询,并且按预期工作.
对这个问题有什么建议吗? :)
1. On page load, the SELECT dropdownlist is show ''Japan'', so as lblCountry.
2. If i change the Item from ''Japan'' to ''Korea'', lblCountry also will update to ''Korea''.
3. btnUpdate been click, page will load again, but it stills show ''Japan'' instead of ''Korea''. Check at database, it still remains as ''Japan''. The same code/query been used on asp:dropdownlist and it works as expected.
Any advice for this question please? :)
推荐答案
document.getElementById('ctl00_ContentPlaceHolder1_lblCountry').innerHTML =y;
document.getElementById('ctl00_ContentPlaceHolder1_lblCountry').value=y;
document.getElementById('ctl00_ContentPlaceHolder1_lblCountry').text=y;
它对我来说很好用,并且还在IsPostback中包含了我们的页面加载定义.
it works fine for me and also include ur page load definations in IsPostback.
It works well for me.
cmd.Parameters.AddWithValue("@country", this.lblcountry.Text);
替换:
to replace:
cmd.Parameters.Add("country", SqlDbType.VarChar);
cmd.Parameters["country"].Value = this.lblCountry.Text;
这篇关于[SELECT]无法更新的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!