Showing posts with label delete. Show all posts
Showing posts with label delete. Show all posts

Saturday, March 31, 2012

Triggering ItemCommand with ImageButton in Datagrid

Hi

I had a Delete Column in a datagrid that was a Linkbutton. That needed to
change into a ImageButton because of the design demands in the project. Then
how do I trigger the DeleteCommand with that button?

I don't want to have to add a AddHandler in the ItemCreated or ItemDataBound
for this ImageButton cause then I can't get the ID from the Row to delete as
easy...

please help/
Lars NetzelSpecify the ImageButton with CommandName="Delete" attribute.

<asp:ImageButton ID="btnDelete" runat="server" CommandName="Delete" .../
CommandNames Cancel, Delete, Edit, NextPage, Page, PrevPage, Select, Sort
and Update correspond to the same action with the DataGrid (DataGrid has
these command names predefined for cthese actions)

See docs for further details.

--
Teemu Keiski
ASP.NET MVP, Finland

"Lars Netzel" <uihsdf@.adf.se> wrote in message
news:%23edWRq6LFHA.3328@.TK2MSFTNGP14.phx.gbl...
> Hi
> I had a Delete Column in a datagrid that was a Linkbutton. That needed to
> change into a ImageButton because of the design demands in the project.
> Then how do I trigger the DeleteCommand with that button?
> I don't want to have to add a AddHandler in the ItemCreated or
> ItemDataBound for this ImageButton cause then I can't get the ID from the
> Row to delete as easy...
> please help/
> Lars Netzel
Ah, stupid me:) I wrote ItemCommand instead of CommandName... thx

/Lars

"Teemu Keiski" <joteke@.aspalliance.com> wrote in message
news:eHCfx26LFHA.3988@.tk2msftngp13.phx.gbl...
> Specify the ImageButton with CommandName="Delete" attribute.
> <asp:ImageButton ID="btnDelete" runat="server" CommandName="Delete" .../>
> CommandNames Cancel, Delete, Edit, NextPage, Page, PrevPage, Select, Sort
> and Update correspond to the same action with the DataGrid (DataGrid has
> these command names predefined for cthese actions)
> See docs for further details.
> --
> Teemu Keiski
> ASP.NET MVP, Finland
> "Lars Netzel" <uihsdf@.adf.se> wrote in message
> news:%23edWRq6LFHA.3328@.TK2MSFTNGP14.phx.gbl...
>> Hi
>>
>> I had a Delete Column in a datagrid that was a Linkbutton. That needed to
>> change into a ImageButton because of the design demands in the project.
>> Then how do I trigger the DeleteCommand with that button?
>>
>> I don't want to have to add a AddHandler in the ItemCreated or
>> ItemDataBound for this ImageButton cause then I can't get the ID from the
>> Row to delete as easy...
>>
>> please help/
>> Lars Netzel
>>

Triggering ItemCommand with ImageButton in Datagrid

Hi
I had a Delete Column in a datagrid that was a Linkbutton. That needed to
change into a ImageButton because of the design demands in the project. Then
how do I trigger the DeleteCommand with that button?
I don't want to have to add a AddHandler in the ItemCreated or ItemDataBound
for this ImageButton cause then I can't get the ID from the Row to delete as
easy...
please help/
Lars NetzelSpecify the ImageButton with CommandName="Delete" attribute.
<asp:ImageButton ID="btnDelete" runat="server" CommandName="Delete" .../>
CommandNames Cancel, Delete, Edit, NextPage, Page, PrevPage, Select, Sort
and Update correspond to the same action with the DataGrid (DataGrid has
these command names predefined for cthese actions)
See docs for further details.
Teemu Keiski
ASP.NET MVP, Finland
"Lars Netzel" <uihsdf@.adf.se> wrote in message
news:%23edWRq6LFHA.3328@.TK2MSFTNGP14.phx.gbl...
> Hi
> I had a Delete Column in a datagrid that was a Linkbutton. That needed to
> change into a ImageButton because of the design demands in the project.
> Then how do I trigger the DeleteCommand with that button?
> I don't want to have to add a AddHandler in the ItemCreated or
> ItemDataBound for this ImageButton cause then I can't get the ID from the
> Row to delete as easy...
> please help/
> Lars Netzel
>
Ah, stupid me:) I wrote ItemCommand instead of CommandName... thx
/Lars
"Teemu Keiski" <joteke@.aspalliance.com> wrote in message
news:eHCfx26LFHA.3988@.tk2msftngp13.phx.gbl...
> Specify the ImageButton with CommandName="Delete" attribute.
> <asp:ImageButton ID="btnDelete" runat="server" CommandName="Delete" .../>
> CommandNames Cancel, Delete, Edit, NextPage, Page, PrevPage, Select, Sort
> and Update correspond to the same action with the DataGrid (DataGrid has
> these command names predefined for cthese actions)
> See docs for further details.
> --
> Teemu Keiski
> ASP.NET MVP, Finland
> "Lars Netzel" <uihsdf@.adf.se> wrote in message
> news:%23edWRq6LFHA.3328@.TK2MSFTNGP14.phx.gbl...
>

trim function

does anyone knows what does Trim(!Delete & "") means? what is this Delete ?
thanks
Regards
GohIs it a boolean or some string variable declared elsewhere in your code?
Apparently this isn't an ASP.NET question, the 'original' post is here:

http://www.vbforums.com/showthread.php?p=2067378#post2067378

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.