结合数据表到网格视图

编程入门 行业动态 更新时间:2024-10-24 10:26:13
本文介绍了结合数据表到网格视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下的code:

Imports System.Data Partial Class Students_AddWishes Inherits System.Web.UI.Page Public dt As New DataTable Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load dt.Columns.Add("ID", System.Type.GetType("System.Int32")) dt.Columns.Add("univirsity", System.Type.GetType("System.Int32")) dt.Columns.Add("major", System.Type.GetType("System.Int32")) End Sub Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click Dim row1 As DataRow = dt.NewRow() row1("ID") = dt.Rows.Count + 1 row1("univirsity") = ddlUnivs.SelectedValue row1("major") = ddlMajors.SelectedValue dt.Rows.Add(row1) GridView1.DataSource = dt GridView1.DataBind() End Sub End Class

问题是,它仅显示一个行或记录。如何使它显示多条记录?

The problem is it shows only one row or record. How to make it shows many records?

推荐答案

你不检查你的页面加载事件,如果它是一个回:

Your page load event you are not checking if it is a post back:

If Not IsPostBack Then 'process code if it is not a post back End If

每当你点击btnAdd按钮,你的页面做了回发到服务器。

Everytime you click the btnAdd button your page does a post back to the server.

我只注意到你可能不理解对象的续航时间。

I just noticed that you probably are not understanding the life time of an object.

您已经在code做到了这一点:

You had done this in your code:

Public dt As New DataTable

但问题是你必须定义这是一个类变量,一旦在页面加载你有DT型的实例,可能有与之相关的一些列。但只要你记录一个事件如按钮单击引用被销毁,创建一个新的DT。

The problem with that is you have defined this is a class variable and once the page has loaded you have an instance of type dt that may have some columns associated with it. But as soon as you record an event such as a button click that reference is destroyed and a new dt is created.

您将不得不作出一些使用session变量或数据库来存储DT的状态。

You will have to make some use of session variables or a database to store the state of dt.

下面是在C#示例:

protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { DataTable dt = new DataTable(); dt.Columns.Add("ID", System.Type.GetType("System.Int32")); dt.Columns.Add("univirsity", System.Type.GetType("System.Int32")); dt.Columns.Add("major", System.Type.GetType("System.Int32")); Session["MyDataTable"] = dt; } } protected void btnAdd_Click(object sender, EventArgs e) { DataTable t = (DataTable)Session["MyDataTable"]; DataRow row1 = t.NewRow(); row1["ID"] = t.Rows.Count + 1; row1["univirsity"] = 3; row1["major"] = 31; t.Rows.Add(row1); Session["MyDataTable"] = t; GridView1.DataSource = t; GridView1.DataBind(); }

和同一code在vb:

And the same code in vb:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) If Not Page.IsPostBack Then Dim dt As New DataTable() dt.Columns.Add("ID", System.Type.[GetType]("System.Int32")) dt.Columns.Add("univirsity", System.Type.[GetType]("System.Int32")) dt.Columns.Add("major", System.Type.[GetType]("System.Int32")) Session("MyDataTable") = dt End If End Sub Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Dim t As DataTable = DirectCast(Session("MyDataTable"), DataTable) Dim row1 As DataRow = t.NewRow() row1("ID") = t.Rows.Count + 1 row1("univirsity") = 3 row1("major") = 31 t.Rows.Add(row1) Session("MyDataTable") = t GridView1.DataSource = t GridView1.DataBind() End Sub

所以,现在什么code的作用是实例化一个新的DataTable对象,只要我们在页面上(第一回发),并添加列。一旦它定义数据表,我们把它扔在一些会话状态。当您单击Add按钮,你不能在你的previous code只是继续使用DT因为DT范围是在你之前的code丢失。我们通过分配这是之前一个临时存储数据表的数据表sessioned做到这一点。我们增加了一行,下一次我们增加一个排它会显示第二行,然后第三行,并重新设置会话方式等等...

So now what the code does is instantiate a new datatable object as long as we are on the page (first post back) and adds the columns. Once it has defined the data table we throw it in some session state. When you click the add button you cannot in your previous code just keep using dt because dt scope was lost in your prior code. We do this by assigning the sessioned datatable which was stored prior to a temp datatable. We add the row and reset the session that way the next time we add a row it will display the second row, then third row, and so on...

我推荐一个好的asp书,如开始ASP 3.5在C#2008上有相同主题提出吨vb书籍。

I recommend a good asp book on such as Beginning ASP 3.5 in C# 2008. There are a ton of vb books on the same subject matter.

更多推荐

结合数据表到网格视图

本文发布于:2023-11-30 14:59:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650419.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网格   视图   数据表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!