This article shows you how to programmatically show, hide and pass value from a SharePoint 2013 popup modal dialog with server side code.
C# Code snippet for showing a popup / Modal dialog in SharePoint 2013
protected void btnShowPopUp_Click(object sender, EventArgs e)
{
// Perform your Server Side Operations Here
ShowBasicDialog("PopUp.aspx", "Basic PopUp From Server Side");
}
private void ShowBasicDialog(string Url, string Title)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine(@"<script type=""text/ecmascript"" language=""ecmascript"">");
sb.AppendLine(@"ExecuteOrDelayUntilScriptLoaded(openBasicServerDialog, ""sp.js"");");
sb.AppendLine(@" function openBasicServerDialog()");
sb.AppendLine(@" {");
sb.AppendLine(@" var options = {");
sb.AppendLine(string.Format( @" url: '{0}',",Url));
sb.AppendLine(string.Format( @" title: '{0}'",Title));
sb.AppendLine(@" };");
sb.AppendLine(@" SP.UI.ModalDialog.showModalDialog(options);");
sb.AppendLine(@" }");
sb.AppendLine(@"</script>");
ltScriptLoader.Text = sb.ToString();
}
C# code to hide a Popup modal dialog
private void ClosePopUpFromServerSide()
{
ltScript.Text = @"<script type='text/javascript'>window.frameElement.commitPopup();</script>";
}
protected void btnOk1_Click(object sender, EventArgs e)
{
// Perform your server side operations here
ClosePopUpFromServerSide();
}
C# code to pass value from Popup to base page
protected void btnOk2_Click(object sender, EventArgs e)
{
// Perform your server side operations here
System.Collections.Generic.List<string> Data = new System.Collections.Generic.List<string>();
Data.Add(txtData1.Text);
Data.Add(txtData2.Text);
PassDataFromServerSide(Data.ToArray());
}
private void PassDataFromServerSide(string[] Data)
{
string PopStr = @"<script type='text/javascript'>window.frameElement.commitPopup([DATA]);</script>";
StringBuilder sb = new StringBuilder();
if (Data.Length > 0)
{
sb.Append(@"new Array(");
for (int i = 0; i < Data.Length; i++)
{
sb.AppendLine(string.Format(@"'{1}'{2}", i, Data[i], i == Data.Length - 1 ? "" : ","));
}
sb.Append(@")");
}
ltScript.Text = PopStr.Replace("[DATA]", sb.ToString());
}
Articles Related To Modal Dialog
How to Show and Hide a Modal Pop up Dialog in a SharePoint 2013 Page or a Visual WebPart
How to redirect, reload or refresh a page after closing the SharePoint 2013 Modal Popup Dialog
How to pass or return value from a SharePoint 2013 Modal Popup Dialog to the Base Page
How to Programmatically Show or Hide a Modal Popup Dialog with Server Side Code in Share Point 2013
Leave a comment