protected string strst=string.Empty;
protected string stret=string.Empty;
protected int strid=0;
protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.BindData();
            }
        }
protected void BindData()
        {
            strst = "AAAAAAAA";
            stret = "BBBBBBBB";
            strid=10;
        }
 protected void btn_Click(object sender, EventArgs e)
        {           
            if (!string.IsNullOrEmpty(strst) && !string.IsNullOrEmpty(stret) && strid > 0)
            {
                Response.Redirect("Result.aspx");
                Response.End();
            }
            else
            {
                Response.Write (strst+"-"+stret+"-"+strid);
            }
        }btn_Click这里的值都是初始值,在BindData()赋值都没起作用,为什么?

解决方案 »

  1.   

    因为你的赋值是在
    if (!this.Page.IsPostBack)
    里面进行的,

    btn_Click
    是Page.IsPostBack的,因此你的赋值没有执行
      

  2.   

    理解了 谢谢
    如果希望得到赋值,是在前台加3个隐藏的控件并在BindData里对这三控件赋值,然后 btn_Click的时候读这三控件的值 效率高还是把if (!this.Page.IsPostBack)这个去掉 好?
      

  3.   

    页面初始化执行BindData这个绑定事件。在点button就不执行了 改成ViewState 存放
     protected void BindData()
        {
            ViewState["strst"] = "AAAAAAAA";
            ViewState["stret"] = "BBBBBBBB";
            ViewState["strid"] = 10;
        }    protected void Button4_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ViewState["strst"].ToString()) && !string.IsNullOrEmpty(ViewState["stret"].ToString()) && int.Parse(ViewState["strid"].ToString()) > 0)
            {
                Response.Write("<script>alert('修改成功')</script>");
                Response.End();
            }
            else
            {
                Response.Write(ViewState["strst"] + "-" + ViewState["stret"] + "-" + ViewState["strid"]);
            }    }