Saturday, March 24, 2012

trouble with click triggered events

i have a datagrid with delete text links.

I want a confirmation to be made after clicking delete once.


protected void DataGrid1_Del(object sender, DataGridCommandEventArgs e)
{
// make sure things that are invisible stay that way. Also make sure all fields are still blank.
ClearForm();
DataGrid1.EditItemIndex= -1;

MsgLabel.Text = "Are you sure you want to delete the record?";
MsgLabel.Visible = true;
CancelBtn.Visible = true;
DeleteBtn.Visible = true;

} // end DataGrid1_Del

private void DeleteBtn_Click(object sender, System.EventArgs e)
{
String deleteCmd = "DELETE from Inventory where InvName = @dotnet.itags.org.InvName";

SqlCommand myCommand = new SqlCommand(deleteCmd, sqlConnection1);
myCommand.Parameters.Add(new SqlParameter("@dotnet.itags.org.InvName", SqlDbType.NVarChar, 20));
myCommand.Parameters["@dotnet.itags.org.InvName"].Value = DataGrid1.DataKeys[(int)e.Item.ItemIndex];****

myCommand.Connection.Open();
myCommand.ExecuteNonQuery();

myCommand.Connection.Close();
dataSet11.Clear();
sqlDataAdapter1.Fill(dataSet11);

DataGrid1.DataBind();
}

What this code is SUPPOSED to do is, when the delete event is triggered, it will ask you if you are sure you want to delete the record. Upon clicking either delete or cancel, you will call the corresponding function.

The problem here is that, in my DeleteBtn_Click function, it does not recognize e.Item.ItemIndex, b/c this function e is System.EventArgs, instead of DataGridCommandEventArgs. (line is marked ****)

Does anyone have any suggestions to accomplish what I'm trying to do here?

Thanks,
TomIf I understand correctly, in "DataGrid1_Del()" can you save the "itemindex" in a session object and further use it in "DeleteBtn_Click()"?
Hope this helps
Yugang

_______________________

This posting is provided "AS IS" with no warranties, and confers no rights.
could you direct me to info on sessions? That is outside my "knowledge base." :D
Inside "DataGrid1_Del()" use code to record itemindex:
Session.Add("ItemIndex", e.Item.ItemIndex)

Inside "DeleteBtn_Click" get value from session:
Dim itenIndex As Integer = CInt(Session.Item("ItemIndex"))

For complete introduction, please refer:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconsessionstate.asp

Hope this helps
Yugang
I will check the link you provided, thanks.

Initially, when I modify my code as you suggested, in the:

int itemIndex = CInt(Session.Item("ItemIndex"))

line, Session does not have a definition for "Item." (C#)

Are you sure this is correct? Like I said, I'll be checking that link.
Sorry to provide you with VB Code. For C# the code should be:
int itemIndex = int.Parse(Session["ItemIndex"].ToString())
Thanks
Yugang
cool deal, so what should I use in this line?

myCommand.Parameters["@.InvName"].Value = DataGrid1.DataKeys[(int)e.Item.ItemIndex];

What would I replace e.Item.ItemIndex with?

thanks Yugang!
ooops, as i clicked post, i saw what i was looking at, stupid me. I put

myCommand.Parameters["@.InvName"].Value = DataGrid1.DataKeys[itemIndex];

right?
Yes. Please try it
Thanks
Yugang
worked like a charm, much thanks Yugang!!!

Tom
I would stay away from the Session object (this also goes with Application) object. They have many problems (like thread affinity) which cause scaling problems.

0 comments:

Post a Comment