We help IT Professionals succeed at work.

Button needs to be clicked twice to trigger btnSaveEvent() in VB.net

I have a form on web application, I load the field values on the form and click the save button and it doesn't save the record on the database at first click but if I press the button again it saves it. I put the breakpoint on my Page_Load()event and tried debugging it so what happens is as I run the application, Page_Load() gets executed populates the data on the grid. Now, if I enter field values and click save, it again goes to Page_Load() event but it doesn't go to button_save() event. But if I click it again then it goes to Page_Load() and then goes to button_Save() and saves the record.

I want to click the mouse button once and save the record, how could I be able to do that? I am using VB.NET. Thank you.
Comment
Watch Question

CERTIFIED EXPERT

Commented:
See if removing the Javascript ONCLICK properties helps it trigger at the first time.
Also when you edit the grid does it postback the data? or does it trigger a grid.changed event?
when you edit grid and then click save button the textbox loses focus and  might be triggering other events instead of the save button

Author

Commented:
I removed the ONCLICK Properties from Javascript still does the same thing


I put the field values on the text box and click the save. It goes to form_load() event but it doesn't call the btnSave() event. But on the next Save Click it goes to form_load()event and then goes to btnSave()event and display the grid result like shown above. 
David Johnson, CDSimple Geek from the '70s
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
are you using a jfavascript button or a web form button?
It shouldn't be reloading the page the button_click event shuld be fireing

Author

Commented:
Thanks David, that is what I am expecting it should not reload the page but when I put the break point in the form_load() event every time I save button the first thing it does is it loads the page and during the second click it again calls Page_Load() and then goes to btnSave_Click().  Below is the btnSave_Click() event

Protected WithEvents btnSave As Global.System.Web.UI.WebControls.Button



I believe this is what happened in my program.......
So, the pitfall was, I had txtFrameID text box as txtframeID_TextChanged(sender As Object, e As EventArgs) and on the aspx page for this txtframeID, AutoPostBack="True". When I clicked the save button, it executed textchange event first, and since AutoPostBack was set true, it didn't go to save_button method. Therefore, after setting AutoPostBack = "False", the program executed and txtFrameID_TextChanged method was called first  and btnSave_method was called after then so I didn't have to click the save button again and got my solution.
So, the only change I made was on the AutoPostBack as shown below prior to it was set True.
<TD  colSpan="2" class="auto-style3"><asp:textbox id="txtFrameID" runat="server" style="display: inline" Width="292" AutoPostBack="False" maxlength="10" CssClass="form-control" OnTextChanged="txtFrameID_TextChanged"></asp:textbox>
    <asp:Button id="btnSave" runat="server" Text="Save" onclick="btnSave_Click"
Select all Open in new window

 I appreciate experts help here, and I apologize if I didn't illustrate my problem properly, I just started web application programming and debugging. Anyways thanks for Experts ideas. 
CERTIFIED EXPERT

Commented:
Exactly what I was trying to get at with my previous comment, but you never answered it.
textchanged event was interfering with the click event,  

This is what I was trying to say in above answer, hence asking if it was postingback but  you did not answer, and if you had events after editing the grid,
 but I guess I didnt explain well enough.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.