Pages

Thursday, December 30, 2010

EP pop-up windows with checkboxes

When building pop-up windows with checkboxes it seems like you can't make the pop-up fields with a checkbox as target. When later reading the value of the pop-up field it always returns “on”.

So with checkboxes you must write the value yourself when closing the pop-up window.

Here is an example. First some code from my pop-up parent:
protected void AddChangeInvoiceBlockScript(SetMenuItemPropertiesEventArgs e, DataSetViewRow row)
{
    AxUrlMenuItem menuItem = new AxUrlMenuItem(BLOCK_DIALOG);

    if (row != null)
    {
        AxTableContext context = AxTableContext.Create(row.GetTableDataKey(row.DataSetView.Metadata.RootDataSource, null));

        menuItem.MenuItemContext = context;

        menuItem.RemoveNavigation = true;

        e.MenuItem.ClientOnClickScript = this.PopupChangeBlocked.GetOpenPopupEventReference(menuItem);
    }
}

And next initialization code and ButtonOK code from my pop-up:
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        CheckBoxBlock.Focus();

        IAxaptaRecordAdapter    ledgerJournalTransRecord;
        AxBaseWebPart           webpart = AxBaseWebPart.GetWebpart(this);

        if (webpart.ExternalContext != null)
        {
            ledgerJournalTransRecord = webpart.ExternalContext.DataKey.GetRecord(this.AxSession);
            if (ledgerJournalTransRecord != null)
                if ((int)ledgerJournalTransRecord.GetField(LEDGERJOURNALTRANS_WMAPRBLOC) == 1)
                    CheckBoxBlock.Checked = true;
        }
    }
}

protected void OkButton_Click(object sender, EventArgs e)
{
    try
    {
        string fieldValueBlock = CheckBoxBlock.Checked ? BLOCK_YES : BLOCK_NO;
         
        // Set the value based on the state of the checkbox
        popupChild.SetFieldValue("hiddenBlockField", fieldValueBlock);

        // Calling the script for closing the dialog box
        this.popupChild.ClosePopup(true, true);
    }
    catch (Exception ex)
    {
        AxExceptionCategory exceptionCategory;
        // This returns true if the exception can be handled here
        if (!AxControlExceptionHandler.TryHandleException(this, ex, out exceptionCategory))
        { 
            // The exception was fatal - in this case re-throw the error
            throw;
        }
    }
}

And finally closing code from my pop-up parent:
void PopupChangeBlocked_PopupClosed(object sender, EventArgs e)
{
    // Get the value of the Blocked check box
    bool block = this.PopupChangeBlocked.GetFieldValue("hiddenBlockField") == "1" ? true : false;

    // Send the value to the dataset, which will handle the update 
    this.dsWMAprApprovalList.GetDataSet().DataSetRun.AxaptaObjectAdapter.Call("setWMAprBlock", block);

    // Clear popup values
    this.PopupChangeBlocked.ClearFieldValues();

    // Refresh the grid
    this.gridWMAprApprovalList.DataBind();
}

You can see another example on how to use checkboxes with pop-ups in the
TSTimesheetDeleteYesNo User Control.

Other resources:
Pop-up Windows on MSDN
AxPopupParentcontrol on MSDN
AxPopupChildControl on MSDN
Solution Monkey "Devlopers Cookbook"