此事件中的代码完全在另外两个事件处理程序中重复。如何将重复的代码放入方法并从事件处理程序调用该方法,因此我只需将其保存在一个位置?我不确定如何将事件args传递给调用方法。
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 :(得分: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);
}
答案 1 :(得分:0)
您可以将这整个代码放在一个简单的方法中。您可以完全复制方法,更改其名称,并从事件处理程序添加对它的调用。
但更好的方法是将此方法指定为所有三个事件的事件处理程序。
答案 2 :(得分:0)
选项1:将相同的事件处理程序附加到多个事件
gvDocAssoc.OnRowDataBound += gvDocAssoc_RowDataBound;
gvDocAssoc.<SecondEvent> += gvDocAssoc_RowDataBound;
选项2:将代码放入方法&amp;在事件处理程序中的适当时间调用该方法
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;
}
}
}
答案 3 :(得分: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;
}
}
}