Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataGrid中的TemplateField控件返回Null_C#_Asp.net_Datagridview_Datagrid_Webforms - Fatal编程技术网

C# DataGrid中的TemplateField控件返回Null

C# DataGrid中的TemplateField控件返回Null,c#,asp.net,datagridview,datagrid,webforms,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"; dd

我以编程方式使用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);
然后,我使用这个templatefield设置整个datagrid

        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);
我的问题是,当我想从每个单元格的textbox和dropdownlist中获取数据时,它总是返回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();

        }
    }
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>

公共部分类默认值2:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!Page.IsPostBack)
{
}
BindGrid();
}
私有void BindGrid()
{
gvTest.Columns.Clear();
var boundField=新的boundField();
boundField.DataField=“Firstname”;
boundField.HeaderText=“名字”;
gvTest.Columns.Add(boundField);
boundField=新的boundField();
boundField.DataField=“Lastname”;
boundField.HeaderText=“第二个名称”;
gvTest.Columns.Add(boundField);
var templateField=new templateField();
templateField.ItemTemplate=新的DayColumn(“txtHoursMonday”);
templateField.HeaderText=“星期一”;
gvTest.Columns.Add(templateField);
templateField=新的templateField();
templateField.ItemTemplate=新的DayColumn(“txthours周二”);
templateField.HeaderText=“星期二”;
gvTest.Columns.Add(templateField);
DataTable dt=新的DataTable();
dt.列。添加(“名字”);
dt.列。添加(“姓氏”);
DataRow dr=dt.NewRow();
dr[“Firstname”]=“Test”;
dr[“Lastname”]=“Test”;
dt.Rows.Add(dr);
gvTest.DataSource=dt;
gvTest.DataBind();
}
类别DayColumn:ITemplate
{
私有字符串控制ID;
公共日列(字符串id)
{
controlID=id;
}
public void实例化EIN(System.Web.UI.Control容器)
{
TextBox txtHours=新建TextBox();
txtHours.ID=控制ID;
txtHours.Text=“0”;
txtHours.Width=100;
txtHours.ValidationGroup=“验证”;
container.Controls.Add(txtHours);
}
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
对于(int i=0;i
回发后TXTManday和DDLSanday控件是否可用?如果没有,则必须将Page_Init方法中的列添加到datagrid。我已将数据加载从Page_Load切换到Page_Init,但仍然返回null。您的意思是,控件在UI中回发后可用,但在事件处理程序中为null?回发后,控件从datagrid中的字段中清除,因此在事件处理程序中也返回null。顺便说一句,我很感谢您抽出时间帮我检查一下。您必须在每次发帖时再次绑定datagrid.gvTest.Columns.Clear();确实成功地清除了列,但是同样的错误弹出,非常奇怪。不确定你的代码有什么问题,因为它在我的测试项目中运行良好。粘贴有问题的整个页面的Ui和cs代码。检查更新的DayColumn类。您必须以某种方式传递唯一id。我为textbox做了这件事。您可以为dropdownlist添加另一个。非常感谢!那帮我解决了。非常感谢。
        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);

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