Tuesday, April 12, 2011

Restrict special characters from being entered into the textbox using Javascript

If the input from the textbox is going to be used as a filter criteria to retrieve records from the database, your application will throw exceptions as sql does not like special characters like '%', ', " in 'Where' and 'Like' clauses. You could write some javascript to restrict users from entering these special characters

function check(e) {
var keynum
var keychar
var numcheck
// For Internet Explorer
if (window.event) {
keynum = e.keyCode
}
// For Netscape/Firefox/Opera
else if (e.which) {
keynum = e.which
}
keychar = String.fromCharCode(keynum)
//List of special characters you want to restrict
if (keychar == "'" || keychar == "`" || keychar == "%" || keychar == "\"" {
return false;
}
else {
return true;
}
}

Here is the asp.net tag

<asp:TextBox ID="txtName" runat="server" onkeypress="return check(event)"></asp:TextBox>

Friday, April 8, 2011

How to pass an array to the stored procedure

If you have a situation where you have to pass multiple selected values from the listbox to a stored procedure and these multiple selected values should be used in a filter criteria "IN", you could do it using dynamic sql. Concatenate the list of selected values with comas. If the search criteria are strings, concatenate quotes to the strings and pass this string to the storedprocedure with dynamic sql in it.

string _selList = "";
int[] indSel = DDL.GetSelectedIndices();
foreach (int i in indSel)
{
_selList += "'" + DDLMfgSite.Items[i].Text + "',";
}
if (_selList.LastIndexOf(",") > 0)
_selList = _selList.Substring(0, _selList.LastIndexOf(","));

Populating a Texbox with ListBox multiple selected items using Javascript

function ddlChange() {
var ddl = document.getElementById('<%=DDL.ClientID%>');
var textBox = document.getElementById('<%=txtSelected.ClientID%>');
len = ddl.length;
textBox.value = "";
for (var j = 0; j < len; j++) {
if (ddl.options[j].selected) {
textBox.value += ddl.options[j].text + "\r\n";
}
}
}


<div>Available Items:</div>
<div>
<asp:ListBox ID="DDL" runat="server" SelectionMode="Multiple" onChange="ddlChange()" Height="170px">
</asp:ListBox>
</div>


<div>Selected Items:</div>
<div>
<asp:TextBox ID="txtSelected" runat="server" TextMode="MultiLine"
ReadOnly="true" Height="170px" Width="365px"></asp:TextBox>
</div>

Thursday, April 7, 2011

Setting Textbox to the dropdownlist's selected item using javascript

<script type="text/javascript"/>
function ddlChange() {
var ddl = document.getElementById('<%=DDL.ClientID%>');
var textBox = document.getElementById('<%=txtSelected.ClientID%>');
textBox.value = ddl.options[ddl.selectedIndex].text;
}
</script>

<asp:DropDownList ID="DDL" runat="server" DataTextField="Site" DataValueField="Site" onChange="ddlChange()">
</asp:DropDownList>
<asp:TextBox ID="txtSelected" runat="server" Width="300px"></asp:TextBox>

Setting ListBox scroll position at the Selected Items

If you have a listbox inside your user control and if all of these conditions below apply:
1. Your listbox is inside a user control
2. The user control becomes visible based on other selections on the webform you made.
3. The listbox is bound to the data in the Page_Init and the default SelectedValue is set in the code behind.

In this scenario, when the user control becomes visible, the listbox scroll position is set at the first item in the listbox instead of at the SelectedValue.

If you want the listbox scroll position at the SelectedValue, you have to write javascript.

Write this javascript in your user control:

window.onload = function () {
var ddl = document.getElementById('<%=DDL.ClientID%>');
len = ddl.length;
for (var j = 0; j < len; j++) {
if (ddl.options[j].selected) {
ddl.options[j].selected = false;
ddl.options[j].selected = true;
}
}
}

What you do here is just unselect and reselect the selected items.

Monday, March 28, 2011

Static vs Const vs Readonly

Static variables are stored in a special area inside Heap known as High Frequency Heap. Those methods and variables which don't need an instance of a class to be created are defined as being static. Static methods can only call other static methods, or access static properties and fields. Static classes can not inherit from any class and can not be inherited.

Static variables keep their values for the duration of the application domain.

It will survive many browser sessions until you restart the web server (IIS) or until it restarts on its own (when it decides it needs to refresh its used resources).


There is a subtle but important distinction between const and readonly keywords in C#:

Use Const when the value of the variable will stay fixed. const variables are implicitly static and they need to be defined when declared.

Use readonly when the value of the variable changes from user to user but remains constant once initialized at runtime. readonly variables are not implicitly static and can only be initialized once. readonly member can be initialized at runtime, in a constructor as well being able to be initialized as they are declared.

For example:

public class MyClass
{
public readonly double PI = 3.14159;
}

or

public class MyClass
{
public readonly double PI;

public MyClass()
{
PI = 3.14159;
}
}


E.g.: You are writing a storage program in which the memory has a fixed size of 104857600 Bytes. You can define a const variable to denote this as:

private const int _memSize = 104857600 ;

Now, you want the user to enter the amount of memory he needs. Since this number would vary from user to user, but would be constant throughout his use, you need to make it readonly. You cannot make it a const as you need to initialize it at runtime. The code would be like:


public class Storage
{
//this is compile time constant
private const int _totalMemory = 104857600 ;
//this value would be determined at runtime, but will
//not change after that till the class's
//instance is removed from memory
private readonly int _memSize ;

public Storage(int memSize)
{}

public AllocateMemory(int memSize)
{
///
///Get the number of cars from the value
///use has entered passed in this constructor
///

_memSize= memSize;
}
}

Friday, March 25, 2011