I have a button that makes a call to an external url to log in to an accounting program (Xero) using OAuth 2. After receiving the token, etc., the button is hidden and a "Disconnect" button is shown. When the disconnect button is clicked, it is then hidden and the Connect button is displayed again. The problem is if I click the "Connect" button again, on postback it executes the code for the "Disconnect" button again, so I can never repeat the first cycle. The "Connect" button stays and all of the disconnect code runs again. I have tried to capture the postback being called by the first button, so that I could tell the disconnect code not to run again, but it is always null. Even worse, the postback always shows that the "Disconnect" button caused the postback even though it is no longer visible on the page. I do not understand why it is doing this. Both buttons are inside an "Update Panel" so that I can hide/show them.
Here is the button code:
<asp:UpdatePanel runat="server">
<ContentTemplate>
<div class="row" id="XeroNotConnected2" runat="server" visible="false">
<div class="col-sm-12 col-md-6">
<div class="form-group">
<label for="imgXero" class="col-sm-3 control-label"></label>
<div class="col-sm-9">
<asp:ImageButton runat="server" ID="imgXero" ImageUrl="~/assets/img/icons/connect-white.svg" OnClick="btnXeroConnect_Click" UseSubmitBehavior="False" />
</div>
</div>
</div>
</div>
<div id="XeroConnected" runat="server" visible="false">
<div class="row" >
<div class="col-sm-12 col-md-6">
<div class="form-group">
<label for="ddlTenants" class="col-sm-3 control-label">Select an Organization:</label>
<div class="col-sm-9">
<asp:DropDownList runat="server" ID="ddlTenants" CssClass="form-control" AutoPostBack="true" OnSelectedIndexChanged="ddlTenants_SelectedIndexChanged"></asp:DropDownList>
</div>
</div>
</div>
</div>
</div>
<div id="XeroConnected2" runat="server" visible="false">
<div class="row">
<div class="col-sm-12 col-md-6">
<div class="form-group">
<label for="btnDisconnect" class="col-sm-3 control-label"></label>
<div class="col-sm-9">
<button type="button" runat="server" class="btn btn-alt3" id="btnDisconnect" onclick="javascript:OpenModal('disconnectConfirm')"><i class="icon s7-plug"></i> Disconnect Xero</button>
</div>
</div>
</div>
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
In Page_Load I have:
if(IsPostBack)
{
Control pbControl = GetControlThatCausedPostBack(this.Page);
if (pbControl != null)
{
if (pbControl.ID == "lnkDelete")
{
InitiateConnect = false;
}
}
}
Method used to get Control that caused postback is:
private Control GetControlThatCausedPostBack(Page page)
{
LoggerBA.Log(DB_Context, Path.GetFileName(Request.ServerVariables["SCRIPT_NAME"]), MethodBase.GetCurrentMethod().Name, UtilityBA.LoggerLevel.Debug, "method");
//initialize a control and set it to null
Control ctrl = null;
//get the event target name and find the control
string ctrlName = page.Request.Params.Get("__EVENTTARGET");
if (!String.IsNullOrEmpty(ctrlName))
ctrl = page.FindControl(ctrlName);
//return the control to the calling method
return ctrl;
}
Code in disconnect button is:
protected void lnkDelete_Click(object sender, EventArgs e)
{
if(InitiateConnect == false)
{
InitiateConnect = true;
PageAsyncTask t = new PageAsyncTask(DisconnectTenant);
Page.RegisterAsyncTask(t);
Page.ExecuteRegisteredAsyncTasks();
Session.Remove("OAuthToken");
Session.Remove("xeroToken");
Session.Remove("Tenants");
Session.Remove("xeroTenant");
XeroConnected.Visible = false;
XeroConnected2.Visible = false;
btnDisconnect.Disabled = true;
XeroNotConnected2.Visible = true;
XeroNotConnected1.Visible = true;
}
}
Any assistance is greatly appreciated.