简体   繁体   中英

Why is this button OnClick event only working once?

I've had a look at the other issues people have reported but none of these solve the problems I'm having. I've been using this walkthrough from MS to manipulate an Access DB using an ASP.net application. Unfortunately the button to add a row has worked once and no longer does anything on either my local webserver or the inbuilt .NET one. I can't work out what the issue is and I've been banging my head against the desk since yesterday.

Here's my code;

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="editTest._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">
    <asp:DataGrid ID="datagrid" runat="server" AutoGenerateColumns="False" 
        oncancelcommand="datagrid_CancelCommand" oneditcommand="datagrid_EditCommand" 
        onupdatecommand="datagrid_UpdateCommand">
        <Columns>
            <asp:EditCommandColumn CancelText="Cancel" EditText="Edit" UpdateText="Update">
            </asp:EditCommandColumn>
            <asp:BoundColumn DataField="firstName" HeaderText="First Name">
            </asp:BoundColumn>
            <asp:BoundColumn DataField="lastName" HeaderText="Surname"></asp:BoundColumn>
            <asp:BoundColumn DataField="sex" HeaderText="Gender"></asp:BoundColumn>
            <asp:BoundColumn DataField="phoneNo" HeaderText="Telephone"></asp:BoundColumn>
        </Columns>
    </asp:DataGrid>
    <div>

        <asp:Button ID="Button1" runat="server" Text="Button" />

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

and the C# end;

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

namespace editTest
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) ReadRecords();
        }

        private void ReadRecords()
        {
            OleDbConnection cnt = null;
            OleDbDataReader rdr = null;

            try
            {
                cnt = new OleDbConnection(
                    "Provider=Microsoft.Jet.OLEDB.4.0; " +
                    "Data Source=" + Server.MapPath("test/accessTest.mdb"));
                cnt.Open();

                OleDbCommand cmd = new OleDbCommand("Select * FROM editTest", cnt);
                rdr = cmd.ExecuteReader();

                datagrid.DataSource = rdr;
                datagrid.DataBind();
            }

            catch (Exception e)
            {
                Response.Write(e.Message);
                Response.End();
            }

            finally
            {
                if (rdr != null) rdr.Close();
                if (cnt != null) cnt.Close();
            }
        }

        private void ExecuteNonQuery(string sql)
        {
            OleDbConnection cnt = null;
            try
            {
                cnt = new OleDbConnection(
                    "Provider=Microsoft.Jet.OLEDB.4.0; " +
                    "Data Source=" + Server.MapPath("test/accessTest.mdb"));
                cnt.Open();

                OleDbCommand cmd =
                    new OleDbCommand(sql, cnt);
                cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                Response.Write(e.Message);
                Response.End();
            }
            finally
            {
                if (cnt != null) cnt.Close();
            }
        }

        protected void datagrid_CancelCommand(object source, DataGridCommandEventArgs e)
        {
            datagrid.EditItemIndex = -1;
            ReadRecords();
        }

        protected void datagrid_EditCommand(object source, DataGridCommandEventArgs e)
        {
            datagrid.EditItemIndex = e.Item.ItemIndex;
            ReadRecords();
        }

        protected void datagrid_UpdateCommand(object source, DataGridCommandEventArgs e)
        {
            int ID = (int)datagrid.DataKeys[(int)e.Item.ItemIndex];

            string firstName = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
            string lastName = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
            string sex = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
            string phoneNo = ((TextBox)e.Item.Cells[4].Controls[0]).Text;

            string sql =
                "UPDATE editTest SET firstName=\"" + firstName +
                "\", lastName=\"" + lastName + "\", sex=\"" + sex +
                "\", phoneNo=\"" + phoneNo + "\"" +
                " WHERE ID=" + ID;
            ExecuteNonQuery(sql);

            datagrid.EditItemIndex = -1;
            ReadRecords();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            string sql = "INSERT INTO editTest (firstName, lastName, sex, phoneNo)"
                        + " VALUES (\"new\", \"new\", \"new\", \"new\")";
            ExecuteNonQuery(sql);
            ReadRecords();
        }
    }
}

Thanks for any help

You missed the event on the aspx:

 <asp:Button ID="Button1" runat="server" Text="Button" OnClick='Button1_Click' />

and make Button1_Click public or protected.

Or, you can set it in the code-behind:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) ReadRecords();

        Button1.Click += Button1_Click;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM