if you have only one condition, you can remove if , and do this on grid databound event , because when you use item command it post back and after it response you.
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
LinkButton btn = (LinkButton) FindControl("lnkbtn");
btn.Attributes.Add("onclick","alert(you are leaving this page!)");
}