如何简化变量,而不是创建许多调用相同类的变量

huangapple go评论74阅读模式
英文:

How to simplified variable instead of creating many variables that call the same class

问题

我正在使用System.Web.UI.WebControls中的Table类从后端创建一个表格,因此我需要为每个单元格和行初始化新变量,如下所示。

我想简化变量,而不是创建许多调用相同类的变量。有没有办法在不多次调用相同类的情况下简化这段代码?


以下是简化后的代码:

// Create a reusable function to initialize table cells
void InitializeTableCells(TableRow row, TableHeaderCell header, TableCell cell, PlaceHolder placeHolder, Label label, string headerText, string labelID, string placeHolderID)
{
    header.Text = headerText;
    label.ID = labelID;
    placeHolder.ID = placeHolderID;

    cell.Controls.Add(placeHolder);
    cell.Controls.Add(new LiteralControl("<br/>"));
    cell.Controls.Add(label);

    row.Controls.Add(header);
    row.Controls.Add(cell);
    table.Rows.Add(row);
}

// Initialize the table rows and cells
TableRow tempRow = new TableRow();
TableHeaderCell tempHeader = new TableHeaderCell();
TableCell tempCell = new TableCell();
PlaceHolder tempPlaceHolder = new PlaceHolder();
Label tempLabel = new Label();

InitializeTableCells(tempRow, tempHeader, tempCell, tempPlaceHolder, tempLabel, "Tax NUMBER", "LblPO" + wo, "PhPO" + wo);

tempRow = new TableRow();
tempHeader = new TableHeaderCell();
tempCell = new TableCell();
tempPlaceHolder = new PlaceHolder();
tempLabel = new Label();

InitializeTableCells(tempRow, tempHeader, tempCell, tempPlaceHolder, tempLabel, "PRINT TIME", "LblPrintTime" + wo, "");

// Repeat the above pattern for other rows and cells

这种方式可以减少重复的代码,并且使代码更加清晰和易于维护。你可以重复调用InitializeTableCells函数来初始化不同的行和单元格。

英文:

I am creating a table from backend using Table class from System.Web.UI.WebControls in asp webforms
Therefore, I need to initialize new variables for each cell and row as shown below.

        TableRow tempRow = new TableRow();
        TableCell tempCell = new TableCell();
        TableHeaderCell tempHeader = new TableHeaderCell();
        PlaceHolder tempPlaceHolder = new PlaceHolder();
        Label tempLabel = new Label();

        TableRow tempRow1 = new TableRow();
        TableCell tempCell1 = new TableCell();
        TableHeaderCell tempHeader1 = new TableHeaderCell();
        PlaceHolder tempPlaceHolder1 = new PlaceHolder();
        Label tempLabel1 = new Label();

        TableRow tempRow2 = new TableRow();
        TableCell tempCell2 = new TableCell();
        TableHeaderCell tempHeader2 = new TableHeaderCell();
        PlaceHolder tempPlaceHolder2 = new PlaceHolder();
        Label tempLabel2 = new Label();

        TableRow tempRow3 = new TableRow();
        TableCell tempCell3 = new TableCell();
        TableHeaderCell tempHeader3 = new TableHeaderCell();
        PlaceHolder tempPlaceHolder3 = new PlaceHolder();
        Label tempLabel3 = new Label();

        TableRow tempRow4 = new TableRow();
        TableCell tempCell4 = new TableCell();
        TableHeaderCell tempHeader4 = new TableHeaderCell();
        PlaceHolder tempPlaceHolder4 = new PlaceHolder();
        Label tempLabel4 = new Label();

        TableRow tempRow5 = new TableRow();
        TableCell tempCell5 = new TableCell();
        TableHeaderCell tempHeader5 = new TableHeaderCell();
        PlaceHolder tempPlaceHolder5 = new PlaceHolder();
        Label tempLabel5 = new Label();

        TableRow tempRow6 = new TableRow();
        TableCell tempCell6 = new TableCell();
        TableHeaderCell tempHeader6 = new TableHeaderCell();
        PlaceHolder tempPlaceHolder6 = new PlaceHolder();
        Label tempLabel6 = new Label();

        TableRow tempRow7 = new TableRow();
        TableCell tempCell7 = new TableCell();
        TableHeaderCell tempHeader7 = new TableHeaderCell();
        PlaceHolder tempPlaceHolder7 = new PlaceHolder();
        Label tempLabel7 = new Label();

        TableRow tempRow8 = new TableRow();
        TableCell tempCell8 = new TableCell();
        TableHeaderCell tempHeader8 = new TableHeaderCell();
        PlaceHolder tempPlaceHolder8 = new PlaceHolder();
        Label tempLabel8 = new Label();

        TableRow tempRow9 = new TableRow();
        TableCell tempCell9 = new TableCell();
        TableHeaderCell tempHeader9 = new TableHeaderCell();
        PlaceHolder tempPlaceHolder9 = new PlaceHolder();
        Label tempLabel9 = new Label();

        tempLabel.Font.Bold = font.Bold;
        tempLabel1.Font.Bold = font.Bold;
        tempLabel2.Font.Bold = font.Bold;
        tempLabel3.Font.Bold = font.Bold;
        tempLabel4.Font.Bold = font.Bold;
        tempLabel5.Font.Bold = font.Bold;
        tempLabel6.Font.Bold = font.Bold;
        tempLabel7.Font.Bold = font.Bold;
        tempLabel8.Font.Bold = font.Bold;
        tempLabel9.Font.Bold = font.Bold;

       
        // first row
        tempHeader.Text = &quot;Tax NUMBER&quot;;
        tempPlaceHolder.ID = &quot;PhPO&quot; + wo;
        tempLabel.ID = &quot;LblPO&quot; + wo;

        tempCell.Controls.Add(tempPlaceHolder);
        tempCell.Controls.Add(new LiteralControl(&quot;&lt;br/&gt;&quot;));
        tempCell.Controls.Add(tempLabel);

        tempRow.Controls.Add(tempHeader);
        tempRow.Controls.Add(tempCell);
        table.Rows.Add(tempRow);


        tempHeader1.Text = &quot;PRINT TIME&quot;;
        tempLabel1.ID = &quot;LblPrintTime&quot; + wo;


        tempCell1.Controls.Add(tempLabel1);

        tempRow.Controls.Add(tempHeader1);
        tempRow.Controls.Add(tempCell1);
        table.Rows.Add(tempRow);

        //2nd row
        tempHeader2.Text = &quot;Part&quot;;
        tempLabel2.ID = &quot;LblPart&quot; + wo;
        tempPlaceHolder2.ID = &quot;PhPart&quot; + wo;

        tempCell2.Controls.Add(tempPlaceHolder2);
        tempCell2.Controls.Add(new LiteralControl(&quot;&lt;br/&gt;&quot;));
        tempCell2.Controls.Add(tempLabel2);

        tempRow2.Controls.Add(tempHeader2);
        tempRow2.Controls.Add(tempCell2);
        table.Rows.Add(tempRow2);

        tempHeader3.Text = &quot;START DATE&quot;;
        tempLabel3.ID = &quot;LblStartDate&quot; + wo;

        tempCell3.Controls.Add(tempLabel3);

        tempRow2.Controls.Add(tempHeader3);
        tempRow2.Controls.Add(tempCell3);
        table.Rows.Add(tempRow2);

        //3rd row

I want to simplified the variable instead of creating many variables that call the same class. Is there any way that I can simplified this code without call the same class many times ?

Is there any way that I can simplified this code without call the same class many times ?

答案1

得分: 1

以下是您提供的内容的中文翻译:

好吧,假设我们有这样的标记:

<asp:GridView ID="GridView1" runat="server" CssClass="table table-striped">
</asp:GridView>
而后台代码可以是这样的:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        LoadData();
}

void LoadData()
{
    DataTable dt = new DataTable();
    dt.Columns.Add("税号");
    dt.Columns.Add("打印时间", typeof(DateTime));
    dt.Columns.Add("部件");
    dt.Columns.Add("开始日期", typeof(DateTime));

    // 创建10行
    for (int i = 1; i <= 10; i++)
    {
        DataRow NewRow = dt.NewRow();
        NewRow["税号"] = "123 45" + i.ToString();
        NewRow["打印时间"] = DateTime.Now;
        NewRow["部件"] = i.ToString().PadLeft(4, '0');
        NewRow["开始日期"] = DateTime.Now.AddDays(i);
        dt.Rows.Add(NewRow);
    }
    // 现在显示结果
    GridView1.DataSource = dt;
    GridView1.DataBind();
}
而结果如下:

[![点击这里输入图像描述][1]][1]

所以,为什么要写这么多的代码不是很清楚。

如果你想在代码中创建一个表格,那么使用数据表是一个更好的选择。

如果你想要“呈现”该表格,那么使用GridView、ListView或其他几种选择是一个更好的方法。

将数据表的概念与如何呈现和显示该表格的方式完全分开。

而且你不一定要使用数据表。你可以创建一个类,然后这样做:

public class MyPart
{
    public string 税号 { get; set; }
    public DateTime 打印时间 { get; set; }
    public string 部件 { get; set; }
    public DateTime 开始日期 { get; set; }
}

protected void Button1_Click(object sender, EventArgs e)
{
    List<MyPart> MyParts = new List<MyPart>();
    // 创建10行
    for (int i = 1; i <= 10; i++)
    {
        MyPart OnePart = new MyPart();
        OnePart.税号 = "123 45" + i.ToString();
        OnePart.打印时间 = DateTime.Now;
        OnePart.部件 = i.ToString().PadLeft(4, '0');
        OnePart.开始日期 = DateTime.Now.AddDays(i);
        MyParts.Add(OnePart);
    }

    GridView1.DataSource = MyParts;
    GridView1.DataBind();
}
最终结果基本相同,唯一的区别在于列标题。

所以,不管我们如何处理这个问题,

再次强调,将数据概念与数据表的呈现方式完全分开,或者如第二个示例所示,使用对象的类列表,然后将其发送到GridView以在网页上显示。
英文:

Well, say we have this markup:

        &lt;asp:GridView ID=&quot;GridView1&quot; runat=&quot;server&quot; 
            CssClass=&quot;table table-striped&quot;&gt;
        &lt;/asp:GridView&gt;

And code behind can be this:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadData();
    }

    void LoadData()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add(&quot;Tax Number&quot;);
        dt.Columns.Add(&quot;Print Time&quot;, typeof(DateTime));
        dt.Columns.Add(&quot;Part&quot;);
        dt.Columns.Add(&quot;Start Date&quot;,typeof(DateTime));

        // create 10 rows
        for (int i = 1;i &lt;= 10;i++)
        {
            DataRow NewRow = dt.NewRow();
            NewRow[&quot;Tax Number&quot;] = &quot;123 45&quot; + i.ToString();
            NewRow[&quot;Print Time&quot;] = DateTime.Now;
            NewRow[&quot;Part&quot;] = i.ToString().PadLeft(4,&#39;0&#39;);
            NewRow[&quot;Start Date&quot;] = DateTime.Now.AddDays(i);                
            dt.Rows.Add(NewRow);
        }
        // now display results
        GridView1.DataSource = dt;
        GridView1.DataBind();   
    }

And the result is this:

如何简化变量,而不是创建许多调用相同类的变量

So, it not all that clear why you writing such a big boatload of code?.

If you want to create a table in code, then use a data table.

If you want to "render" that table, then using a GridView, listview, or one of several other choices is a far better approach.

Keep the concept of a data table 100% separate from THEN how to render and display that table.

And you don't necessary have to use a datatable. You can say create a class, and do it this way:

    public class MyPart
    {
        public string TaxNum { get; set; }
        public DateTime PrintTime { get; set; }
        public string Part { get; set; }
        public DateTime StartDate { get; set; }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        List&lt;MyPart&gt; MyParts = new List&lt;MyPart&gt;();
        // create 10 rows
        for (int i = 1; i &lt;= 10; i++)
        {
            MyPart OnePart = new MyPart();
            OnePart.TaxNum = &quot;123 45&quot; + i.ToString();
            OnePart.PrintTime = DateTime.Now;
            OnePart.Part = i.ToString().PadLeft(4, &#39;0&#39;);
            OnePart.StartDate = DateTime.Now.AddDays(i);
            MyParts.Add(OnePart);
        }

        GridView1.DataSource=MyParts;
        GridView1.DataBind();  
    }

The end result is much the same, only real difference is the column headings.

so, we now get/see this:

如何简化变量,而不是创建许多调用相同类的变量

And I suppose, we could extend the class to have column headings, but regardless as to how we approach this?

Once again, the concept of keeping the data concept 100% separate from that of THEN rendering the data table, or as second example shows, using a class list of objects, and then sending to say GridView to display in the web page.

huangapple
  • 本文由 发表于 2023年6月13日 16:58:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76463261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定