Skip to content

Instantly share code, notes, and snippets.

@oesolberg
Last active January 18, 2020 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oesolberg/0f6bcebda44f090ec3ba325da649cc3d to your computer and use it in GitHub Desktop.
Save oesolberg/0f6bcebda44f090ec3ba325da649cc3d to your computer and use it in GitHub Desktop.
ButtonWithDialog
using System.Text;
// Use with a script at the top of your form
// returnString.AppendLine(
//"<script type='text/javascript'>function userConfirm(itemName) { var answer=confirm('hey - are you sure?? All the devices for the item '+itemName+' will be deleted! ');if(answer){return 'Submit';}else{return 'Cancel';} }</script>");
//
// Usage in code
// var deleteButton = new ButtonWithDialog()
// {
// Name = DeleteButtonKey + HttpUtility.JavaScriptStringEncode(itemId),
// Label = "Delete",
// FunctionToCallWithSubmitStringResult = "userConfirm('" + itemName + "');",
// PageName = _pageName,
// ResultStringOnCancel = "Cancel",
// ResultStringOnOk = "Submit",
// ReloadOnOk = true,
// ReloadOnCancel = false
// };
namespace Config.Controls
{
public class ButtonWithDialog
{
public string Name { get; set; }
public string Label { get; set; }
public string PageName { get; set; }
public string FunctionToCallWithSubmitStringResult { get; set; }
public bool ReloadOnCancel { get; set; }
public bool ReloadOnOk { get; set; }
public string ResultStringOnCancel { get; set; }
public string ResultStringOnOk{ get; set; }
public string Build()
{
var sb = new StringBuilder();
sb.AppendLine($"<!-- ButtonWithDialog {Name} -->");
sb.AppendLine("<script>");
sb.AppendLine("$(function() {");
sb.AppendLine("$('#" + Name + "').button({icons: {primary:'',secondary:''} });");
sb.AppendLine("$('#" + Name + "').click(function(e) {");
sb.AppendLine("var theResult=" + FunctionToCallWithSubmitStringResult + "; ");
sb.AppendLine("var theForm =$('#'+$(this)[0].form.id);");
sb.AppendLine("var theData = theForm.serialize();");
sb.AppendLine("returnTrue = false;");
sb.AppendLine("theData = theData+'&id='+this.id+'&"+Name+"='+theResult;");
sb.AppendLine("commonAjaxPost(theData,'"+PageName+"');");
if (ReloadOnCancel)
{
sb.AppendLine("if(theResult==='" + ResultStringOnCancel + "') {");
sb.AppendLine("location.reload();");
sb.AppendLine("}");
}
if (ReloadOnOk)
{
sb.AppendLine("if(theResult==='" + ResultStringOnOk + "') {");
sb.AppendLine("location.reload();");
sb.AppendLine("}");
}
sb.AppendLine(" return false;");
sb.AppendLine("});");
sb.AppendLine("});");
sb.AppendLine("</script>");
sb.AppendLine("<button type='submit' id='"+Name+"' name='"+Name+"' >"+Label+"</button>");
return sb.ToString();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment