Convert date to ""dd/mm/yyyy" format C#

format datetime in dd/mm/yyyy

In c# we need to convert datetime in different formats.In this post we will see how to get datetime in "dd/mm/yyyy" format.To parse datetime in "dd/mm/yyyy" format we need to use string formatter "dd/mm/yyyy" in .ToString() constructor.

Code to get datetime in dd/mm/yyyy format [C#]

using System;
 public class StringToDateTime
 {
   public static void Main()
   {
     var myDateTimeValue = "2012-01-16 15:02:26";
     DateTime myDateTime = DateTime.Parse(myDateTimeValue);
     var dateingddmmyy = myDateTime.ToString("dd/mm/yyyy");
     Console.Write(dateingddmmyy);
   }
 }
 

Get dropdown selected item onchange in jQuery

get dropdown list selected item onchange in jquery
When using dropdown list in asp.net application, we need to get value of selected item on onchange event of dropdown. In this post we will see example of how to get value of selected item using onchange event of dropdown.

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<title>Get dropdown value</title>
<script type="text/javascript">
$(document).ready(function () {
$("#topics").change (function () {
var text = $("#topics option:selected").text();
var value = $("#topics option:selected").val();
alert("Selected text=" + text + " Selected value= " + value);
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="topics" runat="server">
<asp:ListItem Text="C#" Value="1"></asp:ListItem>
<asp:ListItem Text="jQuery" Value="2"></asp:ListItem>
<asp:ListItem Text="ASP.NET" Value="3"></asp:ListItem>
</asp:DropDownList>
</div>
</form>
</body>
</html>
Output

Get dropdownlist selected value in jQuery

get dropdownlist selected value in jquery
In web application we need to get dropdown value at client side. We will see the example of how to get dropdown value in jQuery.

Code to get dropdown value in jQuery


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"> type="text/javascript"> </script>
  <title>Get dropdown value</title>
  <script type="text/javascript">
    $(document).ready(function () {
      $("#go").click(function () {
        var text = $("#numbers option:selected").text();
        var value = $("#numbers option:selected").val();
        alert("Selected text=" + text + " Selected value= " + value);
      });
    });
  </script>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <asp:DropDownList ID="numbers" runat="server">
      <asp:ListItem Text="1" Value="1"></asp:ListItem>
      <asp:ListItem Text="2" Value="2"></asp:ListItem>
      <asp:ListItem Text="3" Value="3"></asp:ListItem>
    </asp:DropDownList>
  </div>
  <input type="button" id="go" value="Go" />
  </form>
</body>
</html>

Get radio button value in jQuery

Get radio button value in jquery
When using radio buttons in web application we need to get value of radio buttons at clientside. We can use jQuery to get radio button value in jQuery.

Code that shows how to get radio button value in jQuery

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<title>Get radio button value</title>
<script type="text/javascript">
$(document).ready(function () {
$("#submit").click(function () {
var number = $("input[@name=numbers]:checked").val();
alert(number);
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="radio" name="numbers" value="0" checked="checked" style="display:none" /> 
<input type="radio" name="numbers" value="1" /> 
<input type="radio" name="numbers" value="2" /> 
<input type="radio" name="numbers" value="3" /> 
<input id="submit" type="button" value="Submit"/>
</div>
</form>
</body>
</html>
Output

jQuery Tabs - UI

jquery tab
jQuery UI libaries provied good controls. jQuery UI contains tab control that is used to show tabed formatted data. We will see example how to show jquery tabs.
Before starting implementation we need to download jQuery libraries.http://jqueryui.com/download.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="development-bundle/jquery-1.7.1.js" type="text/javascript"></script>
<script src="js/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="js/jquery-ui-1.8.18.custom.min.js" type="text/javascript"></script>
<script src="development-bundle/ui/jquery.ui.widget.js" type="text/javascript"></script>
<script src="development-bundle/ui/jquery.ui.tabs.js" type="text/javascript"></script>

<link href="Styles/jquery-ui-1.8.18.custom.css" rel="stylesheet" type="text/css" />
<title></title>

<script type="text/javascript">
$(function () {
// Tabs
$('#tabs').tabs();
});

</script>
</head>
<body>
<form id="form1" runat="server">
<div id="tabs">

<ul>
<li><a href="#tabs-1">Tab1</a></li>
<li><a href="#tabs-2">Tab2</a></li>
<li><a href="#tabs-3">Tab3</a></li>
</ul>
<div id="tabs-1">Tab1 Contents.</div>
<div id="tabs-2">Tab2 Contents.</div>
<div id="tabs-3">Tab3 Contents.</div>

</div>
</form>
</body>
</html>

Show jQuery popup dialog

jquery dialog
In web applications we need to show popup dialog to show errors or dialog to show message or to show confirmation dailog with ok and cancel button. jQuery UI library provides easy way to show dialog as model or simple dailog. For that we need to add ui js libraries in application.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="development-bundle/jquery-1.7.1.js" type="text/javascript"></script>
    <script src="js/jquery-1.7.1.min.js" type="text/javascript"></script>
    <script src="js/jquery-ui-1.8.18.custom.min.js" type="text/javascript"></script>
    <script src="development-bundle/ui/jquery.ui.widget.js" type="text/javascript"></script>
    <script src="development-bundle/ui/jquery.ui.dialog.js" type="text/javascript"></script>
    <link href="Styles/jquery-ui-1.8.18.custom.css" rel="stylesheet" type="text/css" />
    <title>jQuery Popup</title>
    <script type="text/javascript">
        $(function () {
            // Dialog  
            $('#dialog').dialog({
                autoOpen: false,
                width: 600,
                buttons: {
                    "Ok": function () {
                        $(this).dialog("close");
                    },
                    "Cancel": function () {
                        $(this).dialog("close");
                    }
                }
            });
            // Dialog Link
            $('#btnDelete').click(function () {
                $('#dialog').dialog('open');
                return false;
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div id="dialog" title="Dialog Title">
        <p>
            Do you want to delete record?</p>
    </div>
    <input type="button" id="btnDelete" value="Delete" />
    </form>
</body>
</html>

Confirmationbox in javascript

show confirmationbox in javascript
In asp.net we need to show javascript confirmation box before doing some action.For example if we want to delete some record on the form, and we need users confirmation before deletion happens.

Sample code to show confirmation box in Javascript

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function Confirmation() {
if (confirm("Do you want to delete record?") == true)
return true;
else
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="Submit" onclick="return Confirmation();" />
</div>
</form>
</body>
</html>

Disable button jQuery

Disabling button after clicking on it is bit easy in jQuery.We can access button element of the page in jQuery and disable button using jquery. For that we can get button element using Id attribute selector or using class attribute selector.For example We may need to disable submit button after submitting form by clicking on it.

Sample code to disable submit button[jQuery + asp.net]

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<title></title>
<script type="text/javascript">
$(document).ready(function () {
});

function Disable(btn) {
$(btn).attr("disabled", "true");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="Submit" onclick="return Disable(this);" />
</div>
</form>
</body>
</html>

potentially dangerous Request error

In asp.net C# sometimes we get potentially dangerous Request error. potentially dangerous Request error comes when we try to pass special character's like <,>,:...etc. In order to solve the potentially dangerous Request error we need to set validateRequest="false" in page directive for perticular page like:

<%@ Page Language="C#" AutoEventWireup="true" <b>ValidateRequest="false"</b>%>


Or if we want to set it for whole web application page then we can set it in web.config in section like

<pages smartNavigation="false" <b>validateRequest="false"</b> enableSessionState="true"></pages>

If we are using newer version of .net framework then we need to set "requestValidationMode="2.0"" in "" in web.config like:
<httpRuntime maxRequestLength="10000" executionTimeout="3600" <b>requestValidationMode="2.0"</b>/>

Assign value using jQuery

assign values in jQuery
In asp.net applications we need to assign value in javascript or jQuery to the controls like label or hiddenfield. We will see sample code that assigns value to lable and hidden field using jQuery.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<title>assign value in jQuery</title>
<script type="text/javascript">
$(document).ready(function () {

});

function AssignValue() {
$("#message").html('New Value assigned');
$("#hiddenValue").val('New Value assigned');
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>
<label id="message">
Some Value</label>
<input type="submit" name="btnsubmit" id="btnsubmit" value="LogIn" onclick="return AssignValue();" />
<input type="hidden" id="hiddenValue" />
</p>
</div>
</form>
</body>
</html>

In above example if we want to assign value to hidden field and get hidden field value in code behind we need to remove "return false;" statement from jQuery code so that page will get postback.