1

asp.net C# テーブルを使用してタイムシートとして使用するスプレッドシートのようなテーブルを作成しようとしています。今、テーブル numberOFDayInMonth X 4 cols を作成できます。ヘッダー名とテキスト ボックス コントロールをテーブルに動的に追加する方法について助けが必要ですか?? 誰かが私を助けることができますか??

私のC#コード

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

namespace Compudata_ProjectManager
{
    public partial class testPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DateTimeFormatInfo info = DateTimeFormatInfo.GetInstance(null);

                for (int i = 1; i < 13; i++)
                {

                    //Response.Write(info.GetAbbreviatedMonthName(i) + "<br />");

                    ddl_months.Items.Add(new ListItem(info.GetMonthName(i), i.ToString()));

                }
            }
        }

        protected void ddl_months_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Remove all the current rows and cells.
            // This is not necessary if EnableViewState is set to false.
            tbl_timesheet.Controls.Clear();
            //declare datetime dt
            DateTime dt =  new DateTime();
            int orderOfMonth = Convert.ToInt32(ddl_months.SelectedValue.ToString());
            //get number of date in X month
            int noOfDays = DateTime.DaysInMonth(dt.Year, orderOfMonth);
            int numOfCols = 4;
            for (int row = 0; row < noOfDays; row++)
            {
                // Create a new TableRow object.
                TableRow rowNew = new TableRow();
                // Put the TableRow in the Table.
                tbl_timesheet.Controls.Add(rowNew);
                for (int col = 0; col < numOfCols; col++)
                {
                    // Create a new TableCell object.
                    TableCell cellNew = new TableCell();
                    cellNew.Text = "Example Cell (" + row.ToString() + ",";
                    cellNew.Text += col.ToString() + ")";

                    // Put the TableCell in the TableRow.
                    rowNew.Controls.Add(cellNew);
                }
            }

        }


    }
}
4

1 に答える 1

2

headerrow および Dynamic TextBox コントロール用

Table tbl = new Table(); // Creating a new table

TableHeaderRow header = new TableHeaderRow(); // Creating a header row
tbl.Rows.Add(header); // Add the header row to table tbl        

    // Creating Dynamic TextBox Control
TextBox t = new TextBox();
t.ID = "myTextBox"; // assing an ID

    // Now add this in a table row
TableRow rowNew = new TableRow(); // Creating a new table row
rowNew.Cells[0].Controls.Add(t);         // add the textbox control at cell zero
             // or you can add it as
rowNew.Controls.Add(t);
于 2012-08-30T16:49:50.530 に答える