2015-04-20 74 views
0

我曾嘗試下面如何檢查一般方法中點擊的按鈕?

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); 
    } 

} 

但在「button1WasClicked」讓我在那裏alredy下拉列表改變事件火中單擊鏈接按鈕假值「gridAllPaymentBind」梅索德。

+2

最好在會話中放置該布爾值。 –

+0

您是否在單擊時檢查linkToday_Click方法是否被調用? –

+0

把'private bool button1WasClicked = false;'在'Page.IsPostPack'內檢查,因爲每當頁面回傳值改變爲false時 –

回答

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); 
    } 

} 

希望這將有助於you..good運氣。

+0

aah..very very hanq sir..now我得到了n我失去了壓力... –

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); 
     } 
    } 
} 
相關問題