"onClick" using c# / ASP.NET executes twice on a single click?

Discussion in 'ASP .Net Web Controls' started by Phil Sweeney, Nov 7, 2003.

  1. Phil Sweeney

    Phil Sweeney Guest

    Hello -

    I am trying to get started with ASP.NET and c# by creating a simple
    database "CRUD" program (CRUD: CREATE REPORT UPDATE DELETE) using a
    simple one table contacts database.

    I have come a long way, and have a form that seems to be working well
    for these functions except for one piece:

    When I use the "onclick" for an asp button, which then runs the code
    to insert to the database and refresh a datagrid with the new data,
    the insert statement (and all the code in the "onclick" piece?) runs
    TWICE...???

    I am new to ASP and event based VS controls so please forgive me if
    there is a simple solution to this problem - but I have searched many
    listings and tried many edits to the code and cannot find a solution.

    Thanks in advance for any help.

    Cheers -

    Phil

    ***

    Code snippets below:


    ****************
    WebForm1.aspx
    ****************

    <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" Trace="false"
    AutoEventWireup="false" Inherits="econtact.WebForm1" %>

    *
    *
    *
    <div>

    <asp:Label id="lname_label" style="LEFT: 272px; POSITION: absolute;
    TOP: 256px"
    runat="server">Last Name</asp:Label>

    <asp:TextBox id="add_lname_txt" style="Z-INDEX: 104; LEFT: 352px;
    POSITION: absolute; TOP: 256px" runat="server"></asp:TextBox>

    <asp:TextBox id="add_fname_txt" style="Z-INDEX: 103; LEFT: 96px;
    POSITION: absolute; TOP: 256px" runat="server"></asp:TextBox>

    <asp:Label id="fname_label" style="LEFT: 16px; POSITION: absolute;
    TOP: 256px"
    runat="server">First Name</asp:Label>

    <asp:Button id="btnAdd" style="Z-INDEX: 105; LEFT: 536px; POSITION:
    absolute; TOP: 256px" runat="server"Text="Add" OnClick="btnAdd_Click">
    </asp:Button>

    </div>
    *
    *
    *

    ****************
    WebForm1.aspx.cs
    ****************
    *
    *
    *

    public WebForm1()
    {
    Page.Init += new System.EventHandler(Page_Init);

    }

    private void Page_Load(object sender, System.EventArgs e)
    {
    // Put user code to initialize the page here
    if (! IsPostBack)
    {
    BindGrid();

    } //endif

    }


    private void Page_Init(object sender, System.EventArgs e)
    {
    // Put user code to initialize the page here
    InitializeComponent();
    }


    private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Page_Load);

    }

    *
    *
    *
    public void btnAdd_Click(object source, EventArgs e)
    {
    string fname = add_fname_txt.Text;
    string lname = add_lname_txt.Text;

    string dbconnstr = "Provider=Microsoft.Jet.OLEDB.4.0; Data
    Source=c:\\pjs\\VS\\webdb.mdb";
    string dbsqlstr = "INSERT INTO contacts (first_name, last_name,
    user_id) VALUES ('" + fname + "', '" + lname + "', 1)";


    try
    {

    System.Data.OleDb.OleDbConnection conn
    = new System.Data.OleDb.OleDbConnection(dbconnstr);
    conn.Open();

    System.Data.OleDb.OleDbCommand cmd = new
    System.Data.OleDb.OleDbCommand();
    cmd.CommandText = dbsqlstr;
    cmd.Connection = conn;
    cmd.ExecuteNonQuery();
    cmd.ExecuteReader();

    add_fname_txt.Text = null;
    add_lname_txt.Text = null;

    DataGrid1.EditItemIndex = -1;
    cmd.Dispose();
    conn.Close();

    BindGrid();

    }
    catch(OleDbException ex)
    {
    Console.WriteLine("Error: {0}", ex.Errors[0].Message);
    throw ex;
    }


    }

    *
    *
    *
    ***
    End Code Snippets
     
    Phil Sweeney, Nov 7, 2003
    #1
    1. Advertising

  2. Hello Phil,

    I apologize for the delay in responding to your post. I'm unable to reproduce the behavior you describe using the code snippets below. Are you still experiencing this problem? If so, please
    reactivate so we can dig deeper.

    Thanks,
    Keith

    --------------------
    >From: (Phil Sweeney)
    >Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
    >Subject: "onClick" using c# / ASP.NET executes twice on a single click?
    >Date: 6 Nov 2003 20:33:35 -0800
    >Organization: http://groups.google.com
    >Lines: 154
    >Message-ID: <>
    >NNTP-Posting-Host: 192.128.133.68
    >Content-Type: text/plain; charset=ISO-8859-1
    >Content-Transfer-Encoding: 8bit
    >X-Trace: posting.google.com 1068179615 11314 127.0.0.1 (7 Nov 2003 04:33:35 GMT)
    >X-Complaints-To:
    >NNTP-Posting-Date: Fri, 7 Nov 2003 04:33:35 +0000 (UTC)
    >Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!fu-berlin.de!postnews1.google.com!not-for-mail
    >Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet.webcontrols:15979
    >X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
    >
    >Hello -
    >
    >I am trying to get started with ASP.NET and c# by creating a simple
    >database "CRUD" program (CRUD: CREATE REPORT UPDATE DELETE) using a
    >simple one table contacts database.
    >
    >I have come a long way, and have a form that seems to be working well
    >for these functions except for one piece:
    >
    >When I use the "onclick" for an asp button, which then runs the code
    >to insert to the database and refresh a datagrid with the new data,
    >the insert statement (and all the code in the "onclick" piece?) runs
    >TWICE...???
    >
    >I am new to ASP and event based VS controls so please forgive me if
    >there is a simple solution to this problem - but I have searched many
    >listings and tried many edits to the code and cannot find a solution.
    >
    >Thanks in advance for any help.
    >
    >Cheers -
    >
    >Phil
    >
    >***
    >
    >Code snippets below:
    >
    >
    >****************
    >WebForm1.aspx
    >****************
    >
    ><%@ Page language="c#" Codebehind="WebForm1.aspx.cs" Trace="false"
    >AutoEventWireup="false" Inherits="econtact.WebForm1" %>
    >
    >*
    >*
    >*
    ><div>
    >
    ><asp:Label id="lname_label" style="LEFT: 272px; POSITION: absolute;
    >TOP: 256px"
    >runat="server">Last Name</asp:Label>
    >
    ><asp:TextBox id="add_lname_txt" style="Z-INDEX: 104; LEFT: 352px;
    >POSITION: absolute; TOP: 256px" runat="server"></asp:TextBox>
    >
    ><asp:TextBox id="add_fname_txt" style="Z-INDEX: 103; LEFT: 96px;
    >POSITION: absolute; TOP: 256px" runat="server"></asp:TextBox>
    >
    ><asp:Label id="fname_label" style="LEFT: 16px; POSITION: absolute;
    >TOP: 256px"
    >runat="server">First Name</asp:Label>
    >
    ><asp:Button id="btnAdd" style="Z-INDEX: 105; LEFT: 536px; POSITION:
    >absolute; TOP: 256px" runat="server"Text="Add" OnClick="btnAdd_Click">
    ></asp:Button>
    >
    ></div>
    >*
    >*
    >*
    >
    >****************
    >WebForm1.aspx.cs
    >****************
    >*
    >*
    >*
    >
    > public WebForm1()
    > {
    > Page.Init += new System.EventHandler(Page_Init);
    >
    > }
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > // Put user code to initialize the page here
    > if (! IsPostBack)
    > {
    > BindGrid();
    >
    > } //endif
    >
    > }
    >
    >
    > private void Page_Init(object sender, System.EventArgs e)
    > {
    > // Put user code to initialize the page here
    > InitializeComponent();
    > }
    >
    >
    > private void InitializeComponent()
    > {
    > this.Load += new System.EventHandler(this.Page_Load);
    >
    > }
    >
    >*
    >*
    >*
    > public void btnAdd_Click(object source, EventArgs e)
    > {
    > string fname = add_fname_txt.Text;
    > string lname = add_lname_txt.Text;
    >
    > string dbconnstr = "Provider=Microsoft.Jet.OLEDB.4.0; Data
    >Source=c:\\pjs\\VS\\webdb.mdb";
    > string dbsqlstr = "INSERT INTO contacts (first_name, last_name,
    >user_id) VALUES ('" + fname + "', '" + lname + "', 1)";
    >
    >
    > try
    > {
    >
    > System.Data.OleDb.OleDbConnection conn
    >= new System.Data.OleDb.OleDbConnection(dbconnstr);
    > conn.Open();
    >
    > System.Data.OleDb.OleDbCommand cmd = new
    >System.Data.OleDb.OleDbCommand();
    > cmd.CommandText = dbsqlstr;
    > cmd.Connection = conn;
    > cmd.ExecuteNonQuery();
    > cmd.ExecuteReader();
    >
    > add_fname_txt.Text = null;
    > add_lname_txt.Text = null;
    >
    > DataGrid1.EditItemIndex = -1;
    > cmd.Dispose();
    > conn.Close();
    >
    > BindGrid();
    >
    > }
    > catch(OleDbException ex)
    > {
    > Console.WriteLine("Error: {0}", ex.Errors[0].Message);
    > throw ex;
    > }
    >
    >
    > }
    >
    >*
    >*
    >*
    >***
    >End Code Snippets
    >



    --

    This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm

    Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.
     
    Keith Smith [MSFT], Dec 5, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. erdem

    page_load executes twice ?

    erdem, Jul 23, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    2,881
    erdem
    Jul 23, 2004
  2. aspx page executes twice

    , Mar 27, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    417
  3. David C

    Code in Page_Load executes twice

    David C, Aug 8, 2007, in forum: ASP .Net
    Replies:
    5
    Views:
    658
  4. Manny
    Replies:
    9
    Views:
    256
    Manny
    Aug 30, 2003
  5. Louis J Scoras
    Replies:
    0
    Views:
    140
    Louis J Scoras
    Sep 29, 2005
Loading...

Share This Page