我有一个表格来编辑数据库值。我在页面加载时使用ClientScriptManager.RegisterClientScriptBlock
注册了一个json变量,然后使用它来检查表单的更改并提示用户进行保存。然后,在“保存”按钮单击事件中,我重新执行注册json变量的代码,以使其包含用户刚刚保存的更改。
我遇到的问题是json变量未更新以反映回发后表单的更改。只有刷新页面后,json变量才会更新。
protected void Page_Load(object sender, EventArgs e)
{
Property p = Property.GetPropertyById(id);
PopulateForm(p);
CreateJson(p);
}
protected void btnSave_Click(object sender, EventArgs e)
{
Property p = Property.GetPropertyById(id);
p.Price = txtPrice.Text.Trim();
p.Save();
CreateJson(p);
}
private void CreateJson(Property p)
{
StringBuilder JSONtext = new StringBuilder();
JSONtext.Append("<script type='text/javascript'>");
JSONtext.Append("var property = ");
JSONtext.Append("{");
JSONtext.Append("\"price\":\"");
JSONtext.Append(p.Price.ToString());
JSONtext.Append("};");
JSONtext.Append("</script>");
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "jsonVariable", JSONtext.ToString());
}
我想做的甚至有可能吗?
答案 0 :(得分:0)
尝试一下
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
Property p = Property.GetPropertyById(id);
PopulateForm(p);
CreateJson(p);
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
Property p = Property.GetPropertyById(id);
p.Price = txtPrice.Text.Trim();
p.Save();
CreateJson(p);
PopulateForm(p);
}
它在创建PopulateForm(p)
之后执行json
,在Page_Load
中,它仅在不是PostBack
的情况下运行