ASP.NET 内联全局会话数据

本文关键字:会话 数据 全局 NET ASP | 更新日期: 2023-09-27 18:33:02

我无法弄清楚如何将字符串列表存储为全局变量,在单击按钮时添加到该列表中任何未知次数,将该列表存储在会话中,然后在单独的页面中正确访问该会话数据。我已经接近了,但发生了奇怪的事情,我的调试工作正在做更不寻常的事情。

以下是将 Button1_Click() 绑定到 Button1 的第一个页面脚本:

<%@ Page Language="C#" Debug="true"  %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Drawing" %>

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta charset="utf-8"/>
    <title></title>    
    <script type="text/javascript">
        function validateNums(ele, evt)
        {
            var charCode = (evt.which) ? evt.which : event.keyCode;
            if (charCode > 31 && (charCode < 48 || charCode > 57)) {
                if (ele.id == 'TextBox1') {
                    var i = document.getElementById('TextBox1').value;
                    document.getElementById('TextBox1').value = i.slice(0, -1);
                }
                else
                {
                    var b = document.getElementById('TextBox2').value;
                    document.getElementById('TextBox2').value = b.slice(0, -1);
                }
            }
        }
    </script>
    <script runat="server">
       private static int counter = 0;
       List<string> reg = new List<string>();
        protected void Page_Load(object sender, EventArgs e) 
        {
            string[] states = { "AK","AL","AR","AS","AZ","CA","CO","CT","DC","DE","FL","GA","GU","HI","IA","ID",
            "IL","IN","KS","KY","LA","MA","MD","ME","MH","MI","MN","MO","MS","MT","NC","ND","NE","NH","NJ","NM","NV","NY",
            "OH","OK","OR","PA","PR","PW","RI","SC","SD","TN","TX","UT","VA","VI","VT","WA","WI","WV","WY" };
            int b = 0;
            foreach (string i in states) 
            { 
            this.DropDownList1.Items.Add(states[b]);
            b++;
            }
            if (Session["counter"] != null)
            {
                Session.Clear();
            }
            else { counter = 0; }
        }
        protected void Page_Unload(object sender, EventArgs e) 
        {
            Session["counter"] = counter;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text.Length == 0) Alert_First();
            if (TextBox2.Text.Length == 0) Alert_Second();
            if (DropDownList1.SelectedValue == "") Alert_State();
            if (TextBox4.Text.Length == 0) Alert_Password();
            if (TextBox1.Text.Length != 0 && TextBox2.Text.Length != 0 && DropDownList1.SelectedValue != "")
            {
                if (Password_Validate()) Save_Registery();
                else Alert_Password();
            }
        }
        private bool Password_Validate() 
        {
            foreach (char i in TextBox4.Text)
            {
                if (char.IsNumber(i))
                    return true;
            }
            return false;
        }
        private void Alert_First() 
        {
            TextBox1.BackColor = Color.Red;
            TextBox1.Text = "A value is required";
        }
        private void Alert_Second()
        {
            TextBox2.BackColor = Color.Red;
            TextBox2.Text = "A value is required";
        }
        private void Alert_State()
        {
            DropDownList1.BackColor = Color.Red;
        }
        private void Alert_Password()
        {
            TextBox4.BackColor = Color.Red;
            TextBox4.Text = "A value is required with at least one numeric value";
        }
        private void Save_Registery() 
        {
           if (Session["regList"] != null)
            {
            reg.AddRange((List<string>)Session["regList"]);
             }
            reg.Add(TextBox1.Text);
            reg.Add(TextBox2.Text);
            reg.Add(DropDownList1.Text);
            reg.Add(TextBox4.Text);
            Session["regList"] = reg;
            TextBox3.Text = counter.ToString();
            counter++;
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            int count = 0;
            foreach(string i in reg)
               {
                       TextBox3.Text = TextBox3.Text + reg[0].ToString();
                       count++;
               }
        }
</script>
</head>
<body style="margin:auto; max-width:300px;">
    <form id="form1" runat="server">
    <div style="float:left; max-width:200px;" >
        <asp:Label ID="Label1" runat="server" Text="First Name: "></asp:Label><br />
        <asp:TextBox ID="TextBox1" runat="server"  ></asp:TextBox>
        <br />
        <asp:Label ID="Label2" runat="server" Text="Last Name: "></asp:Label>
        <br /> 
        <asp:TextBox ID="TextBox2" runat="server" ></asp:TextBox>
        <asp:Label ID="Label3" runat="server" Text="State"></asp:Label>
        <br /> 
        <asp:DropDownList ID="DropDownList1" runat="server">
          <asp:ListItem Value="" Text=""></asp:ListItem>
        </asp:DropDownList>
        <br />
        <asp:Label ID="Label4" runat="server" Text="Password: "></asp:Label>
        <br />
        <asp:TextBox ID="TextBox4" runat="server" ></asp:TextBox> 
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"  />
        <asp:Label ID="Label5" runat="server" Text="All Fields are Required" Visible="false"></asp:Label>
        <asp:TextBox ID="TextBox3" runat="server" TextMode="MultiLine"></asp:TextBox>
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" />
        <asp:HiddenField ID="HiddenField1" runat="server" />
    </div>
 <a href="Results.aspx" style="">
        <div>
            <p>
            Results
            </p>
        </div>
</a>
    </form>
</body>
</html>

这是第二页:

<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e) 
        {
            int counter = Convert.ToInt16(Session["counter"]);
            Repeater1.DataSource = (List<string>)Session["regList"];
            Repeater1.DataBind();
        }
    </script>
<meta charset="utf-8" />
    <title></title>    
</head>
<body>
    <form id="form1" runat="server">   
        <div>
            <%= Session["counter"] %>
        </div>
        <asp:Repeater ID="Repeater1" runat="server">
            <HeaderTemplate>
                <table>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td>
                        <%# Container.DataItem %>
                    </td>

                </tr>
            </ItemTemplate>
            <FooterTemplate>
            </table>
            </FooterTemplate>
        </asp:Repeater>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </form>
</body>
</html>

这仅输出我通过上一页输入的最后一组数据。我为帮助调试而添加的计数告诉我,我的会话变量"regList"中只有 3 个值。我的问题是为什么每次触发 Button1_Click() 时我的数据都会被覆盖?

ASP.NET 内联全局会话数据

在第一页的页面加载事件中,您正在通过 reg 重置 seesion 列表,

会话["regList"] = reg;

到那时,reg 是一个空列表,它成为一个问题