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

P

Phil Sweeney

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
 
K

Keith Smith [MSFT]

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: (e-mail address removed) (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: <[email protected]>
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: (e-mail address removed)
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top