Monday, 10 June 2013

ASP.NET Error: The type "xxxx...." user control exists in both folder

Just change the web.config file as followed:
<compilation debug="false" targetFramework="4.0" batch="false" />

Add the batch="false" attribute to the "compilation" element of the web.config file.
This problem occurs because of the way in which ASP.NET 2.0 uses the application references and the folder structure of the application to compile the application. If the batch property of the element in the web.config file for the application is set to true, ASP.NET 2.0 compiles each folder in the application into a separate assembly.

Friday, 7 June 2013

jQuery: Disable right click on my web page?

Disabling the right click on web page

Introduction
Want to protect your source code? Using this code will prevent the vast majority of users from right-clicking over a page and choosing "View source", or right-clicking over an image and saving it.
Some times there might be the requirement to disable the pop up menu on click of right button.

Programmers may need to disable right click to prevent user from saving the images on the page or viewing the source of the page.

Though disabling right click is not complete solution to save the data, but it will make task difficult, or may be impossible for the rookies.

I searched on a net for the solution to this problem and i got following solution.

Method 1

      In this method we add a javascript method, in this we check if click is right click or left click if it is right click then a message is displayed like "Right click disabled"

<script language="javascript">
document.onmousedown=disableclick;
status="Right Click Disabled";
Function disableclick(e)
{
  if(event.button==2)
   {
     alert(status);
     return false; 
   }
}
</script>
 
But in this solution if you click the right click it displays the message
“Right click disabled”.

If we want to remove the messegebox then this solution do not work.

  I am having another simple solution to achieve the same result and also it does not show the messegebox.

Method 2

          In this method we set the oncontextmenu="return false" in the body part of the page
so body part will look like
<body oncontextmenu="return false">
...
</body>
So whenever user clicks the right mouse button nothing will happen, no message no context menu.

 Sometimes there might be requirement to disable the right click on specific control to achive this we have to follow these steps.
1.      remove added code in body (i.e. oncontextmenu=”return false”)
2.      Add one table and in its row add the control e.g. datagrid control
3.      put the oncontextmenu=”return false” in the <tr> for that control

So code will look like this
<Body>
  <Table>
   <tr oncontextmenu="return false">
    <td>
     <asp:datagrid id="dgGrid1">---</asp:datagrid>
   </td>
  </tr>
 </Table>
</Body>

So by this method context menu on the right click of right mouse click can be disabled.



How to add a custom right-click menu to a webpage?

use contextmenu event, like below:
<html>
<head>
<script type="text/javascript">
    if (document.addEventListener) {
        document.addEventListener('contextmenu', function(e) {
            alert("You've tried to open context menu"); //here you draw your own menu
            e.preventDefault();
        }, false);
    } else {
        document.attachEvent('oncontextmenu', function() {
            alert("You've tried to open context menu");
            window.event.returnValue = false;
        });
    }
</script>
</head>
<body>
Lorem ipsum...
</body>
</html>
But you should ask yourself, do you really want to overwrite default right-click behavior - it depends on application that you're developing.

Wednesday, 5 June 2013

CSS: Centered ul li a Horizontal Menu Bar

CSS UL LI - Horizontal CSS Menu

31.10.2008 Category: Web Design
In this tutorial we're going to create a professional horizontal CSS menu. First we are going to create a HTML list by using Unordered List (ul) and List Item (li) elements. Then we are going to style the list with CSS (Cascading Style Sheets) into the form of a horizontal navigation menu like in Picture 1.
Horizontal CSS navigation menu
Picture 1. Horizontal navigation menu that is made with HTML UL and LI elements and styled with CSS.
Previous knowledge about some basic HTML and CSS is required. HTML elements used in this tutorial:
  • ul (Unordered List)
  • li (List Item)
  • a (Anchor / Link)
This is a CSS tutorial so I'm not going to go through the creation of the graphics used in this tutorial. However, you can download the images and even the original PSD file:

HTML List (ul li) With Links

Let's start by creating a list with links in HTML:
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Products</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
ul tags define the whole list and each list element is defined with li tags. Additionally each list item is surrounded with link tag (a). In real life situation you will of course replace # with the real URL. In a browser the list looks like inpicture 2. My list has only five items but you can have as many as you like. Now the HTML part of the menu is complete. Next we're going to style the menu with CSS.
Vertical HTML menu
Picture 2. A basic HTML list created with UL and LI tags.

UL CSS Styling

First I change the background color to black. It's not required but my graphics just looks better on black;)
body {
background-color: #000;
}
Then I use universal selector (*) to remove browser's default values of padding and margin from all elements. I think it just makes life easier. Alternatively, you could place these declarations under the ul selector.
* {
margin: 0;
padding: 0;
}
Now we are going style the ul with CSS. CSS declaration block for ul:
ul {
list-style-type: none;
background-image: url(navi_bg.png);
height: 80px;
width: 663px;
margin: auto;
}
The first declaration removes the default HTML list bullets. The second declaration places small (1x80 pixels) image in the background (repeated automatically.) The third declaration sets the height of the list. The fourth declaration sets the width of the list. The fifth declaration is voluntary. Auto margin will center the list within its parent element. Now the list looks like in picture 3.
Background image placed with css in ul li list
Picture 3. Now the horizontal menu has a background but the list items are still vertically.

LI CSS Styling

Next the menu is made horizontal by floating li elements with CSS:
li {
float: left;
}
Now the list starts to resemble a horizontal menu. At the moment the menu looks like in picture 4.
HTML list menu in horizontal form
Picture 4. Work in progress CSS navigation menu.

Styling Links With CSS

Links are styled by making the following declarations to the descendant link (a) selector:
ul a {
background-image: url(navi_bg_divider.png);
background-repeat: no-repeat;
background-position: right;
padding-right: 32px;
padding-left: 32px;
display: block;
line-height: 80px;
text-decoration: none;
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 21px;
color: #371C1C;
}
The first declaration places a small (2x80 pixels) background image to each link. The background image works as a divider between the links (picture 5). The second declaration forbids the repeating of the background image. The third declaration defines the location of the background image. The fourth and fifth declaration create padding around each link. The sixth declaration makes the whole block clickable (instead of just the text) The seventh declaration sets the height of the link. This is needed so that the whole height of the block would be clickable. This also centers the text vertically. The eighth declaration removes the underlining from the links. The 9th - 11th declarations are just for styling the text. Now the menu looks like in picture 6.
Background images for horizontal list menu
Picture 5. Background images used in this horizontal navigation menu. You can download them here and here.
Horizontal CSS Navigation menu
Picture 6. Now the CSS styled ul li list looks like a real navigation menu.

Finalizing the Horizontal CSS Menu

The menu is almost complete but if you look closely you'll see one problem with the menu. There is unnecessary divider after the last link. It can be removed by adding style declaration inside of the last link tag. So the final HTML code looks like:
<ul>
<li> <a href="#">Home</a></li>
<li><a href="#">Products</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">About</a></li> 
<li><a style="background-image: none;" href="#">Contact</a></li>
</ul>
Last thing to do is a small visual enhancement. The following CSS declaration block creates mouse-over color change to the links:
ul a:hover {
color: #FFF;
}
In picture 7 you see the final horizontal CSS menu. The menu is tested with Internet Explorer 7, Firefox 3 and Google Chrome 0.2.149.30.
Horizontal CSS navigation menu made out of list (ul li)
Picture 7. The complete horizontal menu made with HTML list (ul li) and styled with CSS.

Tuesday, 4 June 2013

ASP.NET: How to: Prepare to Deploy a Web Project

How to: Prepare to Deploy a Web Project

Before you deploy or redeploy a Web project, you might want to prevent the production site from responding to page requests during the deployment process. This can help avoid errors that might result during the time when the changes are only partially completed. Also, when changes occur to some file types and folders, the application domain restarts, and you might want to make sure that this does not happen multiple times.

To take a Web application offline before deployment

  1. Create a file called App_offline.htm and include a message in the file that lets users know that the site is unavailable because you are updating the site.
  2. Place the App_offline.htm file in the root folder of the target Web site.
    While the App_offline.htm file exists in the root of your Web site, any request to the Web site will redirect to that file.
    When you have finished deploying the site, remove the App_offline.htm file.

Basic Deployment Tasks 
Here are the basic tasks that you have to do in order to deploy to a third-party hosting company:
  1. Set up an account with a hosting company.
    For information about hosting providers that support automated deployment from Visual Studio, see ASP.NET Hosting. Make sure that you choose a provider that supports the version of ASP.NET that your web project targets.
  2. Create a publish profile.
    A publish profile specifies the server you are deploying to, credentials for logging on to the server, databases to deploy, and other options. The hosting company sends you the settings you need for the publish profile, either in an email or in a file that you can import into Visual Studio.
  3. Start the deployment process by clicking the one-click publish button. The Publish button is in the Publish Web wizard that you use to create publish profiles. It is also available in the Web One Click Publish toolbar that you can choose from the ViewToolbars menu in Visual Studio.

How to make an Website Offline:

  1. Create a file called App_Offline.htm
  2. Insert a offline message you want to display while the website is offline
  3. Upload that file into the webhost root folder
  4. If the App_Offline.htm file is present website automatically redirect it to this file
  5. To make the Website online again just remove the App_offline.htm file.

Taking an Application Offline before Publishing


In Web Deploy V3, we added support to automatically take an ASP.Net application offline before publishing to it. This is useful if a user wants to ensure that their application does not have a lock on a file (e.g. SQL CE sdf files which only allow one connection to the file at a time) being overwritten, or if they want to ensure that visitors to their site cannot affect the publish process. The way it works is if Web Deploy detects that a change needs to be made on the destination server, it will automatically drop a blank App_Offline.htm file into the root of the application folder. When ASP.Net detects that a file by the name of "App_Offline.htm" exists, it will automatically bring down the app domain hosting the application. When the publish process is completed, the App_Offline.htm file will be removed and the site will be online again.


  • Enabling Application Offline

Web Deploy takes an application offline by using the "AppOffline" rule, which is off by default. If a client wishes to turn it on during a publish operation, it simply needs to enable the rule. Here is an example of how to do this from the command line (executable lives under "%programfiles%\IIS\Microsoft Web Deploy V3"):
msdeploy.exe -verb:sync -source:iisApp=mySite -dest:auto,computername=<publishUrl> -enableRule:AppOffline 

  • What happens when a publish fails

If an error occurred during publishing, the htm file will remain in the site root to keep it offline because it is now in an unknown state. After fixing your publishing issue, you will need to bring the application back online. To do this, you can either manually delete the App_Offline.htm file from your application root on the server, or republish your site. Here is an example of how to delete the App_Offline.htm file from the command line:
msdeploy.exe -verb:delete -dest:contentPath=siteName/App_Offline.htm,computername=<publishUrl> 

Precompiling for Deployment Only

When you precompile for deployment only, the compiler produces assemblies from virtually all ASP.NET source files that are normally compiled at run time. This includes program code in pages, .cs and .vb class files, other code files, and resource files. The compiler removes all source and markup from the output. In the resulting layout, compiled files are generated for each of the .aspx files (with the extension .compiled) that contain pointers to the appropriate assembly for that page.
To change the Web site, including the layout of pages, you must change the original files, recompile the site, and redeploy the layout. The only exception is the site configuration; you may make changes to the Web.config file on the production server without having to recompile the site.
This option provides the greatest degree of protection for your pages and the best performance at startup.

How to Create an Offline Web Page

Web designers commonly have a need to build pages when offline in order to continue work when an Internet connection is not available. By working on a website offline, you can test new concepts prior to going live on the Internet, thereby avoiding any loss of web traffic. Additionally, an offline website provides a full backup of your website in the event of catastrophic failure with the copy saved on your web server. Have a question? Get an answer from Online Tech Support now!

Instructions

    • 1
      Create a directory structure on your computer that is identical to your web server. To do this, create a folder on your computer named "MyWebsite". Then, open the "MyWebsite" folder and create the first level of sub-folders that exist on your website, such as "mycontent" and "images". To create a new folder, right-click in the white space of the folder, left-click "New Folder" and type the name of the website in the text field.
    • 2
      Save copies of all of the images on your website to the "Images" folder located under the "MyWebsite" folder created in step 1.
    • 3
      Change the hyperlinks of the images in your web pages to be directed to the local folder. For example, where your previous image link URLs would be similar to: "http://www.mywebsite.com/images/myimage.jpg", the new URL's will be of the form: "images/myimage.jpg".
    • 4
      View the offline website in your web browser by using your browser's file chooser to open the "Index.html" page, or manually enter the following URL convention in your browser's toolbar:
      "file:///c:/MyWebsite/Index.html".



Publishing a Visual Studio Project (Not Website):

To access this tab on the Properties page

  1. In Visual Studio, open a web application project.
  2. In Solution Explorer, open the shortcut menu and choose Properties (C#) or MyProject (Visual Basic) to display the Properties page.
Choose the Package/Publish Web tab.
Package/Publish dialog box


Note: 
  1. Create a Web Deployment Package in Visual Studio