现在的位置: 首页 > 综合 > 正文

关于Page.IsPostBack的使用问题

2013年02月27日 ⁄ 综合 ⁄ 共 884字 ⁄ 字号 评论关闭

问题:点击修改按钮时不能修改数据库中的内容。

错误代码:  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
    BindCompanyData();
  }

  private void btnUpdate_Click(object sender, System.EventArgs e)
  {
   YLWeb.Class.Company company=new YLWeb.Class.Company();
   try
   {
    company.UpdateProfile(this.ftbProfile.Text);
    Response.Write("<script>window.alert('该公司简介已成功修改!');location.replace('UpdateProfile.aspx')</script>");
   }
   catch(Exception ex)
   {
    Response.Redirect("~/Manage/ErrorPage.aspx?ErrorUrl="
     + YLWeb.Class.WebSystem.RedirectErrorUrl(Request.RawUrl)
     + "&ErrorMessage=" + ex.Message.Replace("/n"," "));
   }
  }

原因:数据绑定函数BindCompanyData()载入前没有作Page.IsPostBack判断,则在点击添加按钮时重新加载BindCompanyData()函数,把输入框的内容重新覆盖了,再运行添加按钮的事件,修改数据库,所以数据库内容没有作出修改。

正确代码:     private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   if(!Page.IsPostBack)
   {
    BindCompanyData();
   }
  }

抱歉!评论已关闭.