如何在不使用数据库的情况下回发后在gridview中保存值 [英] How do I save value in gridview after postback without using database

查看:62
本文介绍了如何在不使用数据库的情况下回发后在gridview中保存值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前的任务是创建Timesheet系统。问题是在页面刷新后,GridView中的用户输入值将丢失。我一直在搜索其他论坛,没有解决方案可以解决我的问题。



我想解释一下我的系统流程。



1.用户将选择在GridView中添加新项目的下拉列表。



< asp:DropDownList ID =dd_project_norunat =serverCssClass =tb_pro_no btn-info

OnSelectedIndexChanged = dd_project_no_SelectedIndexChangedAutoPostBack =true>

< / asp:DropDownList>
2.然后用户将在GridView的文本框中放入一些值,这是工作时间该项目。



< asp:TemplateField ItemStyle-Width =600pxHeaderText =Saturday>

< HeaderTemplate>

< div class =form-inline>

< asp:Label ID =Label12runat =serverText =SatCssClass = col-xs-12>< / asp:Label>

<% - < asp:Label ID =dSatrunat =serverText =DateCssClass =col-xs-12>< / asp:标签> - %>

< ; asp:Label ID =Label16runat =serverText =STCssClass =col-xs-5>< / asp:Label>

< asp:Label ID =Label17runat =serverText =OTCssClass =col-xs-5>< / asp:Label>

< / div>

< / HeaderTemplate>

< ItemTemplate>

< div class =form-inline>

< ; asp:TextBox ID =sat_strunat =serverCssClass =tb_day col-xs-5>< / asp:TextBox>

< asp:TextBox ID =sat_ot runat =serverCssClass =tb_day col-xs-5ReadOnly =true>< / asp:TextBox>

< / div>

< / ItemTemplate>

< FooterTemplate&g t;

< div class =form-inline>

< asp:TextBox ID =tb_nt_sat_totrunat =serverCssClass =tb_day col -xs-5>< / asp:TextBox>

< asp:TextBox ID =tb_ot_sat_totrunat =serverCssClass =tb_day col-xs-5>< ; / asp:TextBox>

< / div>

< asp:Label ID =checkTotalSatrunat =serverForeColor =Red字体大小=7.5>< / asp:标签>

< / FooterTemplate>

< / asp:TemplateField>



3.如果用户想在选择下拉列表后添加新项目,则文本框内的值将丢失。

项目下拉列表背后的代码:



dt.Rows.Add(dr);

ViewState [Files] = dt;



GridView1.DataSource = dt;

GridView1.DataBind();





我的假设是由于用户键的值没有保存在DataSource中(这是ViewState [Files]) ,因此它被重置为null。



感谢建议。谢谢。



我尝试了什么:



我试过了使用UpdatePanel并添加绑定数据



protected void Page_Load(object sender,EventArgs e)

{

if(!Page.IsPostBack)

{

GridView1.DataBind();

}

}

My current task are to create Timesheet system. The problem is when user input value in GridView will be missing after the page been refresh. I have been search through other forum and there is no solution that can fit with my problem.

I would like to explain the flow of my system.

1. There is drop down list that user will select to add new project in the GridView.

<asp:DropDownList ID="dd_project_no" runat="server" CssClass="tb_pro_no btn-info"
OnSelectedIndexChanged="dd_project_no_SelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>
2. Then user will put some value inside textbox of GridView which is the working hour for the project.

<asp:TemplateField ItemStyle-Width="600px" HeaderText="Saturday">
<HeaderTemplate>
<div class="form-inline">
<asp:Label ID="Label12" runat="server" Text="Sat" CssClass="col-xs-12"></asp:Label>
<%--<asp:Label ID="dSat" runat="server" Text="Date" CssClass="col-xs-12"></asp:Label>--%>
<asp:Label ID="Label16" runat="server" Text="ST" CssClass="col-xs-5"></asp:Label>
<asp:Label ID="Label17" runat="server" Text="OT" CssClass="col-xs-5"></asp:Label>
</div>
</HeaderTemplate>
<ItemTemplate>
<div class="form-inline">
<asp:TextBox ID="sat_st" runat="server" CssClass="tb_day col-xs-5"></asp:TextBox>
<asp:TextBox ID="sat_ot" runat="server" CssClass="tb_day col-xs-5" ReadOnly="true"></asp:TextBox>
</div>
</ItemTemplate>
<FooterTemplate>
<div class="form-inline">
<asp:TextBox ID="tb_nt_sat_tot" runat="server" CssClass="tb_day col-xs-5"></asp:TextBox>
<asp:TextBox ID="tb_ot_sat_tot" runat="server" CssClass="tb_day col-xs-5"></asp:TextBox>
</div>
<asp:Label ID="checkTotalSat" runat="server" ForeColor="Red" Font-Size="7.5"></asp:Label>
</FooterTemplate>
</asp:TemplateField>

3. If user want to add new project after select the drop down list, the value inside the textbox will be missing.
The code behind for drop down list of project:

dt.Rows.Add(dr);
ViewState["Files"] = dt;

GridView1.DataSource = dt;
GridView1.DataBind();


My assumption is due to the value for user key in are not saved in DataSource (which is ViewState["Files"] ), hence it become reset to null.

Appreciate for advise. Thank You.

What I have tried:

I have tried using UpdatePanel and add bind data in

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridView1.DataBind();
}
}

推荐答案

从您的代码中我重新判断您正在使用Web表单(即您有一个页面对象)



此处可能使用viewstate

了解ASP.NET视图状态 [ ^ ]



那个beign说gridview不具备控件,所以很有可能发生的是你有一个gridview,它连接了一些编辑功能在这种情况下,你需要抓住这些数据,因为它们被发布了......那么什么?新建一行或保存并更新?我想你也需要这个教程:

教程12:在GridView中使用TemplateFields控制 [ ^ ]



如果您在asp.net web.page上发布内容,那么您可以访问Request.Form中的值[...] ]数组,取决于它是客户端还是服务器端控件,在页面加载后,您可以在控件的服务器端实例上访问它。



更新:好的,这是使用ViewState来保存数据并且不使用ObjectDatasource的类似问题的解决方案,这可能是你最终要做的事情,但坚持手头的情况并尽可能简化网格: D这个将在初始GET上创建一个新网格,并允许您使用它,只要您继续发布c舔网格上的动作链接按钮。



表格:

From your code i recon that you are working with web forms (i.e. you have a page object)

A possibility here is to use viewstate
Understanding ASP.NET View State[^]

That beign said a gridview doesn't as such have controls, so what very likely happens is that you have a gridview which is wired up with some edit functionality in that case you need to catch those data as they get posted and well ... what? make a new row or save and updated? I guess you need this tutorial too:
Tutorial 12: Using TemplateFields in the GridView Control[^]

So well the short of the long is that when you post something on a asp.net web.page you'll have access to the values in the Request.Form[] array and depending on if it is a client side or a server side control, after page load you could have access to it on the server side instance of a control.

UPDATE: OK, here's a solution to a similar problem using ViewState to persist the data and not using an ObjectDatasource which is propably what you want to do ultimately but sticking to the case at hand and keeping things as simple as possible with a grid :D This one will make a new grid on the initial GET and allow you to play with that as long as you keep posting by clicking the action linkbuttons on the grid.

The form:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AnittyGriddy.aspx.cs" Inherits="WebFormsProj.AnittyGriddy" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="Content/bootstrap.min.css" type="text/css" rel="stylesheet"  />
    <script src="Scripts/jquery-1.10.2.min.js" type="text/javascript"></script>

</head>
<body>
    <form id="form1" runat="server">
        <div class="row">
            <div class="col-xs-6"><label>Project scope</label></div>
            <div class="col-xs-6">
                <asp:DropDownList ID="ProjectDD" runat="server" Width="100%" OnSelectedIndexChanged="ProjectDD_SelectedIndexChanged" AutoPostBack="true">
                    <asp:ListItem Text="All" Value="all" Selected="True" />
                    <asp:ListItem Text="Add" Value="add" />
                </asp:DropDownList>
            </div>
        </div>
        <div class="row">
            <div class="col-xs-12">
                <asp:GridView ID="TheGrid" runat="server" Width="100%" Height="100%" AutoGenerateColumns="false" DataKeyNames="ProjectName" OnRowEditing="TheGrid_RowEditing" OnRowUpdating="TheGrid_RowUpdating" AutoGenerateEditButton="true" OnRowCancelingEdit="TheGrid_RowCancelingEdit">
                    <Columns>
                        <asp:TemplateField HeaderText="Project" SortExpression="ProjectName">
                            <EditItemTemplate>
                                <asp:TextBox ID="ProjectNameText" runat="server" Text='<%#Bind("ProjectName") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="ProjectNameLabel" runat="server" Text='<%#Bind("ProjectName") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Estimate" SortExpression="ProjectEstiamte">
                            <EditItemTemplate>
                                <asp:TextBox ID="ProjectEstimateText" runat="server" Text='<%#Bind("ProjectEstimate") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="ProjectEstimateLabel" runat="server" Text='<%#Bind("ProjectEstimate") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>
        </div>
    </form>
</body>
    
</html>





后面的代码:



And the code behind:

using System;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;

namespace WebFormsProj
{
    public partial class AnittyGriddy : System.Web.UI.Page
    {      
        protected DataTable GridSource {
            get { return (DataTable)ViewState["gridsource"]; }
            set { ViewState["gridsource"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ConstructInitialGridSource();
            }
            BindGrid();
        }

        private void ConstructInitialGridSource()
        {
            var table = new DataTable();
            table.Columns.Add(new DataColumn("ProjectName", typeof(string)));
            table.Columns.Add(new DataColumn("ProjectEstimate", typeof(int)));
            table.PrimaryKey = new DataColumn[] { table.Columns[0] };
            var row = table.NewRow();
            row[0] = "Alfa";
            row[1] = 100;
            table.Rows.Add(row);
            GridSource = table;
        }

        private void BindGrid()
        {
            TheGrid.DataSource = GridSource;
            TheGrid.DataBind();
        }

        protected void ProjectDD_SelectedIndexChanged(object sender, EventArgs e)
        {
            var dropdown = (DropDownList)sender;
            var todisplayordo = dropdown.SelectedValue;
            switch (todisplayordo)
            {
                case "add":
                    {
                        var mytable = GridSource;
                        var thenew = mytable.NewRow();
                        thenew[0] = DateTime.Now.Ticks.ToString();
                        thenew[1] = 666;
                        mytable.Rows.Add(thenew);
                        GridSource = mytable;                        
                        TheGrid.EditIndex = mytable.Rows.Count - 1;
                        BindGrid();
                        break;
                    }
                default:
                    {
                        //already databound to all
                        break;
                    }
            }
        }

        protected void TheGrid_RowEditing(object sender, GridViewEditEventArgs e)
        {
            TheGrid.EditIndex = e.NewEditIndex;
            TheGrid.DataBind();
        }

        protected void TheGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string keyField = e.Keys[0].ToString();
            var mytable = GridSource;
            var editRow = mytable.Rows.Find(e.Keys[0]);
            var keys = Request.Form.AllKeys;
            foreach (DataColumn clmn in mytable.Columns)
            {
                var idofclmn = keys.First(k => k.EndsWith(clmn.ColumnName + "Text"));
                editRow[clmn.ColumnName] = Request.Form[idofclmn];
            }
            GridSource = mytable;
            TheGrid.EditIndex = -1;
            BindGrid();
        }

        protected void TheGrid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            TheGrid.EditIndex = -1;
            TheGrid.DataBind();
        }
    }
}


这篇关于如何在不使用数据库的情况下回发后在gridview中保存值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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