Share Blog

Saturday, June 30, 2018

Disable Browser Back Button after LogOut in ASP.Net using JavaScript


How to disable Back button in Browser after Logout using JavaScript.

Browser Back button cannot be disabled and hence in order to prevent User navigating to previous page, the User is redirected back to the Current page forcefully using JavaScript.

Disable Browser Back Button Script
The following JavaScript code snippet must be placed in the HEAD section of the Page where the User must be prevented from going back.
<script type = "text/javascript" >
   function preventBack(){window.history.forward();}
    setTimeout("preventBack()", 0);
    window.onunload=function(){null};
</script>

Home Page

The HTML Markup of Home page consists of an HTML Anchor link to the Logout page.
The Disable Browser Back Button Script is placed in the HEAD section so that User cannot access the Home page using Browser Back button from Logout page.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Home</title>
<script type="text/javascript">
function preventBack() { window.history.forward(); }
setTimeout("preventBack()", 0);
window.onunload = function () { null };
</script>
</head>
<body>
<h3>Home</h3>
<hr />
<a href="Logout.aspx">Logout</a>
</body>
</html>


Logout Page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="logout.aspx.cs" Inherits="Test" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Logout</title>
</head>
<body>
<h3>Logout</h3>
</body>
</html>



Wednesday, June 20, 2018

Remove Last Character from String in C#


Here I will explain how to remove last character from string in C#,with example or delete or remove last character in string with examples in C#.We can easily remove last character from string by using Remove or Trim or IndexOf properties.

Method 1

Following is the one way of removing last character from string using Remove property

string inputstring = "1,2,3,4,5,6,7,8,9,10,11,";

string outputstring = inputstring.Remove(inputstring.Length - 1, 1);

Console.Write(outputstring);

Console.ReadLine();


Method 2

Following is the another way of removing last character from string using Trim property


string inputstring = "1,2,3,4,5,6,7,8,9,10,11,";

string outputstring = inputstring.Remove(inputstring.LastIndexOf(","));

Console.Write(outputstring);

Console.ReadLine();


Method 3

Following is the another way of removing the last character from string using IndexOf property but that character should exists only one time otherwise this property will return the values before the first character.


string inputstring = "1,2,3,4,5,6,7,8,9,10,11,";

string outputstring = inputstring.Trim(",".ToCharArray());

Console.Write(outputstring);

Console.ReadLine();


How to extract data from string in C#

I getting data in my application through Code. Code returns data in several parameters/variables. One of the parameter return below string.



string input = @"Some text BLA/123/5345/349230498 some more text PNR: 12345678, Name: John, CallName: Peter, TimeStamp: 01.10.2015";
string value = String.Empty;

List<string> keyValuePairs = input.Split(',').ToList();

foreach (var keyValuePair in keyValuePairs)
{
string key = keyValuePair.Split(':')[0].Trim();
if (key == "Name")
{
value = keyValuePair.Split(':')[1];
}
}

Disable Cut, Copy and Paste in TextBox using AngularJS in ASP.Net

How to Disable Cut, Copy and Paste in TextBox or TextArea using AngularJS in ASP.Net.

Cut, Copy and Paste operations in TextBox or TextArea can be performed using CTRL button or using Mouse Right Click.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestJavaScript.aspx.cs" Inherits="TestJavaScript" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Disable Cut, Copy and Paste in TextBox using AngularJS</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module('MyApp', [])
app.controller('MyController', function ($scope) {
$scope.AttachEvent = function (control, eventName) {
if (control.addEventListener) {
control.addEventListener(eventName, function (e) { e.preventDefault(); }, false);
} else if (control.attachEvent) {
control.attachEvent('on' + eventName, function () { return false; });
}
};
var controls = document.getElementsByTagName("*");
var regEx = new RegExp("(^| )disable( |$)");
for (var i = 0; i < controls.length; i++) {
if (regEx.test(controls[i].className)) {
$scope.AttachEvent(controls[i], "copy");
$scope.AttachEvent(controls[i], "paste");
$scope.AttachEvent(controls[i], "cut");
}
}
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div ng-app="MyApp" ng-controller="MyController">
Enter Text <asp:TextBox ID="txtbox1" runat="server" CssClass="disable"></asp:TextBox><br />
<br />
Paste Text <asp:TextBox ID="txtbox2" runat="server" CssClass="disable" TextMode="MultiLine" Rows="4" Columns="20"></asp:TextBox>
</div>
</form>
</body>
</html>


Friday, June 08, 2018

Bind (Populate) AutoComplete ComboBox from Database in Windows Forms using C#

How to bind (populate) Bind (Populate) AutoComplete ComboBox from Database in Windows Forms application using C#.



The population of an AutoComplete ComboBox is done in same way as the normal ComboBox, just the AutoCompleteMode needs to be enabled.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
namespace Face_rekognitaion
{
public partial class AutoComplateComboBox : Form
{
public AutoComplateComboBox()
{
InitializeComponent();
}

private void AutoComplateComboBox_Load(object sender, EventArgs e)
{
string constr = @"Data Source=.;Initial Catalog=app_abc;Integrated Security=True";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter("SELECT empid, name FROM tab_emp", con))
{
//Fill the DataTable with records from Table.
DataTable dt = new DataTable();
sda.Fill(dt);

//Insert the Default Item to DataTable.
DataRow row = dt.NewRow();
row[0] = 0;
row[1] = "";
dt.Rows.InsertAt(row, 0);

//Assign DataTable as DataSource.
cmboxname.DataSource = dt;
cmboxname.DisplayMember = "name";
cmboxname.ValueMember = "empid";

//Set AutoCompleteMode.
cmboxname.AutoCompleteMode = AutoCompleteMode.Suggest;
cmboxname.AutoCompleteSource = AutoCompleteSource.ListItems;
}
}
}

private void btnSubmit_Click(object sender, EventArgs e)
{
string message = "Employee Name: " + cmboxname.Text;
message += Environment.NewLine;
message += "EmployeeID: " + cmboxname.SelectedValue;
MessageBox.Show(message);
}
}
}