0

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.

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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
    });
}

Comments

0

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);
             }
          }
     )
})

2 Comments

I think he uses WebForms seeing the method signature.
if that's the case, then he might find this one useful
0

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;
     }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.