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

67 views Asked by At

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 = "Tax NUMBER";
        tempPlaceHolder.ID = "PhPO" + wo;
        tempLabel.ID = "LblPO" + wo;

        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);


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


        tempCell1.Controls.Add(tempLabel1);

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

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

        tempCell2.Controls.Add(tempPlaceHolder2);
        tempCell2.Controls.Add(new LiteralControl("<br/>"));
        tempCell2.Controls.Add(tempLabel2);

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

        tempHeader3.Text = "START DATE";
        tempLabel3.ID = "LblStartDate" + 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

There are 1 answers

0
Albert D. Kallal On

Well, say we have this markup:

        <asp:GridView ID="GridView1" runat="server" 
            CssClass="table table-striped">
        </asp:GridView>

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("Tax Number");
        dt.Columns.Add("Print Time", typeof(DateTime));
        dt.Columns.Add("Part");
        dt.Columns.Add("Start Date",typeof(DateTime));

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

And the result is this:

enter image description here

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<MyPart> MyParts = new List<MyPart>();
        // create 10 rows
        for (int i = 1; i <= 10; i++)
        {
            MyPart OnePart = new MyPart();
            OnePart.TaxNum = "123 45" + i.ToString();
            OnePart.PrintTime = DateTime.Now;
            OnePart.Part = i.ToString().PadLeft(4, '0');
            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:

enter image description here

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.