为什么我的Button Click事件只触发一个?

时间:2011-11-17 22:22:19

标签: c# asp.net .net button event-handling

我在网上四处寻找并且无法找到相关解决方案。

我创建了一个简单的.net Web应用程序,它从本地Access数据库中读取数据。连接在读取第一行并填充文本框时起作用。

当我单击按钮时,它会读取并填充第二行,但它会停止工作,并且不会读取下一行数据。我没有收到任何错误或构建问题。按钮停止工作!我缺少哪些事件处理程序?

通过使用IsPostBack来查看页面之前是否已加载以及ViewState在按钮点击/页面重新加载时存储位置值。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication6._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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"></asp:TextBox>
        <br />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" EnableClientScript="False"/>
        <br />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

    </div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;

namespace WebApplication6
{
    public partial class _Default : System.Web.UI.Page
    {       
        private OleDbConnection connection;
        private OleDbCommand command;
        private OleDbDataAdapter adapter;
        private DataSet dataset;
        private string firstname;
        private string lastname;
        private int age;
        private int position;

        protected void Page_Load(object sender, EventArgs e)
        {
            connection = new OleDbConnection();
            command = new OleDbCommand();
            adapter = new OleDbDataAdapter();
            dataset = new DataSet();

            connection.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\People.mdb;Persist Security Info=False";

            command.Connection = connection;
            command.CommandText = "SELECT * FROM Table1";

            adapter.SelectCommand = command;
            adapter.Fill(dataset, "Table1");

            ShowValuesOfRow(0);
        }

        private void ShowValuesOfRow(int pos)
        {
            DataRow row = dataset.Tables["Table1"].Rows[pos];
            position = pos;

            firstname = row["FirstName"].ToString();
            lastname = row["LastName"].ToString();
            age = (int)row["Age"];

            TextBox1.Text = firstname;
            TextBox2.Text = lastname;
            TextBox3.Text = age.ToString();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int lastIndex = dataset.Tables["Table1"].Rows.Count - 1;

            if (position < lastIndex)
            {
                position++;
                ShowValuesOfRow(position);
            }
        }
    }
}

3 个答案:

答案 0 :(得分:4)

问题是位置变量。每次单击该按钮时,页面都会再次遍历整个生命周期,并将位置重新初始化为0(这适用于所有变量)。解决此问题的一种方法是使用viewstate存储回发之间的位置。

MSDN:http://msdn.microsoft.com/en-us/library/system.web.ui.control.viewstate.aspx

示例:

public int Position
{
    get
    {
        int? position = null;
        if (ViewState["Position"] != null)
        {
            position = ViewState["Position"] as int?;
        }
        return position ?? 0;
    }
    set
    {
        ViewState["Position"] = value;
    }
}

答案 1 :(得分:0)

您想要使用

if(!IsPostBack)
在页面加载中

每次单击按钮时,页面加载都会运行并重置数据集。

另外,对'pos'使用静态变量,它也可能每次都重置。

(如果多个用户同时使用此页面,请注意静态变量......如果是这种情况,请查看会话变量)

答案 2 :(得分:0)

您需要在Web开发中理解的重要事实是,您在Web上根据请求/响应执行的所有操作都是无状态的 - 变量不会在各个请求之间保留在内存中(与桌面形式的应用程序相对)。您总是需要使用不同的方法,如ViewState,Cache,Session或HTML hiddent输入元素。

相关问题