You can use the ScriptManager
ScriptManager.RegisterStartupScript(this, GetType(),
"SuccessfullSave",
@"$('#SuccessfullSave').modal('show');
$('.modal-backdrop').appendTo('#aspnetForm');",
true);
Let me explain this code:
The ScriptManager.RegisterStartupScript
is a method that allows you to inject into your web page javascript
The parameters you pass are:
- the page (
this
)
- the type [use
GetType()
]
- the "name" or key of the script
- the actual JavaScript itself (which in this case is the JQuery code
to show a modal
$('#SuccessfullSave').modal('show');
where #successfullSave is the id of the modal and $('.modal-backdrop').appendTo('#aspnetForm');
is code to correct
styles so the modal is up in front and not weirdly placed behind a
gray see through background)
- and the last parameter is a
bool
value saying insert this code
inside of script tags
This completed code would look something like below
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
}
}
catch (System.Exception ex)
{
label.Text = ex.toString();
//HERE IS WHERE YOU PUT THIS
ScriptManager.RegisterStartupScript(this, GetType(),
"ErrorMessage",
@"$('#NameOfModal').modal('show');
$('.modal-backdrop').appendTo('#aspnetForm');",
true);
}
}
To close the modal:
Instead of using the traditional modal dismiss that comes with bootstrap you need to call the opposite of $('#NameOfModal').modal('show');
That is $('#NameOfModal').modal('hide');
or you can just click outside the modal