You all were very helpful to me in getting this figured out. In my case, the accepted answer did not work because my dropdowns are created in code-behind during ajax as GenericHtmlControls and therefor, do not exist at page load to hook the events with the .dropdown selector. Additionally, my pages can have many dropdowns dynamically added that are affected by this, as well as any number of other dropdowns that are NOT affected.
I added a unique css class (editModeModuleMenu) to the affected div(s) containing the actual menu and previously marked with .dropdown-menu, changed the event selector to window and scoped the function to ONLY select and alter the single dropdown in focus.
$(window).on('show.bs.dropdown', function (e) {
var el = $(e.target).closest('.editModeModuleMenu');
if (el.length == 0) return;
$('body').append(el.css({
position: 'absolute',
left: el.offset().left,
top: el.offset().top
}).detach());})
And here is the relevant code-behind:
private void attachSettingGear(Panel container)
{
var dropdown = new HtmlGenericControl("div");
dropdown.Attributes["class"] = "dropdown";
dropdown.Style["position"] = "absolute";
dropdown.Style["top"] = "2px";
dropdown.Style["right"] = "10px";
var a = new HtmlGenericControl("a");
a.Attributes["href"] = "#";
a.Attributes["class"] = "dropdown-toggle";
a.Attributes["data-bs-toggle"] = "dropdown";
a.Style["color"] = "white";
dropdown.Controls.Add(a);
var gear = new HtmlGenericControl("span");
gear.Attributes.Add("class", "navBarIcon bi-gear");
a.Controls.Add(gear);
container.Style["position"] = "relative";
container.Controls.Add(dropdown);
var menu = new HtmlGenericControl("div");
menu.Attributes["class"] = "dropdown-menu editModeModuleMenu";
dropdown.Controls.Add(menu);
createMenuLink(menu, "Delete", "trash3", $"javascript:popupConfirm('Confirm Delete','Are you sure you want to delete this module?', 'Delete', 'Cancel', deletePageModule, null, {ModuleSettings.ID})");
}
private void createMenuLink(HtmlGenericControl menu, string text, string icon, string href)
{
var ico = new HtmlGenericControl("span");
ico.Attributes.Add("class", "navBarIcon bi-" + icon);
var item = new HtmlGenericControl("a");
item.Attributes["class"] = "dropdown-item";
item.Attributes["href"] = href;
item.InnerText = text;
item.Controls.AddAt(0, ico);
menu.Controls.Add(item);
}
Thank you all for the excellent information. I hope my addition helps someone else struggling with this.