简体   繁体   English

使用动态创建的文本框更新gridview

[英]gridview update with dynamically created textboxes

I have a gridview that is bound to a sql data source. 我有绑定到sql数据源的gridview。

In the RowBound method I have this code in order to format the editable textboxes: 在RowBound方法中,我具有以下代码以便格式化可编辑的文本框:

protected void gridview_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    //Format the Edit row.
        if (e.Row.RowIndex == gridview.EditIndex && e.Row.RowIndex >= 0)
        {
           //Add Ajax Calendar to Date fields
            TextBox startDate= ((TextBox)e.Row.Cells[7].Controls[0]);
            startDate.ID = "txtStartDate";
            TextBox endDate= ((TextBox)e.Row.Cells[8].Controls[0]);
            endDate.ID = "txtEndDate";
            startDate.Attributes.Add("onchange", "javascript:GridviewAutoCalculateEndDate(this, " + endDate.ClientID + ");");

            AjaxControlToolkit.CalendarExtender startDateCalendar = new AjaxControlToolkit.CalendarExtender();
            startDateCalendar .ID = "startDateCalendar ";
            startDateCalendar .TargetControlID = "txtStartDate";
            startDateCalendar .Format = "dd/MM/yyyy";
            AjaxControlToolkit.CalendarExtender endDateCalendar = new AjaxControlToolkit.CalendarExtender();
            endDateCalendar.ID = "endDateCalendar";
            endDateCalendar .TargetControlID = "txtEndDate";
            endDateCalendar .Format = "dd/MM/yyyy";

            e.Row.Cells[7].Controls.Add(startDate);
            e.Row.Cells[8].Controls.Add(endDate);
            e.Row.Cells[7].Controls.Add(startDateCalendar );
            e.Row.Cells[8].Controls.Add(endDateCalendar );
        }
}

The above just adds a dynamic AjaxCalendarExtender to two textboxes and the javascript, takes the date entered in the startDate textbox, adds one year to it and updates it in the endDate textbox (so you do not have to change it manually). 上面只是将动态AjaxCalendarExtender添加到两个文本框和javascript中,采用在startDate文本框中输入的日期,对其添加了一年并在endDate文本框中进行了更新(因此您不必手动更改)。

The problem I am having is that when I click the Update command field, I get a null reference to those two textboxes and the text inside them also clears (something to do with postback?) 我遇到的问题是,当我单击“更新”命令字段时,我获得了对这两个文本框的空引用,并且其中的文本也清除了(与回发有关吗?)

Here is the update code: 这是更新代码:

protected void gridview_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    DateTime startDate= new DateTime();
    DateTime.TryParseExact(e.NewValues[6].ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out startDate);
    DateTime endDate= new DateTime();
    DateTime.TryParseExact(e.NewValues[7].ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out endDate);

    dataContext.UpdateDates(startDate, endDate);
    PerformDataBind();
}

Basically startDate and endDate seem to clear and become null right after clicking Update. 基本上,startDate和endDate似乎在单击Update之后立即清除并变为null。 The e.NewValues don't seem to pull the date fields. e.NewValues似乎没有拉日期字段。 If I am to remove the dynamic data they update just fine. 如果我要删除动态数据,它们会更新就好了。

What am I missing? 我想念什么?

变量名称“ startDate”和“ endDate”仅存在于if语句中,如果您想使用该名称来对它们进行定义,请在更高的范围内进行定义

What's happening: 发生了什么:

Dynamically created controls must be created each and every time the Page loads. 每次加载页面时,都必须创建动态创建的控件。 When you press the Update button, a PostBack occurs, and in that PostBack you'd have to recreate the dynamic controls to be able to access them. 当您按下“更新”按钮时,将发生“回发”,并且在该“回发”中,您必须重新创建动态控件才能访问它们。

Meaning, if RowUpdating is happening, and you didn't go through RowDataBound before, the controls are not there. 意思是,如果正在发生RowUpdating ,并且您之前没有经过RowDataBound ,则控件不存在。

I also presume that during an Updating PostBack, gridview.EditIndex will have a different value, so your controls won't be there in any case. 我还假定在Updating PostBack期间, gridview.EditIndex将具有不同的值,因此您的控件在任何情况下都不会存在。

What you should be doing instead: 您应该怎么做:

You should design your edit mode template in your markup, and let ASP.NET handle control creation. 您应该在标记中设计编辑模式模板,并让ASP.NET处理控件的创建。 Take advantage of the data binding capabilities, let them do the work for you. 利用数据绑定功能,让他们为您完成工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM