Sometimes we need to store a lot of confidential data in web.config in our production environment (for examples: username/password for impersonation or for connect to database, some appSettings, etc.). And in fact there is a very important information in web.config file and it is our connection string that contains our server name, database name, user id and password. And it is not secure to store that as clear text, obviously some people on your server may have access to this file and steal your data. So we must store them in encrypted form. So how to encrypt the data in web.config file? And how to decrypt the same?
Monday, June 30, 2014
Monday, June 9, 2014
Check all check box on the click of a single check box.
Following is the code to select all the check boxes on the click of a single check box.
checkboxes is the name given to all the check boxes that need to be checked on the click of a single check box.
checkAll is the ID of the single check box on the click of which all check boxes will get checked/unchecked.
$("input[name='checkboxes']").click(function () {
if ($("input[name='checkboxes']").length == $("input[name='checkboxes']:checked").length) {
$("#checkAll").attr("checked", "checked");
}
else {
$("#checkAll").removeAttr("checked");
}
});
$("#checkAll").click(function () {
$("input[name='checkboxes']").attr("checked", this.checked);
});
checkboxes is the name given to all the check boxes that need to be checked on the click of a single check box.
checkAll is the ID of the single check box on the click of which all check boxes will get checked/unchecked.
Friday, May 23, 2014
Server side ajax paging using jquery MVC4
Server Side AJAX paging using jquery in MVC4
Every developer has to code for a view that displays all the records. But what if the database consists of thousands of records? In that case displaying all the records on the view is not good. Rather some sort of paging must be provided that will give the user the ability to navigate among the records and even provide the developer to have fast web page. So, we need to fetch the records on demand and display them on the view. This will be much fast if records are fetched using AJAX. Pagenation can be done in different ways. One way is to use Html Helpers. i.e., @Html.PagedList. The other way is using AJAX. So, how AJAX is used for fast paging?
Thursday, May 15, 2014
Store Password in Encrypted form
Store Password in Encrypted Form
Now a days security is a big issue for any organisation or an individual. Whether you do surfing on internet or use your ATM. Everywhere we can find in some or the other way to secure our data or transaction or just to be on the safe side.
Software Engineers or Developers try hard to provide security for the applications they built. One common way to provide security is to provide Login credentials for their applications. To improve it further, 3-attempts login feature is provided. After 3 unsuccessful attempt, the applications gets blocked.
There are lot of other ways to provide security.
Now, what is more important is to secure your data. i.e., What if somebody peeps into your database? The person will get to know users credentials of your system or application.
One way to prevent this is to store password in encrypted form.
So let us learn how to store password in encrypted form using a reliable MD5 algorithm.
Wednesday, April 30, 2014
How to trigger button click in MVC4?
How to trigger button click in MVC4?
This post will let you know to handle the click event of button when Enter Key is hit by the user.
A function
This
In the example code below, we have assumed the id of the form as 'loginForm'. You can replace it with your own form Id.
This post will let you know to handle the click event of button when Enter Key is hit by the user.
A function
keypressHandler() is created in which we need to check whether the key hit by the user is Enter Key or not. If it is, then get the focus on the login button and trigger the click event.This
keypressHandler() function should be called from the form whenever any key is pressed.In the example code below, we have assumed the id of the form as 'loginForm'. You can replace it with your own form Id.
function keypressHandler(e){
if (e.which == 13) {
$(this).blur();
$('#Loginbutton').focus().click(); //give your submit button an ID
}
}
$('#loginForm').keypress(keypressHandler); //give your form an ID
How to prevent the functionality of browser back button?
Prevent the functionality of browser back button
In this post I will teach you how to prevent the functionality of browser back button.
Since, we have to prevent the back functionality of the browser, this can't be handled from the code-behind. This is the client functionality. So we need to so something at the client side.
So, we simply have to use javascript history object which is a part of the window object.
history object has 3 methods and 1 property.
history object methods:
In this post I will teach you how to prevent the functionality of browser back button.
Since, we have to prevent the back functionality of the browser, this can't be handled from the code-behind. This is the client functionality. So we need to so something at the client side.
So, we simply have to use javascript history object which is a part of the window object.
history object has 3 methods and 1 property.
history object methods:
- forward()- loads the next URL in the history list. It won't work if the next page does not exist in the history list.
- back()- loads the previous URL in the history list. It won't work if the previous page does not exist in the history list.
- go()- loads the specific URL from the history list. It accepts either int parameter or string parameter.
- length- returns the number of URL in the history list.
/*This is the function*/
function disableBackButton() {
window.history.forward()
}
/* call the function */
disableBackButton();
/* when the page loads again call the function */
window.onload=disableBackButton();
/* if the page is persisted than disable the back functionality */
window.onpageshow = function (evt) {
if (evt.persisted) disableBackButton()
}
/* do nothing on unload */
window.onunload = function () { void (0) } NOTE: Place this code in the <script type=text/javascript></script> tag. The code should be placed on the page where the user see Log Out button.
Monday, April 21, 2014
Writing an Image in Excel File
Writing an Image in Excel File
This post will explain you how to write an image in excel file. You first need to define the path of the image in web.config file. Just write the code in web.config file as
<add key="LogoPath" value="E:\\MyProject.Web\\Content\\Images\\ProjectLogo.jpg"/>
Now draw this logo into your excel file using System.Drawing.Image class. An abstract base class that provides functionality for the Bitmap and Metafile descended classes. We will use FromFile() method that is used to create image from the specified file. The file path is passed as an argument to FromFile() method. We can use its overloaded form where we need to specify another argument as boolean i.e., true or false. This specifies whether to use embedded color management information in that file.
Once this Image is created, now create final imagepath that will be rendered in the excel. Here we need to create an image tag(<IMG="" SRC="" WIDTH="" HEIGHT="" />). In this we need to specify the image's width and height by using the above created Image.
Now our iamgepath is ready for finally rendering it into excel file. The code is as follows:
NOTE: This code has a limitation. You need to specify the path every time whenever the image location is changed. The better solution is to use a mapping between ServerPath and LocalPhysicalPath.
HttpContext.Response.Write("<TR style='font-size:12.0pt; text-align:center; font-style:italic; text-decoration:underline; font-weight:bold; font-family:Book Antiqua;'>");
string filePath = System.Configuration.ConfigurationManager.AppSettings["LogoPath"];
System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(filePath);
string imagepath = string.Format("<img src='{0}' width='{1}' height='{2}'/>", filePath, imgPhoto.Width, imgPhoto.Height);
imgPhoto.Dispose();
HttpContext.Response.Write("<Td>");
HttpContext.Response.Write(imagepath);
HttpContext.Response.Write("</Td>");
HttpContext.Response.Write("</TR>");
Tuesday, April 8, 2014
Pagination in MVC4
Pagination in MVC4
This post will explain pagination in MVC4.
You need to install NuGet Package manager named PagedList. You can get the same by clicking NuGet PagedList.
Once this is installed in your project, add a reference to PagedList and PagedList.Mvc dlls. In your view, include the following code at the top as:
The model which your view will be returning should be IPagedList<> rather than IEnumerable<>.
Now you are ready to use the Html Helper for the PagedList. i.e., @Html.PagedListPager() where you can also specify various display options for the pagination.
The view will look like this:
The various display options for the PagedList can be found in PagedList.Mvc namespace
This post will explain pagination in MVC4.
You need to install NuGet Package manager named PagedList. You can get the same by clicking NuGet PagedList.
Once this is installed in your project, add a reference to PagedList and PagedList.Mvc dlls. In your view, include the following code at the top as:
@using PagedList;
@using PagedList.Mvc;The model which your view will be returning should be IPagedList<> rather than IEnumerable<>.
Now you are ready to use the Html Helper for the PagedList. i.e., @Html.PagedListPager() where you can also specify various display options for the pagination.
The view will look like this:
@using PagedList;
@using PagedList.Mvc;
@model IPagedList<Distribution.Entities.District>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<link href="../../Content/PagedList.css" rel="stylesheet" type="text/css" />
<h2>Display</h2>
@Html.ActionLink("Create New", "Create")
The various display options for the PagedList can be found in PagedList.Mvc namespace
inside the class PagedListRenderOptions.
You can make any changes as per the requirement.
The Complete View is as:
View
Now that your view is ready, you will have to write code for your controller for the pagination. This is as under:
Controller
You can make any changes as per the requirement.
public PagedListRenderOptions()
{
DisplayLinkToFirstPage = PagedListDisplayMode.IfNeeded;
DisplayLinkToLastPage = PagedListDisplayMode.IfNeeded;
DisplayLinkToPreviousPage = PagedListDisplayMode.IfNeeded;
DisplayLinkToNextPage = PagedListDisplayMode.IfNeeded;
DisplayLinkToIndividualPages = true;
DisplayPageCountAndCurrentLocation = false;
MaximumPageNumbersToDisplay = 10;
DisplayEllipsesWhenNotShowingAllPageNumbers = true;
EllipsesFormat = "…";
LinkToFirstPageFormat = "««";
LinkToPreviousPageFormat = "«";
LinkToIndividualPageFormat = "{0}";
LinkToNextPageFormat = "»";
LinkToLastPageFormat = "»»";
PageCountAndCurrentLocationFormat = "Page {0} of {1}.";
ItemSliceAndTotalFormat = "Showing items {0} through {1} of {2}.";
FunctionToDisplayEachPageNumber = null;
ClassToApplyToFirstListItemInPager = null;
ClassToApplyToLastListItemInPager = null;
ContainerDivClasses = new [] { "pagination-container" };
UlElementClasses = new[] { "pagination" };
LiElementClasses = Enumerable.Empty();
}
The Complete View is as:
View
@using PagedList;
@using PagedList.Mvc;
@model IPagedList<distribution .entities.district="">
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<link href="../../Content/PagedList.css" rel="stylesheet"
type="text/css" />
<h2>Display</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.First()
.State.StateName)
</th>
<th>
@Html.DisplayNameFor(model => model.First()
.DistrictName)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.State.StateName)
</td>
<td>
@Html.DisplayFor(modelItem => item.DistrictName)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new
{ id=item.DistrictId }) |
@Html.ActionLink("Details", "Details", new
{ id=item.DistrictId }) |
@Html.ActionLink("Delete", "Delete",
new { id=item.DistrictId })
</td>
</tr>
}
</table>
@Html.PagedListPager(Model,page=>Url.Action("Index",new {page}),
new PagedListRenderOptions
{Display=PagedListDisplayMode.IfNeeded,
DisplayPageCountAndCurrentLocation=true})
Now that your view is ready, you will have to write code for your controller for the pagination. This is as under:
Controller
public ActionResult Index(int? page)
{
return View(db.Districts.ToList().ToPagedList(page ?? 1,4));
}
Monday, April 7, 2014
Thursday, April 3, 2014
How to find if radio button is checked or not?
This post will explain how to get the checked item from radio button. It will determine if radio button is checked or not.
This is done using jquery as follows:
This is done using jquery as follows:
$('#RadioButtonId').is(':checked')
Subscribe to:
Posts (Atom)

