首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >返回空的TemplateField控件

返回空的TemplateField控件
EN

Stack Overflow用户
提问于 2015-04-21 09:53:51
回答 1查看 94关注 0票数 0

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

代码语言:javascript
复制
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);

然后,我使用这个templatefield设置了整个数据集。

代码语言:javascript
复制
        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。

代码语言:javascript
复制
        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参数一样是正确的。如果有人能帮我,我会非常感激,因为我似乎已经用尽了所有其他资源,希望找到解决办法,我完全愿意改变这个结构,如果需要的话。

以下是完整的代码:

代码语言:javascript
复制
    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();



        }
    }
}

以及实例化:

代码语言:javascript
复制
        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:

代码语言:javascript
复制
              <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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-21 12:33:05

代码语言:javascript
复制
    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");
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29768764

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档