在ASP.NET中使用datatable [英] Using datatable in ASP.NET

查看:67
本文介绍了在ASP.NET中使用datatable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个asp.net数据输入页面,每次用户点击按钮时,刚输入的数据将被保存在临时数据表中,然后才能实际发送到后端。代码如下:



  Imports  System.Data。 SqlClient 

公共 dt
Inherits System.Web.UI.Page

Dim ds 正如 dsAster

Dim dtable As DataTable
Dim r As DataRow

受保护的 Sub Page_Load( ByVal sender 作为 对象 ByVal e As System.EventArgs)句柄 .Load


结束 Sub

受保护的 Sub Button2_Click(发件人作为 对象,e As EventArgs)句柄 Button2.Click


dtable = ds.Tables( dt1

r = dtable.NewRow

r( col1)= .TextBox1 .Text
r( col2)= string = + Me .TextBox2.Text.ToString

dtable.Rows.Add(r)

Me .DataList1.DataSource = dtable
< span class =code-keyword> Me .DataList1.DataBind()





现在问题是每次用户单击保存按钮,它将覆盖之前输入的按钮。简而言之,DataTable只包含最后输入的记录。我需要的是新记录将附加到数据表。

我认为这是因为每次页面加载时都会创建数据表。



我尝试了什么:



我的想法是不重新创建数据表,如果它不是回发,所以我将声明部分移动到如果不是ispostback中的page_load ...结束if block,但这将使任何vars声明post page_load将无法识别。请帮忙。这两天我一直在拉我的头发:(

谢谢大家。

解决方案

我建​​议你保留 DataTable 会话中并在更新表格之前检查会话可用性。



当您检查是否存在时,您可以轻松地从会话获取数据,然后更新表并将其分配回会话

Hi all,
I have an asp.net data entry page, and everytime the user click button the data just entered will be saved in a temp datatable before actually send to the backend. The code is following:

Imports System.Data.SqlClient

Public Class dt
    Inherits System.Web.UI.Page

            Dim ds As New dsAster

            Dim dtable As DataTable
            Dim r As DataRow

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


    End Sub

Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click


        dtable = ds.Tables("dt1")

        r = dtable.NewRow

        r("col1") = Me.TextBox1.Text
        r("col2") = "string=" + Me.TextBox2.Text.ToString

        dtable.Rows.Add(r)

        Me.DataList1.DataSource = dtable
        Me.DataList1.DataBind()



Now the prob is everytime the user click the save button, it will override the one that entered previously. In short, the DataTable will only contain last entered record. What i require is new records will be appended to the datatable.
I think it's because the datatable got created everytime the page load.

What I have tried:

My idea is to not to recreate the datatable if it's not a postback, so I move the declaration portion into page_load in a if not ispostback...end if block, but that will make any vars declare post page_load will not be recognised. Pls help. I've been pulling my hair these past 2 days :(
Thanks guys.

解决方案

I would suggest you to keep the DataTable in Session and check the Session availability before updating the table.

When you check if that is present, after that you can easily get data from Session and then update the table and assign it back to Session.


这篇关于在ASP.NET中使用datatable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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