I need to borrow an extra set of eyes for this....
Anyone notice something I'm doing wrong in my code?
Every so ofter, I'll select my delete button in my datagrid and my javascript does not run, prompting with a message box for verification. Here's my code...
Code:
private void grdBooks_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType != ListItemType.Header && e.Item.ItemType != ListItemType.Footer)
{
LinkButton delete = (LinkButton)e.Item.Cells[e.Item.Cells.Count-1].Controls[0];
delete.Attributes["onclick"] ="javascript:return confirm('Are you sure you want to delete "
+ DataBinder.Eval(e.Item.DataItem, "Title") + "?')";
}
}
private void grdBooks_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int key = (int)grdBooks.DataKeys[e.Item.ItemIndex];
string connectionString = "Data Source=localhost;Initial Catalog=VegaSoft;uid=sa;pwd=;";
try {
Books bLayer = new Books(connectionString);
bool result = bLayer.DeleteBook(key);
BindData();
}
catch {
}
}
private void BindData()
{
string connectionString = "Data Source=localhost;Initial Catalog=VegaSoft;uid=sa;pwd=;";
Books bLayer = new Books(connectionString);
try {
grdBooks.DataSource = bLayer.GetBooks();
Session["LibrarySet"] = grdBooks.DataSource;
grdBooks.DataKeyField = "BookID";
grdBooks.CurrentPageIndex = 0;
grdBooks.DataBind();
}
catch {
}
}