如何检查按钮点击一般方法?

时间:2015-04-20 08:39:48

标签: c#

我试过下面的

public partial class PaymentSearch : System.Web.UI.Page
{
   private bool button1WasClicked = false;


   protected void linkToday_Click(object sender, EventArgs e)
   {
      button1WasClicked = true;
    }

  protected void ddlRecordPayment_SelectedIndexChanged(object sender, EventArgs e)
  {
  gridAllPaymentBind(1);
  }
   public void gridAllPaymentBind(int pageIndex)
   {
   if (button1WasClicked == true)
  {
   result = 3;
    command.Parameters.AddWithValue("@result", result);
   }

 }

但是在' gridAllPaymentBind'方法' button1WasClicked'在下拉列表更改事件触发之前我点击链接按钮的地方获取虚假值。

2 个答案:

答案 0 :(得分:0)

ASP.NET本质上是无状态的,因此变量值不会在回发之间保持不变。因此,为了使其持久,您可以使用SessionViewState

public partial class PaymentSearch : System.Web.UI.Page
{
   private bool button1WasClicked
   {
       get { return (bool)Session["button1WasClicked"]; }
       set { Session["button1WasClicked"] = value; }
   }

   protected void linkToday_Click(object sender, EventArgs e)
   {
       button1WasClicked = true;
   }

  protected void ddlRecordPayment_SelectedIndexChanged(object sender, EventArgs e)
  {
      gridAllPaymentBind(1);
  }

  public void gridAllPaymentBind(int pageIndex)
  {
      if (button1WasClicked == true)
      {
         result = 3;
        command.Parameters.AddWithValue("@result", result);
      }
  }
}

答案 1 :(得分:0)

尝试如下...

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack == true)
            {
                Session["button1WasClicked "] = false;
}
}


   protected void linkToday_Click(object sender, EventArgs e)
   {
       Session["button1WasClicked "] = true;
    }

  protected void ddlRecordPayment_SelectedIndexChanged(object sender, EventArgs e)
  {
  gridAllPaymentBind(1);
  }
   public void gridAllPaymentBind(int pageIndex)
   {
bool button1WasClicked = (bool)Session["button1WasClicked "];
   if (button1WasClicked == true)
  {
   result = 3;
    command.Parameters.AddWithValue("@result", result);
   }

 }
希望这会对你有所帮助。祝你好运。