ASP WebForms中如何简化重复创建相同类实例的代码?
简化ASP WebForms后端创建Table的重复代码方案
下面几种实用方法可以帮你摆脱重复创建控件变量的繁琐:
1. 复用局部变量
不需要为每个控件实例都声明新的变量,只要变量已经完成控件的添加工作,就可以重新赋值给新的实例。同时把Label的公共样式配置抽成小逻辑,避免重复设置:
// 只声明一次变量,重复复用 TableRow tempRow; TableCell tempCell; TableHeaderCell tempHeader; PlaceHolder tempPlaceHolder; Label tempLabel; // 抽取出Label的公共配置逻辑 Action<Label> setLabelStyle = label => label.Font.Bold = font.Bold; // 第一行:Tax NUMBER tempRow = new TableRow(); tempHeader = new TableHeaderCell { Text = "Tax NUMBER" }; tempPlaceHolder = new PlaceHolder { ID = "PhPO" + wo }; tempLabel = new Label { ID = "LblPO" + wo }; setLabelStyle(tempLabel); tempCell = new TableCell(); tempCell.Controls.Add(tempPlaceHolder); tempCell.Controls.Add(new LiteralControl("<br/>")); tempCell.Controls.Add(tempLabel); tempRow.Controls.Add(tempHeader); tempRow.Controls.Add(tempCell); table.Rows.Add(tempRow); // 第一行:PRINT TIME tempRow = new TableRow(); tempHeader = new TableHeaderCell { Text = "PRINT TIME" }; tempLabel = new Label { ID = "LblPrintTime" + wo }; setLabelStyle(tempLabel); tempCell = new TableCell(); tempCell.Controls.Add(tempLabel); tempRow.Controls.Add(tempHeader); tempRow.Controls.Add(tempCell); table.Rows.Add(tempRow); // 第二行:Part tempRow = new TableRow(); tempHeader = new TableHeaderCell { Text = "Part" }; tempPlaceHolder = new PlaceHolder { ID = "PhPart" + wo }; tempLabel = new Label { ID = "LblPart" + wo }; setLabelStyle(tempLabel); tempCell = new TableCell(); tempCell.Controls.Add(tempPlaceHolder); tempCell.Controls.Add(new LiteralControl("<br/>")); tempCell.Controls.Add(tempLabel); tempRow.Controls.Add(tempHeader); tempRow.Controls.Add(tempCell); table.Rows.Add(tempRow); // 第二行:START DATE tempRow = new TableRow(); tempHeader = new TableHeaderCell { Text = "START DATE" }; tempLabel = new Label { ID = "LblStartDate" + wo }; setLabelStyle(tempLabel); tempCell = new TableCell(); tempCell.Controls.Add(tempLabel); tempRow.Controls.Add(tempHeader); tempRow.Controls.Add(tempCell); table.Rows.Add(tempRow);
2. 封装辅助方法
把重复的控件创建、配置逻辑封装成独立方法,代码会更简洁,后期维护也方便:
// 生成带样式的Label private Label GetStyledLabel(string id, bool isBold) { return new Label { ID = id, Font = { Bold = isBold } }; } // 生成包含PlaceHolder+换行+Label的单元格 private TableCell GetCellWithPhAndLabel(string phId, string labelId, bool labelBold) { var cell = new TableCell(); var ph = new PlaceHolder { ID = phId }; var label = GetStyledLabel(labelId, labelBold); cell.Controls.Add(ph); cell.Controls.Add(new LiteralControl("<br/>")); cell.Controls.Add(label); return cell; } // 生成一行并添加到表格 private void AddTableHeaderRow(Table table, string headerText, TableCell cell) { var row = new TableRow(); var header = new TableHeaderCell { Text = headerText }; row.Controls.Add(header); row.Controls.Add(cell); table.Rows.Add(row); } // 调用方法快速构建表格 AddTableHeaderRow(table, "Tax NUMBER", GetCellWithPhAndLabel("PhPO" + wo, "LblPO" + wo, font.Bold)); AddTableHeaderRow(table, "PRINT TIME", new TableCell { Controls = { GetStyledLabel("LblPrintTime" + wo, font.Bold) } }); AddTableHeaderRow(table, "Part", GetCellWithPhAndLabel("PhPart" + wo, "LblPart" + wo, font.Bold)); AddTableHeaderRow(table, "START DATE", new TableCell { Controls = { GetStyledLabel("LblStartDate" + wo, font.Bold) } });
3. 批量处理结构化数据
如果有大量结构相似的行,可以把行的配置信息整理成集合,通过循环批量生成控件,减少重复代码:
// 定义两种行的配置数据 var rowsWithPh = new List<(string Header, string PhId, string LabelId)> { ("Tax NUMBER", "PhPO" + wo, "LblPO" + wo), ("Part", "PhPart" + wo, "LblPart" + wo) }; var simpleRows = new List<(string Header, string LabelId)> { ("PRINT TIME", "LblPrintTime" + wo), ("START DATE", "LblStartDate" + wo) }; // 批量生成带PlaceHolder的行 foreach (var item in rowsWithPh) { var row = new TableRow(); var header = new TableHeaderCell { Text = item.Header }; var cell = new TableCell(); var ph = new PlaceHolder { ID = item.PhId }; var label = new Label { ID = item.LabelId, Font = { Bold = font.Bold } }; cell.Controls.Add(ph); cell.Controls.Add(new LiteralControl("<br/>")); cell.Controls.Add(label); row.Controls.Add(header); row.Controls.Add(cell); table.Rows.Add(row); } // 批量生成仅带Label的行 foreach (var item in simpleRows) { var row = new TableRow(); var header = new TableHeaderCell { Text = item.Header }; var cell = new TableCell(); var label = new Label { ID = item.LabelId, Font = { Bold = font.Bold } }; cell.Controls.Add(label); row.Controls.Add(header); row.Controls.Add(cell); table.Rows.Add(row); }
内容的提问来源于stack exchange,提问作者muhammad hazieq
相关产品推荐
相关产品推荐

