Thursday, July 31, 2008

Multiple Select Grid Row

To select row of grid on click of any cell

Put one datagrid and checkbox on the form, check checkbox to enable selection using control.

Add mouseup eventhandler

this.dgSelect.MouseUp += new System.Windows.Forms.MouseEventHandler(this.dgSelect_MouseUp);

Add the below method of mouseup event

private void dgSelect_MouseUp(object sender, MouseEventArgs e)
{
if(chkCtrl.Checked)
{
if(System.Windows.Forms.Control.ModifierKeys == Keys.Control)
{
select();
}
else
{
for(int j = 0; j < dgRow ; j++)
dgSelect.UnSelect(j);
selectedRow.Clear();
dgSelect.Select(dgSelect.CurrentCell.RowNumber);
selectedRow.Add(dgSelect.CurrentCell.RowNumber);
}
}
else
{
if(!(System.Windows.Forms.Control.ModifierKeys == Keys.Control))
{
select();
}
else
{
for (int i = 0; i < selectedRow.Count; i++)
{
dgSelect.Select(int.Parse(selectedRow[i].ToString()));
}
}
}
}

Add below methods to select

private void select()
{
int c = dgSelect.CurrentRowIndex;
if(selectedRow.Contains(c))
{
dgSelect.UnSelect(c);
selectedRow.Remove(c);
}
else
{
dgSelect.Select(c);
selectedRow.Add(c);
}
for (int i = 0; i < selectedRow.Count; i++)
{
dgSelect.Select(int.Parse(selectedRow[i].ToString()));
}
}

check checkbox to enable selection using control, uncheck it to select without control

Happy Coding :)

Monday, July 21, 2008

IIS Compression

There are three ways of doing IIS compression:

Static files only
Dynamic application only
Both static files and dynamic application

Why compression is required?

Compression is required for faster transport of data, as amount of data transfered will be less and to get faster user experience in case of low bandwidth. So we can say compression is required for speed and bandwidth.

How compression works?

When a request reaches to IIS, it checks whether the browser from which it got the request has compression-enabled or not.Then, it checks for kind of compression is enabled, i.e. static files or dynamic application.

IIS caches the compressed static files only, if it is already cached, IIS sends the compressed file to the requested browser. If IIS doesn't find the compressed version, it sends an uncompressed version of the requested file and in background it compresses the requested file for subsequent use.

In case of dynamic application(e.g aspx or asp), IIS doesn't cache it. It compresses the response as request reaches and sends response to the client.

Evaluating HTTP Compression

If application consists lots of dynamic content, we need to check few things to reach to decision whether to implement HTTP Compression.

If % Processor Time counter is higher than 70%, enabling HTTP compression is not recommended. If your server has multiple processors, you need to check the individual processors % Processor time.

Follow below link to know how to IIS compression

Using HTTP Compression

Happy Coding :)

Saturday, July 19, 2008

GET DNS IPs

To get DNS IPs we need to import System.Net Namespace. It contains Dns.GetHostByName method, IPHostEntry and IPAddress class.

Create two text boxes with txtDomainName and txtIPs ID respectively and create one button with btnGetIps ID.

Put below code in btnGetIps event

try
{
string strIPs = string.Empty;
IPHostEntry ipHE = Dns.GetHostByName(txtDomainName.Text);
IPAddress[] ipAdd = ipHE.AddressList;
foreach (IPAddress ip in ipAdd)
{
strIPs += ip + ",";
}
txtIPs.Text = strIPs;
}
catch (System.Exception ex)
{
Response.Write(ex.Message);
}

Enter domain name in txtDomainName text box and click btnGetIps.
All the IPs of the domain entered in txtDomainName textbox will appear in txtIPs textbox.

Happy Coding :)

Friday, July 18, 2008

SQL Split Function

CREATE FUNCTION Split
(
@Word VARCHAR(8000),
@Separator VARCHAR(255)
)
RETURNS @SplitKeyword TABLE (Keyword VARCHAR(8000))
AS
BEGIN
DECLARE @TempWord VARCHAR(255)
DECLARE @TempKeyword TABLE (Keyword VARCHAR(8000))

WHILE (CHARINDEX(@Separator, @Word, 1)>0)
BEGIN
SET @TempWord = SUBSTRING(@Word, 1 , CHARINDEX(@Separator, @Word, 1) - 1)
SET @Word = SUBSTRING(@Word, CHARINDEX(@Separator, @Word, 1) + 1, LEN(@Word))

INSERT INTO @TempKeyword VALUES(@TempWord)
END

INSERT INTO @TempKeyword VALUES(@Word)

INSERT @SplitKeyword
SELECT * FROM @TempKeyword
RETURN
END

SELECT * FROM Split('10/31','/')

Tuesday, July 15, 2008

Understanding Session.Abandon

When the web application requires a log off based on session, clear the session state or call abandon(Session.Abandon) method.
Session.Abandon flushes the session state. The Abandon method sets flag which tells the session state needs to be abandoned.

The flag is evaluated at the end of the page request and action takes based on the status of the flag.
As soon as page processing is done, the session is removed.
When Session.Abandon is called, the session ID cookie is not removed for the browser.
Even after Session.Abandon call, any new request to the application uses the same Session ID with new session state instance.

If user opens another application within the same DNS domain, the user will not lose their session state after the Abandon method is called from the application.

To make session ID cookie null and to get rid of above problem, use the below code

Session.Abandon();
Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));

To assure that when user opens the log on page send null cookie to the client. The simple way to send a null cookie is using Response.Redirect.

Lets try one example :

create one aspx page and put below code in Page_Load

Response.Write(Session.SessionID + "
");
Session.Abandon();
Response.Write(Session.SessionID + "
");

you will get same SessionID before and after Session.Abandon

Lets create one more aspx with the below code

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack && (Request.Cookies["TESTLOGIN"] == null || Request.Cookies["TESTLOGIN"].Value == ""))
{
Session.Abandon();
Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));
AddCookie();
Response.Redirect(Request.Path);
}
Response.Write("Session.SessionID=" + Session.SessionID + "
");
Response.Write("Cookie ASP.NET_SessionId=" + Request.Cookies["ASP.NET_SessionId"].Value + "
");
}


private void AddCookie()
{
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, "TestCookie", DateTime.Now, DateTime.Now.AddSeconds(5), false,"");
string encryptedText = FormsAuthentication.Encrypt(ticket);
Response.Cookies.Add(new HttpCookie("TESTLOGIN", encryptedText));
}

I have used a different cookie to find out whether I am redirected to the logon page.

Add watch and check Session.SessionId value. You will notice that session ID will change.

Happy Coding :)

Saturday, July 5, 2008

DropDownlist Tooltip for each item

Few days back one of my colleague wanted to show tool tip on each item, due to inappropriate rendering of contents of DropDownList. Below figure shows that DropDownlist Items are not coming completely due to small width of DropDownList and large data.



I searched on net but didn't find any appropriate solution for it.Then I thought to incorporate tooltip for each item. Here is the code to implement it.

string str = "Add Item in DDL ";
for (int i = 0; i < 11; i++)
{
DropDownList1.Items.Add(str + i.ToString());
DropDownList1.Items[i].Attributes.Add("title", str + i.ToString());
}

Title attributes add tooltip in each item in DropDownList.

Note : Title attribute doesn't work in IE 6.

Happy Coding :)

Site Meter