ItemCommand Not Firing with ImageButton
-
OK I was just about to post a question on here, and I went to check one more thing before posting it. I got it to work. BUT, I still want to know why it didnt work the other way. I have a DataList for a menu. I have plus and minus gifs that expand and collapse when clicked. When these gifs were rendered using ImageButton, they would not fire the ItemCommand event for the DataGrid. I then Changed them to LinkButtons, with a basic image tag for the text, and the event fires....WHY? Here is the code for the scenario that did NOT work. (Using the ImageButton) '*********************************************************
'***************** THIS CHANGED************************ '****************************************************** .....HTML Table removed Here...... to keep it simple, this was just the department names, and category names with category names indented to show they resided under Departments ***** The code Behind for the ItemCommand Event That did NOT work Looked like this.... Public Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.ItemCommand '*********************THIS CHANGED *********************** If CType(e.Item.Cells(0).FindControl("img"), ImageButton).ImageUrl.EndsWith("closed.gif") Then '******************************************************* ' condition for node opening Dim i As Integer = 1 Dim Root As System.Int64 = Convert.ToInt64(DataGrid1.Items(e.Item.ItemIndex).Cells(1).Text()) Dim img As ImageButton For i = 1 To DataGrid1.Items.Count - 1 If (CInt(DataGrid1.Items(i).Cells(1).Text) = Root) Then 'This is a department DataGrid1.Items(i).Visible = True End If If CType(DataGrid1.Items(i).Cells(2).Text, Integer) > 0 Then DataGrid1.Items(i).Cells(0).Controls.Clear() DataGri
-
OK I was just about to post a question on here, and I went to check one more thing before posting it. I got it to work. BUT, I still want to know why it didnt work the other way. I have a DataList for a menu. I have plus and minus gifs that expand and collapse when clicked. When these gifs were rendered using ImageButton, they would not fire the ItemCommand event for the DataGrid. I then Changed them to LinkButtons, with a basic image tag for the text, and the event fires....WHY? Here is the code for the scenario that did NOT work. (Using the ImageButton) '*********************************************************
'***************** THIS CHANGED************************ '****************************************************** .....HTML Table removed Here...... to keep it simple, this was just the department names, and category names with category names indented to show they resided under Departments ***** The code Behind for the ItemCommand Event That did NOT work Looked like this.... Public Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.ItemCommand '*********************THIS CHANGED *********************** If CType(e.Item.Cells(0).FindControl("img"), ImageButton).ImageUrl.EndsWith("closed.gif") Then '******************************************************* ' condition for node opening Dim i As Integer = 1 Dim Root As System.Int64 = Convert.ToInt64(DataGrid1.Items(e.Item.ItemIndex).Cells(1).Text()) Dim img As ImageButton For i = 1 To DataGrid1.Items.Count - 1 If (CInt(DataGrid1.Items(i).Cells(1).Text) = Root) Then 'This is a department DataGrid1.Items(i).Visible = True End If If CType(DataGrid1.Items(i).Cells(2).Text, Integer) > 0 Then DataGrid1.Items(i).Cells(0).Controls.Clear() DataGri
Hi Shawn, Have you tried to run your application in debug mode to see if the
ItemCommand
event gets fired when you place an ImageButton in a template column? Basically, in addition to the predefined buttons such as edit, delete, update ..., theClick
event of the Button/LinkButton/ImageButton controls will be bubbled up to theItemCommand
event when they are placed inside a datagrid (also with datalist and repeater) control. -
Hi Shawn, Have you tried to run your application in debug mode to see if the
ItemCommand
event gets fired when you place an ImageButton in a template column? Basically, in addition to the predefined buttons such as edit, delete, update ..., theClick
event of the Button/LinkButton/ImageButton controls will be bubbled up to theItemCommand
event when they are placed inside a datagrid (also with datalist and repeater) control.minhpc, Yes, I did set a break point and ran the app in debug mode. The ItemCommand Event never fired when the ImageButton was in the column, But then when I changed it to a LinkButton, it fired. Im fairly new to ASP.NET, so I was just wondering why the LinkButton fired this event, but not the ImageButton. I put both versions of code in the starter thread, could you see why the ImageButton wouldnt fire? Thanks Shawn
-
minhpc, Yes, I did set a break point and ran the app in debug mode. The ItemCommand Event never fired when the ImageButton was in the column, But then when I changed it to a LinkButton, it fired. Im fairly new to ASP.NET, so I was just wondering why the LinkButton fired this event, but not the ImageButton. I put both versions of code in the starter thread, could you see why the ImageButton wouldnt fire? Thanks Shawn
Shawn, The datagrid control (after looking at your sample code) looks fine to me except the spelling error aps. May I suggest you to comment out all the source code in the ItemCommand event handler and simply put a simple call to Response.Write method? What I want to do here is to try to play around with the ImageButton in a very simple case and see if the ItemCommand event gets fired. Another way is to check if the Click event of the ImageButton gets fired.
-
Shawn, The datagrid control (after looking at your sample code) looks fine to me except the spelling error aps. May I suggest you to comment out all the source code in the ItemCommand event handler and simply put a simple call to Response.Write method? What I want to do here is to try to play around with the ImageButton in a very simple case and see if the ItemCommand event gets fired. Another way is to check if the Click event of the ImageButton gets fired.
minhpc, Yeah, That spelling error was not in the original code, but was done after I posted it into the code project form (as I was trying to format in order to make it more readable.) I did have break points set though, with all the code commented out of the event except for a simple If 1=1 Then Dim str As string = "Hello" and it never fired..... I dont know what it was, it was something weird though...no big deal, thanks for looking at it though Shawn
-
minhpc, Yeah, That spelling error was not in the original code, but was done after I posted it into the code project form (as I was trying to format in order to make it more readable.) I did have break points set though, with all the code commented out of the event except for a simple If 1=1 Then Dim str As string = "Hello" and it never fired..... I dont know what it was, it was something weird though...no big deal, thanks for looking at it though Shawn
Hi Shawn, I'm sorry when I can't help you figure out the cause. Anyway, if you have time, you might want to try with the sample code below which demonstrates the use of the ImageButton control in a template column. Hopefully, you can find a hint in your sample code:
<%@ Page Language="vb"%>
<%@ Import Namespace="System.Data"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>WebForm1</title>
<script runat="server" language="vb">
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
If Not IsPostBack Then
BindGrid()
End If
End SubPublic Sub BindGrid() Dim objConn As New System.Data.SqlClient.SqlConnection("server=localhost;uid=sa;pwd=;database=Northwind;Timeout=600;") objConn.Open() Dim strSQL As String Dim objDataset As New DataSet Dim objAdapter As New System.Data.SqlClient.SqlDataAdapter strSQL = "Select \* from customers" objAdapter.SelectCommand = New System.Data.SqlClient.SqlCommand(strSQL, objConn) objAdapter.Fill(objDataset) DataGrid1.DataSource = objDataset DataGrid1.DataBind() End Sub Public Sub DataGrid1\_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Response.Write("DataGrid1\_ItemCommand : " + e.CommandSource.GetType().ToString()) End Sub </script>
</head>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 264px; POSITION: absolute; TOP: 112px"
runat="server" AutoGenerateColumns="False" OnItemCommand="DataGrid1_ItemCommand">
<Columns>
<asp:TemplateColumn ItemStyle-Height="20px" ItemStyle-Width="5px">
<ItemTemplate>
<asp:ImageButton ID="img" ImageUrl="images/plus_open.gif" CommandName="imgclick"
Runat="server"></asp:ImageButton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="CustomerID" HeaderText="CustomerID"></asp:BoundColumn>
<asp:BoundColumn DataField="CompanyName" HeaderText="CompanyName"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
</form>