private void SetMailTarget(bool selectedOnly) { List <string> userIdsToEmail = new List <string>(); if (selectedOnly) { // TO SELCTED USERS foreach (GridViewRow row in UserGrid.Rows) { HtmlInputCheckBox checkbox = (HtmlInputCheckBox)row.FindControl("SelectUserCheckBox"); if ((checkbox != null) && (checkbox.Checked)) { int userId = Convert.ToInt32(UserGrid.DataKeys[row.RowIndex].Value); userIdsToEmail.Add(userId.ToString()); } } } else { // TO ALL USERS IN SEARCH RESULTS IList <User> users = UserDataSource.Search(GetSearchCriteria(), "U.LoweredUserName"); // Looping through all the rows in the GridView foreach (User user in users) { userIdsToEmail.Add(user.Id.ToString()); } } Session["SendMail_IdList"] = "UserId:" + string.Join(",", userIdsToEmail.ToArray()); }