Hi
I want to define below code at only 1 place. Asof now i am writing it in each form. How it can be done through User Control . I have i User Control whose code is
JavaScript
var swalInit = swal.mixin({
buttonsStyling: false,
confirmButtonClass: 'btn btn-primary',
cancelButtonClass: 'btn btn-light'
});
function ShowPopup_Redirect(heading, body, type, url) {
swalInit.fire({
title: heading,
text: body,
type: type,
position: 'top',
allowOutsideClick: false,
});
}C#
×
Cancel
Delete
Ramco RamcoRank #462 · 5.4K reputationPosted Aug 23, 2024, 6:06 AM EDT
Hi Aman
Can you plase provide me full sample as per above code. That is also User Control
Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted Aug 23, 2024, 6:21 AM EDT
Hi Aman
How i will check at server side that User Has pressed Delete button or Cancel button
Thanks
Aman GuptaRank #193 · 12.4K reputationPosted Aug 23, 2024, 6:10 AM EDT
To centralize the code for the modal popup and the JavaScript functionality into a single place, you can use a User Control in ASP.NET. Here's a step-by-step guide on how you can do this:
Define the User Control:
You've already created a User Control
MBox.ascx. You should include the common JavaScript and modal HTML code in this file. Make sure the JavaScript is enclosed in script tags and that the modal HTML structure is correctly placed inside the user control.MBox.ascx:var swalInit = swal.mixin({ buttonsStyling: false, confirmButtonClass: 'btn btn-primary', cancelButtonClass: 'btn btn-light' }); function ShowPopup_Redirect(heading, body, type, url) { swalInit.fire({ title: heading, text: body, type: type, position: 'top', allowOutsideClick: false, }).then(function (result) { if (result.value) { window.location.href = url; } }); } × Cancel DeleteUse the User Control in Your Pages:
Now, you can include this User Control in any page where you need the modal popup functionality. To do this, you need to register the User Control in your page and then include it where needed.
In your .aspx page:
Call the JavaScript Function from Your Code-Behind:
To trigger the JavaScript function from your code-behind, you can use
ScriptManager.RegisterStartupScriptto call theShowPopup_Redirectfunction.In your .aspx.cs page:
protected void SomeEventHandler(object sender, EventArgs e) { string heading = "Confirmation"; string body = "Are you sure you want to proceed?"; string type = "warning"; string url = "https://www.example.com"; string script = $"ShowPopup_Redirect('{heading}', '{body}', '{type}', '{url}');"; ScriptManager.RegisterStartupScript(this, GetType(), "ShowPopup", script, true); }Ensure Your JavaScript Libraries Are Included:
Make sure that your page includes any required JavaScript libraries such as SweetAlert. You might include these libraries in your Master Page or specific pages where needed.
By centralizing the modal code in a User Control, you reduce duplication and make your codebase easier to maintain.
Aman GuptaRank #193 · 12.4K reputationPosted Aug 23, 2024, 6:01 AM EDT
Hi Ramco,
To centralize the code for the modal popup and the JavaScript functionality into a single place, you can use a User Control in ASP.NET. Here's a step-by-step guide on how you can do this:
Define the User Control:
You've already created a User Control
MBox.ascx. You should include the common JavaScript and modal HTML code in this file. Make sure the JavaScript is enclosed in script tags and that the modal HTML structure is correctly placed inside the user control.Use the User Control in Your Pages:
Now, you can include this User Control in any page where you need the modal popup functionality. To do this, you need to register the User Control in your page and then include it where needed.
Call the JavaScript Function from Your Code-Behind:
To trigger the JavaScript function from your code-behind, you can use
ScriptManager.RegisterStartupScriptto call theShowPopup_Redirectfunction.Ensure Your JavaScript Libraries Are Included:
Make sure that your page includes any required JavaScript libraries such as SweetAlert. You might include these libraries in your Master Page or specific pages where needed.
Hope this helps you Ramco.
Thanks