ViewState Data Source on Page

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" AutoGenerateColumns="False" runat="server" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" ShowFooter="True">
            <Columns>
                <asp:BoundField DataField="EntityID" HeaderText="ID" />
                <asp:BoundField DataField="Description" HeaderText="Description" />
                <asp:CommandField ShowEditButton="True" ShowDeleteButton="True" />
            </Columns>
            <EmptyDataTemplate>
                You need add a data.
            </EmptyDataTemplate>
        </asp:GridView>
   
    </div>
        <asp:Button ID="Button1" runat="server" CommandArgument="d" OnClick="Button1_Click" Text="New" />&nbsp;
    </form>
</body>
</html>
 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        List<MyEntity> _entitys = null;

        public List<MyEntity> MyDataSource
        {
            get
            {
                return _entitys;
            }
            set
            {
                _entitys = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            MyDataSource = (List<MyEntity>)ViewState["Entitys"];
            if (MyDataSource == null)
            {
                MyDataSource = new List<MyEntity>();
            }
            ViewState["Entitys"] = MyDataSource;

            if (!IsPostBack)
            {
                bindGridView1();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            MyEntity ent = new MyEntity();
            MyDataSource.Add(ent);

            GridView1.EditIndex = MyDataSource.Count - 1;
            bindGridView1();
        }

        protected void bindGridView1()
        {
            GridView1.DataSource = MyDataSource;
            GridView1.DataBind();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
           
            bindGridView1();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            MyDataSource[e.RowIndex].EntityID = int.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text);
            MyDataSource[e.RowIndex].Description = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text;

            GridView1.EditIndex = -1;
           
            bindGridView1();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            MyDataSource.RemoveAt(e.RowIndex);

            bindGridView1();
        }

    }
}

namespace WebApplication1
{
    [Serializable]
    public class MyEntity
    {
        private int _entityID = 0;
        private string _description = string.Empty;

        public MyEntity()
        {
        }

        public int EntityID
        {
            get
            {
                return _entityID;
            }
            set
            {
                _entityID = value;
            }
        }

        public string Description
        {
            get
            {
                return _description;
            }
            set
            {
                _description = value;
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET ViewState 是一种用于在 Web 应用程序中跨请求存储数据的机制。以下是一个使用 ViewState 的示例: 假设您有一个页面,其中包含一个文本框和一个按钮。用户在文本框中输入一些文本,然后单击按钮。在单击按钮时,将在服务器端处理程序中使用 ViewState 存储文本框中的值,并在页面上显示它。 以下是一个简单的 ASP.NET 页面代码示例,它演示了如何使用 ViewState 存储和检索文本框中的值: ```html <%@ Page Language="C#" %> <!DOCTYPE html> <html> <head runat="server"> <title>ViewState Example</title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <br /> <asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click" /> <br /> <asp:Label ID="Label1" runat="server"></asp:Label> </div> </form> </body> </html> ``` 在按钮单击事件处理程序中,我们将文本框中的值存储在 ViewState 中,并将其显示在页面上: ```csharp protected void Button1_Click(object sender, EventArgs e) { string text = TextBox1.Text; ViewState["myText"] = text; Label1.Text = "Text saved: " + text; } ``` 在页面加载事件处理程序中,我们检索存储在 ViewState 中的值,并将其显示在页面上: ```csharp protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (ViewState["myText"] != null) { string text = (string)ViewState["myText"]; Label1.Text = "Text retrieved: " + text; } } } ``` 通过这种方式,我们可以在页面上保留用户在文本框中输入的值,即使用户单击其他按钮或导航到其他页面。请注意,ViewState 可能会增加页面大小,并增加网络传输时间。因此,我们应该谨慎使用 ViewState,并仅在必要时使用它。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值