简体   繁体   中英

gridview update with dynamically created textboxes

I have a gridview that is bound to a sql data source.

In the RowBound method I have this code in order to format the editable textboxes:

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).

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. The e.NewValues don't seem to pull the date fields. 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.

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.

What you should be doing instead:

You should design your edit mode template in your markup, and let ASP.NET handle control creation. Take advantage of the data binding capabilities, let them do the work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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