2016-12-20 26 views
0

這段代碼似乎在我的朋友的網頁上可以正常工作,但是當我嘗試使用'=='運算符或者.Equals函數比較按鈕時,它不會進入如果聲明。從gridview上運行的foreach中獲得null來比較按鈕

protected void Button1_Click(object sender, EventArgs e) 
{ 
    int orderid = 0; 
    foreach (GridViewRow gvr in GridViewOrders.Rows) 
    { 
     Button btn = (Button)gvr.FindControl("ButtDetailedView"); 
     if (btn == (Button)sender) 
      orderid = int.Parse(gvr.Cells[0].Text); 
    } 
    if (orderid != 0) 
    { 
     Response.Redirect("ViewOrderDetails.aspx?OrderId=" + orderid); 
    } 
} 
+1

爲什麼不使用((按鈕)發送器),請將.Name代替找到的控制和 比較對象引用? – dgorti

+0

你是否也將此事件'Button1_Click'綁定到其他按鈕? –

回答

0

一種可能是,你的Button1_Click事件勢必Button1的勢必ButtDetailedView。因此,發送者不能ButtDetailedView和你的比較:

Button btn = (Button)gvr.FindControl("ButtDetailedView"); 
if (btn.Equals((Button)sender)) 

回報false