Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear all,

I would like to know, how can I create a pop-up dialog box, whenever "api_login is null". I would also like to know, how can I put url link in the pop-up box as well.

C#
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;

namespace User_test_Jan14.Controllers
{
    public class UserController : ApiController
    {
        private cdwEntities db = new cdwEntities();

        // GET api/User
        public IEnumerable<api_login> Getapi_login()
        {
            return db.api_login.AsEnumerable();
        }

        // GET api/User/5
        public api_login Getapi_login(string id)
        {
            api_login api_login = db.api_login.Find(id);
            if (api_login == null)
            {
                
                Response.Write("<script>alert('Your alert box')</script>");
            }

            return api_login;
        }
}
}

Many thanks for your response and help.
Posted
Updated 28-Jan-14 2:52am
v3

1 solution

You can write a simple message box by using the following line of code:

C#
Response.Write("<script>alert('Your alert box')</script>");


You can write any kind of HTML code back to the browser in this way, to put a link in the popup (which is not possible with the bare JavaScript Message Box) you would need to write actual HTML code instead of
Quote:
"<script>alert('Your alert box')</script>"


Another side note:

The above code can only be used from an *aspx.cs file ("default.aspx.cs", in case the site is called "default.aspx"). Otherwise you would need to do something like:

//default.aspx.cs:

C#
public partial class Default : System.Web.UI.Page
{
   UserController controller = new Controller();
   protected void Page_Load(object sender, EventArgs e)
   {
      if(controller.Getapi_login("YourId") == null)
      {
         Response.Write("<script>alert('Your alert box')</script>");
      }
   }
}
 
Share this answer
 
v2
Comments
miss786 28-Jan-14 8:01am    
Thank you so much for your response and help. I added the following line code and I am getting the "The name 'Response' does not exist in the current context" error.

public api_login Getapi_login(string id)
{
api_login api_login = db.api_login.Find(id);
if (api_login == null)
{
//throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
Response.Write("<script>alert('Your alert box')</script>");
}

return api_login;
}
Marco Bertschi 28-Jan-14 8:04am    
Is this method placed in the code behind of a web page?
e.g. a file with the ending *.aspx.cs ?
miss786 28-Jan-14 8:16am    
Thank you for your response. I have updated my code above for further reference. The code is written in API controller class. Many thanks for your help and time.
Marco Bertschi 28-Jan-14 8:22am    
The problem is that "Response" is only available in the code behind file of a web application: e.g. you would need to call Response.Write("<script>alert('Your alert box')</script>"); from a *.aspx.cs file, otherwise it is not going to work.
Marco Bertschi 28-Jan-14 8:29am    
I have updated my example, it should work now.
As said before, "Response" is only accessible from a page's code-behind.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900