Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

postback event occurs twice

Status
Not open for further replies.

raghu3

Programmer
Dec 19, 2005
96
US
Must be some thing simple.
The aspx file has 3 buttons: on click of each, there is javascript validation -> set a hidden field -> submit.

Post back reads the hidden field and figures out which button was clicked and performs the set operation. One such is insert a record in db. Others re-build the page.
Whenever I access the insert option: 2 records are inserted in the db: I have only 1 insert stmt ?
Why is this happening twice ??. Right now I am testing this and nobody else has access to this application.
The table has a date-time field with system date as default, the 2 inserts occur with in a span of a new micro seconds:
2006-03-27 09:56:54.077
2006-03-27 09:56:55.280


 
raghu3: Any chance you may be executing the Page_Load code twice, i.e., you're not using the

Sub Page_Load(...
If Not Page.IsPostBack Then
'page load code...
End If
End Sub
 
My code is a bit messy here it is:

private void Page_Load(object sender, System.EventArgs e)
{

//Javascript validations
ChangeButton.Attributes.Add("onClick", "return change()");
.
.
.
// list box
strList= "--Select--|" + DEF1 + "|" + at1.buildList();
chkExpand.Checked=false; // default expand is false
.
.
// parse string
List1.DataSource=list1;
List1.DataBind();

if (Page.IsPostBack)
{

// check which button is clicked.
string pb=Request.Form["pb"];


switch(pb)
{
case "change1" :
// request form variable for this process
.
.
break;

case .
.
.

case "insert":
// request form variable for this process
insertRec();
break;
.
.
.}
else { // not post back 1st time
defaults();
}


private void insertRec() {

// request form variables and insert record

}





} // page load





}
 
raghu3: Not a C# programmer but it looks like the intial part of your code will be executed whether its a Page Load or a Post Back. Perhaps it would be better to set your if (Page.IsPostBack) first under the Page Load event -- or better, if(Not Page.IsPostBack) -- of course if it is the Postback which is the probelm to begin with. It looks to me that the code just under Page Load is executed twice --

Someone with experience in C# can drop by and take a look.
 
Which is fine: the initial part has to execute regardless of post back.

The insert is one of the case stmt that is to happen on postback.
 
raghu3: Ok, I understand. What threw me was adding the javascript attribute which I would expect only to execute on Page Load and not Post Back. There are quite a few C# programmers here, hang in there and one will drop in and take a look.
 
I am still learning. I understood that the buttons are rendered on every page load so I added the JS validations to be executed on load.
What also I understood that the JS functions are cross-browser/cross platform: sure shots. ASP control validators are typical for PC/IE. My apps will run on PC/MACs: IE/Netscape/Firefox. Some PCs are really old:NT ...
 
Sounds good raghu3 - javascript is pretty reliable although not on all machines (thought I would think most). You bring up a good point about the buttons; I have all my attributes added in the Page Load and they work well on post back (perhaps ViewState is working here?). It looks from your code that you rebind the DataList on Page_Load - perhaps this might be the problem. Try placing the DataList rebind in a separate Sub and call that sub from if(Page.IsPostBack) and if(Not Page.IsPostBack). In VB if I had my code set up as above the DataList would bind twice.
 
Isadore: I'll try the databind in a sub. This appears to be a design issue.
If this were vb how would you resove it ?, the pseudocode will help me.
Being from unix/c background, I prefer the c# syntax.
Thanks in advance for your help.
 
databind in a sub does'nt work!. still 2 records for 1...
-raghu3
 
raghu3: Here's an idea; not sure why someone else hasn't dropped by (C# programmer) but it might be one of those days.

If I were looking at this problem I might abandon the following:

string pb=Request.Form["pb"];

..and use the button's OnClick event -- actually if you did that, you wouldn't have to test which button was clicked; you'd know from the code behind.

Sub btnA_Click(...)
'code for A
End Sub

Sub btnB_Click(...)
'code for B
End Sub

Sub btnC_Click(...)
'code for C
End Sub

It seems that perhaps by using the Request.Form format you may be inadvertently causing the button's event to click twice -- just a thought.
 
Will try this and update...
thanks a lot.
-raghu
 
raghu3: sorry couldn't be of more help; it just seems that the event is triggering twice and perhaps it is the result of validation + postback etc. Someone may yet drop in and give you a bit more insight on this.
 
is AutoEventWireup enabled? what version of .NET are u using?

Known is handfull, Unknown is worldfull
 
I'm a C# programmer - one finaly dropped by :)

I hate to say it but I can't really find anything wrong with the code you posted. I didn't say I was a very good C# programmer.

What I can say is that I have had this problem before and it wasn't caused in the Page_Load event. I can't for the life of me remember what it was - I was using a custom (ASP.net 1.1) master page solution at the time, I think it was that.

What else happens in your page? What version of .net are you using?


Yet another unchecked rambling brought to you by:
Oddball
 
Oddball and vbkris -thanks for the assistance here - I think raghu3 is genuinely favorable towards finding a solution.
 
AutoEventWireUp is not enabled.
Using .net1.1 on windows 2003.

The other events are probably happening twice, I cannot know as they run queries and populate texts. Maybe the text is populated twice !
 
This is probably a design issue: will revisit the sequence of events, clean up the code and post the outcome here.
Thanks a lot for all the help.
 
Ok raghu3 - post back and lets take another look at this -- I think several who normally post here on a dailey basis are tied up at the moment. Look forward to a reviesed view of this.
 
Got it !.

Did old fashioned debugging:- inserted a record in a temp table in Page_Load and within postback. Found out that page_load happens twice.

Though the AutoEventWireUp is not enabled (in the aspx), the following function (in .cs) brings this up :

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


The above being generated when I started the project ... never realised that this needs to be in sync.

Thanks all ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top