gridview中的单元格失去对RowUpdating事件的控制

时间:2023-02-07 14:44:52

I'm quite new to the C# combo with ASP.NET, so i'll try to be as clear as i can with my explanation.

我对ASP.NET的C#组合很新,所以我会尽可能清楚地解释一下。

I'm using a GridView to display some columns and rows that reside in a database. This works excellent. Because i wanted to add columns dynamically out of a List of names. Let's say we have a list with 5 names in it, then it dynamically creates a column for every name in the GridView. Here's some code to display what i do:

我正在使用GridView来显示驻留在数据库中的一些列和行。这很好用。因为我想从名称列表中动态添加列。假设我们有一个包含5个名字的列表,然后它为GridView中的每个名称动态创建一个列。这是一些代码来显示我的行为:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //Create columns for each student
        List<Student> allStudents = new Eetlijst.Business.Students().GetAll();
        allStudents.Reverse();

        foreach (Student student in allStudents)
        {
            StudentBoundField studentField = new StudentBoundField();
            studentField.HeaderText = student.Naam;
            MaaltijdGrid.Columns.Insert(4, studentField);
        }

        ShowDataInGrid(DateTime.Now.Month, DateTime.Now.Year);

There's some other stuff in there that isn't important. The ShowDataInGrid looks as following:

那里还有其他一些不重要的东西。 ShowDataInGrid看起来如下:

public void ShowDataInGrid(int maand, int jaar)
{
    List<Maaltijd> allMaaltijden = new Eetlijst.Business.Maaltijden().GetByMonthAndYear(maand, jaar);
    Session.Contents.Add("maaltijden", allMaaltijden);

    MaaltijdGrid.DataSource = allMaaltijden;
    MaaltijdGrid.DataBind();
}

How do i create the dynamic columns for each row? I create them with a DataBound event. So when the GridView databinds, the columns get filled with data. There is also a check inside to see if the row is in edit mode or not. So when i click on the edit button next to the row, the row goes perfectly in edit mode. The code i made adds a TextBox control to the cell. When i fill in a value and press the Update button, it can't find any controls anymore and therefore i can't get the value i entered. The control is still there in the DataBound when i put a break point after it. But as soon as i click the Update button, all the controls of the cells are gone.

如何为每行创建动态列?我用DataBound事件创建它们。因此,当GridView数据绑定时,列将填充数据。内部还有一个检查,以查看该行是否处于编辑模式。因此,当我单击该行旁边的编辑按钮时,该行将完全处于编辑模式。我做的代码将一个TextBox控件添加到单元格。当我填写一个值并按下更新按钮时,它再也找不到任何控件,因此我无法获得我输入的值。当我在它之后设置一个断点时,DataBound中的控件仍然存在。但是当我单击“更新”按钮时,单元格的所有控件都消失了。

I searched on the internet and all i found is that it has something to do with the application firing a postback before it reaches the RowUpdating method.

我在互联网上搜索,我发现它与应用程序在到达RowUpdating方法之前触发回发有关。

So, how can i let the controls exist that i added to the cell when it is in edit mode?

那么,我如何让控件存在,当它处于编辑模式时我添加到单元格?

If anyone needs the code that's inside the DataBound event i'll post it.

如果有人需要DataBound事件中的代码,我将发布它。

2 个解决方案

#1


1  

Dynamic controls or columns need to be recreated on every page load, because of the way that controls work. Dynamic controls do not get retained so you have to reload them on every page postback; however, viewstate will be retained for these controls.

由于控件的工作方式,需要在每次页面加载时重新创建动态控件或列。动态控件不会被保留,因此您必须在每个页面回发时重新加载它们;但是,将保留viewstate用于这些控件。

HTH.

HTH。

#2


0  

Call this function ShowDataInGrid(DateTime.Now.Month, DateTime.Now.Year); in your

调用此函数ShowDataInGrid(DateTime.Now.Month,DateTime.Now.Year);在你的

Page_Init event.

Page_Init事件。

You will get the control and also the value.

您将获得控制权和价值。

#1


1  

Dynamic controls or columns need to be recreated on every page load, because of the way that controls work. Dynamic controls do not get retained so you have to reload them on every page postback; however, viewstate will be retained for these controls.

由于控件的工作方式,需要在每次页面加载时重新创建动态控件或列。动态控件不会被保留,因此您必须在每个页面回发时重新加载它们;但是,将保留viewstate用于这些控件。

HTH.

HTH。

#2


0  

Call this function ShowDataInGrid(DateTime.Now.Month, DateTime.Now.Year); in your

调用此函数ShowDataInGrid(DateTime.Now.Month,DateTime.Now.Year);在你的

Page_Init event.

Page_Init事件。

You will get the control and also the value.

您将获得控制权和价值。