使用会话变量在数据表中创建和存储值

时间:2016-03-21 19:58:27

标签: c# asp.net datatable

我在存储和显示数据表中的值时遇到问题。我的目标是为数据表添加一个值,重新加载页面,然后显示具有附加值的数据表,并提供输入更多值的功能。目前,我甚至无法显示数据表。我确信这是一个在会话中保存数据的问题,但我无法让会话中的保存/加载工作。

我知道你们可以给我的任何帮助,如果它真的非常简单,我会道歉。似乎我只需要第二个意见。

这是我当前的网页,

function setPro(sourceId,type,destId){
    document.getElementById(destId).style. + type = document.getElementById(sourceId).value;

}

我目前的代码背后,

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <p><asp:TextBox ID="txtName" runat="server"></asp:TextBox><asp:TextBox ID="txtEmail" runat="server"></asp:TextBox><asp:TextBox ID="txtAddress" runat="server"></asp:TextBox><asp:TextBox ID="txtInfo" runat="server"></asp:TextBox></p>
    <p>
        <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click"/></p>
    </div>
    <div>
        <table border="1">
            <asp:Repeater ID="rptrData" runat="server">
                <HeaderTemplate>
                    <tr>
                        <td>Name</td>
                        <td>Email</td>
                        <td>Address</td>
                        <td>Info</td>
                    </tr>
                </HeaderTemplate>
                <ItemTemplate>
                    <tr>
                        <td><%#Eval("Name") %></td>
                        <td><%#Eval("Email") %></td>
                        <td><%#Eval("Address") %></td>
                        <td><%#Eval("Info") %></td>
                    </tr>
                </ItemTemplate>
                <AlternatingItemTemplate>
                    <tr>
                        <td><%#Eval("Name") %></td>
                        <td><%#Eval("Email") %></td>
                        <td><%#Eval("Address") %></td>
                        <td><%#Eval("Info") %></td>
                    </tr>
                </AlternatingItemTemplate>
            </asp:Repeater>
        </table>
    </div>
    </form>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

您的代码有一些基本问题。如果你看到我改变了什么,并将它与你所拥有的进行比较,你应该能够找出一些问题。

public DataTable MyDT;
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        MyDT = new DataTable();
        MyDT.Columns.Add("Name");
        MyDT.Columns.Add("Address");
        MyDT.Columns.Add("Email");
        MyDT.Columns.Add("Info");
        Session["MyDT"] = MyDT;
    }
    else
    {
        // Here we load the session datatable to the variable you declared at the top.
        // It will be ready for us in the button click event.  We know this because
        // of asp.net page lifecycle which will run the page_load before the button
        // click event.

        MyDT = (DataTable)Session["MyDT"];
    }

    // You can bind the repeater here also if you want.  Doesn't really make a huge difference
}



protected void btnAdd_Click(object sender, EventArgs e)
{
    string Name = txtName.Text;
    string Address = txtAddress.Text;
    string Email = txtEmail.Text;
    string Info = txtInfo.Text;

    DataRow dtRow = MyDT.NewRow();
    dtRow["Name"] = Name;
    dtRow["Address"] = Address;
    dtRow["Email"] = Email;
    dtRow["Info"] = Info;
    MyDT.Rows.Add(dtRow);

    rptrData.DataSource = MyDT;
    rptrData.DataBind();
}

您会注意到我甚至不必将数据表保存回会话变量。 DataTable是一个引用类型,因此我对变量MyDT所做的任何更改都将保存到Session变量所指向的相同位置。

如果您的MyDT课程,我也删除了顶部的新声明。你不需要那个。

最后,您会看到在首次加载页面时一次创建列。正如有人在您的问题中评论重新创建列不是一个好主意或需要。

答案 1 :(得分:0)

我在VB中有这个解决方案,没有时间自己转换为C#,但希望这会有所帮助。

这个想法是在加载页面时首先创建表并将其存储在session var中。然后添加到它,创建一个新的DataRow并将它们插入表中,更新会话var,并重新绑定到网格。

$ python manage.py update_index --remove