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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OnUpdate method running twice 1

Status
Not open for further replies.

eguthrie

Programmer
Nov 30, 2001
51
US
Hi. I have an OnUpdate method that is triggered from a datagrid. The problem is that when the event is triggered, the code in the OnUpdate method runs twice in succession. I traced it through, and after it finished the last line of code, it immediately starts again on the 1st line.

I made sure AutoEventWireup is set to false. Can anyone tell me why this might be happening?

TIA
 
The ItemCommand event always occurs first when you click on the "update" link in the grid, then the UpdateCommand event occurs. So check if the ItemCommand event of the grid is wired to the same OnUpdate handler.
 
No, the ItemCommand event isn't wired to anything. I can't see that anything else is triggering OnUpdate.
 
Check code behind, in the InitializeComponent, there can be something like this:
Code:
this.myGrid.ItemCommand  += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.OnUpdate);
 
Thanks. But I've already checked the InitializeComponent code. There's only one reference to the OnUpdate event handler:
this.dgBudget.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.OnUpdate);

Any other places I can check?

 
That's weird. Just a guess, what happens in the Page_Load? Do you do anything in there, probably to bind the grid? And if you do, do you check for Page.IsPostBack?
 
Yes, it's weird and very aggravating.

Actually, I'm not even binding the grid in Page_Load. The grid is on a hidden Panel that is made visible after some selections from a couple of dropdown lists are made and a button is clicked. The only thing that's done in Page_Load is to populate the dropdown lists.
 
There is only one place to check left: in the <asp:DataGrid> tag, there might be OnItemCommand='OnUpdate'. That's all I can think of.
 
No luck there. Only the 3 I'd expect (OnUpdateCommand, OnEditCommand, OnCancelCommand).

Thanks for your help. If I figure out what the problem is, I'll post it.
 
I would probably try rebuilding the page, step by step and just see what action causes the error.
 
If I can't figure it out any other way, I'll do that. Thanks again. Let me know if you think of anything else I could try.
 
It never goes back to Page_load between the 1st and 2nd run of the OnUpdate method code. I put breakpoints in OnUpdate and traced it through the method code. It goes directly from the last line of code in that method to the first line in the same method.


 
No to both questions.

I think the problem lies with the datagrid. All of the event methods (OnEdit, OnCancel, and OnUpdate) have that same problem of it going through the code twice.

I tried it with some other events (button events), and the problem didn't occur.
 
I fixed the problem, but I'm not sure why it worked.

In the Visual Studio IDE in the Events section of the datagrid Properties, I removed the event handler from the UpdateCommand, EditCommand, and CancelCommand. After I did that, it now no longer goes through the code twice!

I think I'm still somewhat unclear on many aspects of the datagrid. I don't know why specifying the Event handler in the event properties would have caused it to fire off twice.

Thank you LV and Edelwater for your help.

-Evelyn


 
It's weird, 'cause whatever event handlers you have typed in the Properies window, should be reflected in the code behind InitializeComponent. Well, it's fixed and that what matters.
 
Yeah, I think something is not functioning as it's supposed to, but it's working now anyway. Maybe it will rear its ugly head in some other unexpected way later as a result.

 
I have had the same thing happen to me on a couple of pages. I step throughit with the debugger line by line and it goes to the from the very last line to the first line of the function and executes it all over again.

What's bad is if its an insert statement. It creates 2 new records. I never did figure out what it was, it just stopped for some unknown reason.

I even set a flag to keep it from running the function the second time, but strangely it kept resetting back the flag to false when it should have been true. Really strange!

I think it is some kind of bug in VS.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top