2010-03-31 79 views
0

此事件中的代碼正好在另外兩個事件處理程序中重複。如何將重複的代碼放入方法中並從事件處理程序中調用該方法,因此我只需將其保存在一個地方?我不知道如何將事件參數傳遞給調用方法。摘要出重複代碼

protected void gvDocAssoc_RowDataBound(object sender, GridViewRowEventArgs e) 
     { 



      if (e.Row.RowType == DataControlRowType.DataRow) 
      { 
       if ((Convert.ToString(DataBinder.Eval(e.Row.DataItem, "DETAIL_TYPE_DESC")) == "Transcript") && 
        (Convert.ToString(DataBinder.Eval(e.Row.DataItem, "INSTITUTION_CODE")) == "")) 
       { 
        e.Row.BackColor = System.Drawing.Color.Red; 
       } 
       if ((Convert.ToString(DataBinder.Eval(e.Row.DataItem, "DETAIL_TYPE_DESC")) == "Certified Diploma") && 
        (Convert.ToString(DataBinder.Eval(e.Row.DataItem, "INSTITUTION_CODE")) == "")) 
       { 
        e.Row.BackColor = System.Drawing.Color.Red; 
       } 

       if ((Convert.ToString(DataBinder.Eval(e.Row.DataItem, "DOC_TYPE_DESC")) == "Post Graduate conditions") && 
        (Convert.ToString(DataBinder.Eval(e.Row.DataItem, "INSTITUTION_CODE")) == "")) 

       { 
        e.Row.BackColor = System.Drawing.Color.Red; 
       } 

      } 


     } 
+0

什麼問題將e作爲參數傳遞給第四功能? – 2010-03-31 16:15:55

+0

到一個功能 – vittore 2010-03-31 16:30:09

回答

7

您可以將同一個處理程序分配給多個正在引發事件的對象。例如,

myGridView1.RowDataBound += gvDocAssoc_RowDataBound; 
myGridView2.RowDataBound += gvDocAssoc_RowDataBound; 
myGridView3.RowDataBound += gvDocAssoc_RowDataBound; 

或者,只是創建具有相同簽名的事件處理程序的方法,並通過在相同的參數:

private void RepeatedCodeHandler(object sender, GridRowEventArgs e) 
{ 
    // Repeated code 
} 

,並從你的三個處理程序的重複代碼,

protected void gvDocAssoc_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    // Call method 
    RepeatedCodeHandler(sender, e); 
} 
0

你可以把這整個代碼放在一個簡單的方法。您可以完全複製該方法,更改其名稱,並從事件處理程序向其添加調用。

但更好的方法是將此方法分配爲所有三個事件的事件處理程序。

0

選項1:附上相同的事件處理程序的多個事件

gvDocAssoc.OnRowDataBound += gvDocAssoc_RowDataBound; 
gvDocAssoc.<SecondEvent> += gvDocAssoc_RowDataBound; 

選項2:把代碼到一個方法&調用在適當的時間的方法中的事件處理程序

protected void gvDocAssoc_RowDataBound(object sender, GridViewRowEventArgs e){ 
    set_row_styling(e); 
} 

private void set_row_styling(GridViewRowEventArgs e){ 
if (e.Row.RowType == DataControlRowType.DataRow) 
      { 
       if ((Convert.ToString(DataBinder.Eval(e.Row.DataItem, "DETAIL_TYPE_DESC")) == "Transcript") && 
        (Convert.ToString(DataBinder.Eval(e.Row.DataItem, "INSTITUTION_CODE")) == "")) 
       { 
        e.Row.BackColor = System.Drawing.Color.Red; 
       } 
       if ((Convert.ToString(DataBinder.Eval(e.Row.DataItem, "DETAIL_TYPE_DESC")) == "Certified Diploma") && 
        (Convert.ToString(DataBinder.Eval(e.Row.DataItem, "INSTITUTION_CODE")) == "")) 
       { 
        e.Row.BackColor = System.Drawing.Color.Red; 
       } 

       if ((Convert.ToString(DataBinder.Eval(e.Row.DataItem, "DOC_TYPE_DESC")) == "Post Graduate conditions") && 
        (Convert.ToString(DataBinder.Eval(e.Row.DataItem, "INSTITUTION_CODE")) == "")) 

       { 
        e.Row.BackColor = System.Drawing.Color.Red; 
       } 

      } 
} 
0

我會這樣做

bool RowHasError(GridViewRow row, string itemName, string itemValue) 
    { 
     return (Convert.ToString(DataBinder.Eval(row.DataItem, itemName)) == itemValue) && 
        (Convert.ToString(DataBinder.Eval(row.DataItem, "INSTITUTION_CODE")) == ""); 
    } 

    protected void gvDocAssoc_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      if (RowHasError(e.Row, "DETAIL_TYPE_DESC", "Transcript") 
       || (RowHasError(e.Row, "DETAIL_TYPE_DESC", "Certified Diploma") 
       || (RowHasError(e.Row, "DOC_TYPE_DESC", "Post Graduate conditions")) 
      { 
       e.Row.BackColor = System.Drawing.Color.Red; 
      }     
     } 
    }