DataGrid中的TemplateField控件返回Null

时间:2015-04-21 09:53:52

标签: c# asp.net datagridview datagrid webforms

我已经以编程方式使用templatefields设置了DataGridView,如下所示。

class DayColumn : ITemplate
{
    public void InstantiateIn(System.Web.UI.Control container)
    {
        DropDownList ddlEntryType = new DropDownList();
        ddlEntryType.ID = "ddlEntryType";
        ddlEntryType.DataValueField = "EntryTypeID";
        ddlEntryType.DataTextField = "Title";
        var entryTypes = EntryType.GetAll();
        ddlEntryType.DataSource = entryTypes;
        ddlEntryType.DataBind();
        ddlEntryType.Width = 120;
        container.Controls.Add(ddlEntryType);

        TextBox txtHours = new TextBox();
        txtHours.ID = "txtHours";
        txtHours.Text = "0";
        txtHours.Width = 100;
        txtHours.ValidationGroup = "Validation";
        container.Controls.Add(txtHours);

然后我使用此模板字段设置整个数据网格。

        var storeID = Request.QueryString["storeID"];
        var employees = Employee.GetForTimesheet(storeID.ToInt());

        var boundField = new BoundField();
        boundField.DataField = "Firstname";
        boundField.HeaderText = "First Name";
        grdTimesheet.Columns.Add(boundField);

        boundField = new BoundField();
        boundField.DataField = "Lastname";
        boundField.HeaderText = "Second Name";
        grdTimesheet.Columns.Add(boundField);

        boundField = new BoundField();
        boundField.DataField = "PayrollNumber";
        boundField.HeaderText = "Payroll Number";
        grdTimesheet.Columns.Add(boundField);

        var templateField = new TemplateField();
        templateField.ItemTemplate = new DayColumn();
        templateField.HeaderText = "Monday";
        grdTimesheet.Columns.Add(templateField);

        templateField = new TemplateField();
        templateField.ItemTemplate = new DayColumn();
        templateField.HeaderText = "Tuesday";
        grdTimesheet.Columns.Add(templateField);

我的问题是,当我想从每个单元格的文本框和下拉列表中获取数据时,它总是返回null。

        protected void SaveTimesheet(object sender, EventArgs e)
    {
        for (int i = 0; i < grdTimesheet.Rows.Count; i++)
        {

            TimesheetDataGridEmployee tsEmployee = new TimesheetDataGridEmployee();
            tsEmployee.firstName = grdTimesheet.Rows[i].Cells[0].Text.ToString();
            tsEmployee.lastName = grdTimesheet.Rows[i].Cells[1].Text.ToString();
            tsEmployee.payrollNumber = grdTimesheet.Rows[i].Cells[2].Text.ToInt();

            TextBox txtMonday = (TextBox) grdTimesheet.Rows[i].Cells[3].FindControl("txtHours");
            tsEmployee.week.monday.hours = txtMonday.Text.ToDecimal();

            DropDownList ddlMonday = grdTimesheet.Rows[i].Cells[3].FindControl("ddlEntryType") as DropDownList;
            tsEmployee.week.monday.entryTypeID = ddlMonday.SelectedValue.ToInt();

        }
    }

txtMonday和ddlMonday总是返回null,尽管使用我的想法和其他人说的是从数据网格获取控件的正确方法。行和单元格目标是正确的,findControl参数也是如此。如果有人可以帮助我,我将深深感激,因为我似乎已经耗尽了所有其他资源以期找到解决方案,如果需要,我完全愿意改变这种结构。

以下是完整代码:

    public partial class EditTimesheet : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Permissionstring = "Timesheets_Edit";
        if (!IsPostBack)
        {

        }

        LoadData();
    }

    protected void LoadDays()
    {

        var templateField = new TemplateField();
        templateField.ItemTemplate = new DayColumn();
        templateField.HeaderText = "Monday";
        grdTimesheet.Columns.Add(templateField);

        templateField = new TemplateField();
        templateField.ItemTemplate = new DayColumn();
        templateField.HeaderText = "Tuesday";
        grdTimesheet.Columns.Add(templateField);

        templateField = new TemplateField();
        templateField.ItemTemplate = new DayColumn();
        templateField.HeaderText = "Wednesday";
        grdTimesheet.Columns.Add(templateField);

        templateField = new TemplateField();
        templateField.ItemTemplate = new DayColumn();
        templateField.HeaderText = "Thursday";
        grdTimesheet.Columns.Add(templateField);

        templateField = new TemplateField();
        templateField.ItemTemplate = new DayColumn();
        templateField.HeaderText = "Friday";
        grdTimesheet.Columns.Add(templateField);

        templateField = new TemplateField();
        templateField.ItemTemplate = new DayColumn();
        templateField.HeaderText = "Saturday";
        grdTimesheet.Columns.Add(templateField);
    }

    protected void LoadData()
    {
        grdTimesheet.Columns.Clear();

        var boundField = new BoundField();
        boundField.DataField = "Firstname";
        boundField.HeaderText = "First Name";
        grdTimesheet.Columns.Add(boundField);

        boundField = new BoundField();
        boundField.DataField = "Lastname";
        boundField.HeaderText = "Second Name";
        grdTimesheet.Columns.Add(boundField);

        boundField = new BoundField();
        boundField.DataField = "PayrollNumber";
        boundField.HeaderText = "Payroll Number";
        grdTimesheet.Columns.Add(boundField);

        LoadDays();
        SourceAndBind();
    }

    protected void SourceAndBind()
    {
        var storeID = Request.QueryString["storeID"];
        var employees = Employee.GetForTimesheet(storeID.ToInt());
        grdTimesheet.DataSource = employees;
        grdTimesheet.DataBind();
    }

    protected void SaveTimesheet(object sender, EventArgs e)
    {
        for (int i = 0; i < grdTimesheet.Rows.Count; i++)
        {
            TimesheetDataGridEmployee tsEmployee = new TimesheetDataGridEmployee();
            tsEmployee.firstName = grdTimesheet.Rows[i].Cells[0].Text.ToString();
            tsEmployee.lastName = grdTimesheet.Rows[i].Cells[1].Text.ToString();
            tsEmployee.payrollNumber = grdTimesheet.Rows[i].Cells[2].Text.ToInt();

            DropDownList ddlMonday = grdTimesheet.Rows[i].Cells[3].FindControl("ddlEntryType") as DropDownList;
            tsEmployee.week.monday.entryTypeID = ddlMonday.SelectedValue.ToInt();

            TextBox txtMonday = (TextBox) grdTimesheet.Rows[i].Cells[3].FindControl("txtHours");
            tsEmployee.week.monday.hours = txtMonday.Text.ToDecimal();



        }
    }
}

实例化:

        public void InstantiateIn(System.Web.UI.Control container)
    {
        DropDownList ddlEntryType = new DropDownList();
        ddlEntryType.ID = "ddlEntryType";
        ddlEntryType.DataValueField = "EntryTypeID";
        ddlEntryType.DataTextField = "Title";
        var entryTypes = EntryType.GetAll();
        ddlEntryType.DataSource = entryTypes;
        ddlEntryType.DataBind();
        ddlEntryType.Width = 120;
        container.Controls.Add(ddlEntryType);

        TextBox txtHours = new TextBox();
        txtHours.ID = "txtHours";
        txtHours.Text = "0";
        txtHours.Width = 100;
        txtHours.ValidationGroup = "Validation";
        container.Controls.Add(txtHours);

    }

的GridView:

              <asp:GridView ID="grdTimesheet" runat="server" class="table table-striped table-bordered table-hover" AutoGenerateColumns="False" Font-Size="10" 
          Font-Names="Arial" GridLines="Vertical" Width="40%" OnRowDataBound="grdTimesheet_RowDataBound">

          </asp:GridView>

1 个答案:

答案 0 :(得分:0)

    public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

        }

        BindGrid();
    }

    private void BindGrid()
    {
        gvTest.Columns.Clear();

        var boundField = new BoundField();
        boundField.DataField = "Firstname";
        boundField.HeaderText = "First Name";
        gvTest.Columns.Add(boundField);

        boundField = new BoundField();
        boundField.DataField = "Lastname";
        boundField.HeaderText = "Second Name";
        gvTest.Columns.Add(boundField);

        var templateField = new TemplateField();
        templateField.ItemTemplate = new DayColumn("txtHoursMonday");
        templateField.HeaderText = "Monday";
        gvTest.Columns.Add(templateField);

        templateField = new TemplateField();
        templateField.ItemTemplate = new DayColumn("txtHoursTuesday");
        templateField.HeaderText = "Tuesday";
        gvTest.Columns.Add(templateField);

        DataTable dt = new DataTable();
        dt.Columns.Add("Firstname");
        dt.Columns.Add("Lastname");

        DataRow dr = dt.NewRow();
        dr["Firstname"] = "Test";
        dr["Lastname"] = "Test";

        dt.Rows.Add(dr);

        gvTest.DataSource = dt;
        gvTest.DataBind();
    }

    class DayColumn : ITemplate
    {
        private string controlID;

        public DayColumn(string id)
        {
            controlID = id;
        }


        public void InstantiateIn(System.Web.UI.Control container)
        {
            TextBox txtHours = new TextBox();
            txtHours.ID = controlID;
            txtHours.Text = "0";
            txtHours.Width = 100;
            txtHours.ValidationGroup = "Validation";
            container.Controls.Add(txtHours);
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < gvTest.Rows.Count; i++)
        {
            TextBox txtMonday = (TextBox)gvTest.Rows[i].FindControl("txtHoursMonday");
            TextBox txtTuesday = (TextBox)gvTest.Rows[i].FindControl("txtHoursTuesday");
        }
    }
}