Thursday, October 11, 2012

2nd Popup Issue In SharePoint 2010 Visual Web Part (With Ajax Update Panel)

I need to popup an alert for the user 2 times based on his/her option picked.
 
Business scenario:
If user choose to deleting a master record, 1st popup and alert the user his/her action.
And if (s)he choose to delete, now I have to find if any child records effecting, if so, not to delete the main record but alert the user again for further action.

This is how I solved it: With the help of JavaScript and Server events.
Added two delete buttons on webpart designer
  1. Main Delete:
                   <asp:Button ID="btnDelete" runat="server" Text="Delete" Width="100px" OnClientClick="if(confirm('Are you sure you want to delete this saved record?')==true)return true;else return false;" CausesValidation="false" onclick="btnDelete_Click" class="ms-ButtonHeightWidth" ChildrenAsTriggers="true"/>            
 2. Hidden Delete: (using style to hide)
                        <asp:Button ID="btnDeleteConfirm" runat="server" Text="DeleteConf" Width="100px" CausesValidation="false" onclick="btnDeleteConfirm_Click" class="ms-ButtonHeightWidth" style="display:none" />
 
OnClientClick event of the Main delete button prompt for 1st initial popup. Once he/she choose to say YES, onclick="btnDelete_Click" will fire as coded in javascript function.
Now in the btnDelete_Click event if I find any child records (I am using entity framwork here) I will register and call ConfirmDelete()

protected void btnDelete_Click(object sender, EventArgs e) {
.
.
if(entityList.Count > 0)
{
   RegisterPopupScript();
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "PopupDelete", "ConfirmDelete();", true);
}else
{MasterSalesRepService.Delete(Convert.ToInt32(lblSalesRepId.Text));
RedirectPage();
}
}

private void RegisterPopupScript()
        {
            Type type =
this.GetType();
            if (Page.ClientScript.IsStartupScriptRegistered(type, "
PopupScript"))           
               
return;
                       
            System.Text.
StringBuilder script = new System.Text.StringBuilder();
script.Append("<script language=\"javascript\" type=\"text/javascript\">\n");
script.Append("function ConfirmDelete(){if(confirm('Do you want to delete child record(s) as well?')==true) return document.getElementById('" + btnDeleteConfirm.ClientID + "').click(); else  return false; }");
script.Append("
</script >");                      
           
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "PopupScript", script.ToString(), false);          
        } 

protected void btnDeleteConfirm_Click(object sender, EventArgs e)
        {
            DeleteXrefSalesRep();
            DeleteMasterSalesRep();
            RedirectPage();
        }

In the script I will fire "btnDeleteConfirm_Click" only if user proceed other wise I won't
Note: I won't delete main record as well as child record if user cancels the action as you see from my btnDelete_Click code.

No comments: