【问题标题】:Hold temporary information in gridview before post-back在回发之前在gridview中保存临时信息
【发布时间】:2011-03-28 18:29:19
【问题描述】:

我有一个对话框(模式),我将在其中注册一个(或多个)联系人。

联系人转到一个网格视图,可以在其中对其进行编辑或删除。

Gridview中的数据,只能在流程结束时保存在数据库中。

我怎样才能做到这一点?

模式代码

$(function () {
    $(".ModalBox").dialog({
        autoOpen: false,
        height: 400,
        resizable: false,
        draggable: false,
        width: 602,
        modal: true,
        open: function (type, data) {
            $(this).parent().appendTo($("form:first"));
        }
    });
});

OBS.:

  • 我没有很好的 CSharp 或 html 代码示例,因为我不知道如何实现这一点。我所有的代码看起来都很乱(已经尝试了很多东西)

  • 我的GridView是一个ascx,modal在同一个ascx中。

  • 我相信一些临时表或类似的东西会有所帮助,但我从未做过类似的事情(看起来像购物车软件),我什至不知道如何寻找它。

谢谢。如果你能做一些代码示例,那就太好了。

编辑: 我做了这个代码:

CSharp 代码:

[Serializable]
        public class TabelaTempContato
        {
            public int IDCliente { get; set; }
            public string Nome { get; set; }
            public string Email { get; set; }
            public string Telefone { get; set; }
            public string Cpf { get; set; }
            public string Rg { get; set; }
            public string Departamento { get; set; }
            public string Cargo { get; set; }
        }

        protected List ListaTabelaTemp
        {
            get
            {
                if (this.ViewState["TabelaTemp"] == null)
                {
                    this.ViewState["TabelaTemp"] = new List();
                }

                return (List)this.ViewState["TabelaTemp"];
            }
        }

        protected void AddItem()
        {
            this.ListaTabelaTemp.Add(new TabelaTempContato());
            this.gvContato.DataSource = this.ListaTabelaTemp;
            this.gvContato.DataBind();
        }

        protected void btnTest_Click(object sender, EventArgs e)
        {
            this.AddItem();
        }

我创建了一个临时网格视图,但数据为空,我尝试从模式中的文本中提取它,但我无法做到,我不熟悉如何从中获取数据gridview 到我的数据库。 (我相信这是比较容易的部分,所以我暂时没有专注于它)

编辑:我用我的解决方案创建答案。

【问题讨论】:

  • 我会非常小心地将您的列表存储在 ViewState 中。它可能是一个真正的性能杀手——我自己多年前就犯了这个错误。如果您在页面范围内需要它们,请将值推送到隐藏的表单字段。否则,数据库不会花费您太多。
  • 感谢 Corey =) 我了解在 Viwestate 中保存数据的问题。但是作为现在的项目,现在很难改变它,所以我正在分页(通过数据库查询)少量并等到我的老板让我重新制作项目(项目开始时的预想是重制 o.O")。但这是一个伟大而真实的建议,再次感谢 =)

标签: c# .net asp.net temp-tables temporary


【解决方案1】:

        [Serializable]
        public struct TempContato
        {
            public int IDCliente { get; set; }
            public string Nome { get; set; }
            public string Email { get; set; }
            public string Telefone { get; set; }
            public string Cpf { get; set; }
            public string Rg { get; set; }
            public string Departamento { get; set; }
            public string Cargo { get; set; }
        }

        protected List ListaTabelaTemp
        {
            get
            {
                if (this.ViewState["ListaTempContato"] == null)
                    this.ViewState["ListaTempContato"] = new List();

                return (List)this.ViewState["ListaTempContato"];
            }
        }

        protected void AddItem()
        {
            TempContato tempContato = new TempContato();

            //tempContato.IDCliente = Convert.ToInt32(this.txtEmailContato.Text);
            tempContato.Nome = this.txtNomeContato.Text;
            tempContato.Email = this.txtEmailContato.Text;
            tempContato.Telefone = this.txtTelefoneContato.Text;
            tempContato.Cpf = this.txtCpfContato.Text;
            tempContato.Rg = this.txtRgContato.Text;
            tempContato.Departamento = this.ddlDepartamentoContato.SelectedValue;
            tempContato.Cargo = this.ddlCargoContato.SelectedValue;

            this.ListaTabelaTemp.Add(tempContato);
        }

        protected void AtualizarGrid()
        {
            this.gvContato.DataSource = this.ListaTabelaTemp;
            this.gvContato.DataBind();
        }

        protected void btnTest_Click(object sender, EventArgs e)
        {
            this.AddItem();
            this.AtualizarGrid();
        }

现在我从我的模态中获取值!现在只需要几样东西(我相信)。

1- 获取数据库中的数据以首次加载 GridView(如果是版本),并加载新的临时数据。

2- 保存新的临时数据。

完成:

1- 我在我的视图状态中加载并使用它来加载网格。

2- 也使用我的视图状态将我的数据保存在数据库中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 2015-04-26
    • 1970-01-01
    相关资源
    最近更新 更多