2016-12-20 24 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