Reputation: 18734
I have a .Net method which does some validation on an object, and then, I need to display the issues to the user.
I am trying to use a jquery message box I found:
The jquery function:
function ShowPopup() {
$.msgBox({
title: "Unable to save",
content: "An error has occured while saving the object."
});
}
I need to call that from a .Net method, passing it a List of strings. Is that possible? And then set the content property to be the list of errors?
My .Net saving method, which may trigger this popup, looks like this:
protected void btnSave_Click(object sender, EventArgs e)
{
var o = new UserDto
{
DisplayName = txtName.Text,
Email = txtEmail.Text,
Username = txtUsername.Text,
Password = txtPassword.Text,
TimeZoneId = ddZones.SelectedValue,
Id = Session["SelectedUserId"] == null ? 0 : int.Parse(Session["SelectedUserId"].ToString())
};
var result = new UserService(Common.CurrentUserId()).SaveUser(o);
if (result.Success == false)
{
// Show an error.
return;
}
Response.Redirect("users.aspx");
}
If success is false, I want to pass it a list of errors, and show that popup.
The jQuery function is from here.
Upvotes: 0
Views: 2536
Reputation: 79
You can use ClientScriptManager http://msdn.microsoft.com/es-es/library/asz8zsxy.aspx to inject your javascript into the page.
protected void btnSave_Click(object sender, EventArgs e)
{
var o = new UserDto
{
DisplayName = txtName.Text,
Email = txtEmail.Text,
Username = txtUsername.Text,
Password = txtPassword.Text,
TimeZoneId = ddZones.SelectedValue,
Id = Session["SelectedUserId"] == null ? 0 : int.Parse(Session["SelectedUserId"].ToString())
};
var result = new UserService(Common.CurrentUserId()).SaveUser(o);
if (result.Success == false)
{
// Define the name and type of the client scripts on the page.
String csname1 = "MessageBoxScript";
Type cstype = this.GetType();
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
// Check to see if the startup script is already registered.
if (!cs.IsStartupScriptRegistered(cstype, csname1))
{
StringBuilder cstext1 = new StringBuilder();
cstext1.Append("<script type=text/javascript> $.msgBox({title: 'Unable to save',content: 'An error has occured while saving the object.'}); </");
cstext1.Append("script>");
cs.RegisterStartupScript(cstype, csname1, cstext1.ToString());
}
return;
}
Response.Redirect("users.aspx");
}
Another option is to save your errors in a session variable like:
C#
Session["Errors"] = "My errors";
Javascript:
var errors = '<%=Session["errors"]%>';
if(errors){
$.msgBox({
title: "Unable to save",
content: errors
});
}
Upvotes: 1
Reputation: 10824
you can call your jQuery method with ScriptManager this way :
if (result.Success == false)
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "tmp", "<script type='text/javascript'>ShowPopup();</script>", false);
return;
}
Upvotes: 0
Reputation: 4489
Try this
if (result.Success == false)
{
// Show an error.
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "close", "ShowPopup(parm1,parm2);", true);
return;
}
Hope it will helps you
Upvotes: 1
Reputation: 1117
I'm assuming that your btn_Save method fires in response to a client event, such as the user clicking the Save button. I'm also assuming you're using MVC. If that's the case, then the best way to accomplish what you're looking for is to make the Save button on the client fire a $.click event. In the click event, you call your MVC controller Save method using ajax. This way, the Save method can return JSON from the server, and you can display the returned messages on the client. Something like this:
Server:
[HttpPost]
public ActionResult Save(object myData)
{
return Json(new {message="Hello World"});
}
Client:
$('#saveBtn').click(function()
{
$.post('@Url.Action("Save")',
{myData: data},
function(result){
if (result){
showPopup(result);
}
}
)
})
Upvotes: 0