在GridView的细胞失去对RowUpdating事件控制 [英] Cells in gridview lose controls on RowUpdating event

查看:164
本文介绍了在GridView的细胞失去对RowUpdating事件控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的C#组合与ASP.NET,所以我会尽可能地做到清楚,我可以用我的解释。

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.

我使用一个GridView,以显示驻留在一个数据库中的一些列和行。这种做工精良。因为我想动态添加列,具体名称的列表。比方说,我们有5名中有一个列表,然后将其动态创建的GridView中的每一个名称的列。 下面是一些code,以显示我做什么:

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:

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

还有一些其他的东西在里面,是不是很重要。该ShowDataInGrid看起来如下:

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

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

我如何创建动态列的每一行? 我创建它们与数据绑定事件。因此,当在GridView databinds,列得到填充数据。还有一个检查内部,以查看是否该行处于编辑模式或没有。所以,当我点击旁边给该行的编辑按钮,该行顺利完美的编辑模式。在codeI做增加了一个TextBox控件的单元格。 当我填写一张价值preSS更新按钮,它无法再找到任何控制,因此,我不能让我输入的值。控制仍然存在,在数据绑定,当我把一个破发点之后。但是当我点击更新按钮,细胞的所有控件都消失了。

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.

我在互联网上搜索和所有我发现的是,它是与应用射击回发之前到达程序的RowUpdating方法。

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.

那么,如何才能让这些控件存在,我加入到细胞,当它在编辑模式?

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

如果有人需要的code这就是数据绑定事件中我会发布它。

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

推荐答案

动态控件或列需要重新创建的,因为它控制的工作方式上的每一个页面加载。动态控件没有得到保留,因此必须重新加载它们在每一页上回发;然而,视图状态将保留这些控件。

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.

HTH。

这篇关于在GridView的细胞失去对RowUpdating事件控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆