Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > "onClick" using c# / ASP.NET executes twice on a single click?

Reply
Thread Tools

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

 
 
Phil Sweeney
Guest
Posts: n/a
 
      11-07-2003
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
 
Reply With Quote
 
 
 
 
Keith Smith [MSFT]
Guest
Posts: n/a
 
      12-05-2003
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: http://www.velocityreviews.com/forums/(E-Mail Removed) (Phil Sweeney)
>Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontro ls
>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: <(E-Mail Removed) >
>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 Removed)
>NNTP-Posting-Date: Fri, 7 Nov 2003 04:33:35 +0000 (UTC)
>Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed 00.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.webcontro ls:15979
>X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontro ls
>
>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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Code in Page_Load executes twice David C ASP .Net 5 08-09-2007 02:17 PM
aspx page executes twice rajendra.mishra@gmail.com ASP .Net 0 03-27-2006 04:18 PM
[Rails] Active Record: Stand alone: code executes twice Louis J Scoras Ruby 0 09-29-2005 01:50 PM
page_load executes twice ? erdem ASP .Net 3 07-23-2004 08:28 PM
Stumped - page executes twice sometimes (even thrice occasionally!) Manny ASP General 9 08-30-2003 02:36 PM



Advertisments