Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

image button in a datagrid

Status
Not open for further replies.

ikkedm

Programmer
Apr 15, 2003
8
0
0
BE
hi,

is here someone who can help me?
i want to use a imagebutton in a dategrid, but when i click on the button nothing happens. My funcion is never called, my page reloads that's all that happens.

here my asp code:

<P><asp:datagrid id=&quot;ficheGrid&quot; Width=&quot;100%&quot; Runat=&quot;server&quot; Font-Size=&quot;XX-Small&quot; Font-Names=&quot;verdana&quot; cellpadding=&quot;2&quot; AutoGenerateColumns=&quot;False&quot; Height=&quot;25px&quot; OnEditCommand=&quot;ficheGrid_OnEdit&quot;>
<HeaderStyle CssClass=&quot;grid-header&quot; Font-Bold=&quot;True&quot; Height=&quot;25px&quot;></HeaderStyle>
<Columns>
<asp:TemplateColumn Visible=&quot;true&quot; HeaderText=&quot;Edit&quot;>
<HeaderStyle CssClass=&quot;grid-header&quot; Wrap=&quot;False&quot; HorizontalAlign=&quot;Center&quot; Width=&quot;50px&quot; VerticalAlign=&quot;Middle&quot;></HeaderStyle>
<ItemStyle cssclass=&quot;grid-edit-column&quot; HorizontalAlign=&quot;Center&quot;></ItemStyle>
<ItemTemplate>
<asp:ImageButton id=&quot;fichemgbtnedit&quot; Runat=&quot;server&quot; CausesValidation=&quot;False&quot; commandName=&quot;Edit&quot; ImageUrl=&quot;~/studentIcons/edit.gif&quot;></asp:ImageButton>
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton id=&quot;ficheimgbtnupdate&quot; Runat=&quot;server&quot; CausesValidation=&quot;true&quot; commandName=&quot;Update&quot; ImageUrl=&quot;~/studentIcons/icon-floppy.gif&quot;></asp:ImageButton>
<asp:ImageButton id=&quot;ficheimgbtncancel&quot; Runat=&quot;server&quot; CausesValidation=&quot;false&quot; commandName=&quot;Cancel&quot; ImageUrl=&quot;~/studentIcons/edit_x.gif&quot;></asp:ImageButton>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid></P>

Michaël
 
The problem you have is(I think because code behind is missing) that the imagebutton does not exsist untill the datagrid makes it. You should use the ItemDataBound event to set the click event for the buttons.
 
my code behind is:

Protected Sub fichegrid_onEdit(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Handles ficheGrid.ItemCommand
If e.CommandName = &quot;Select&quot; Then
lblfoutmelding.Text = &quot;fout&quot;
End If
Dim imagebutton As imagebutton = e.CommandSource
ficheGrid.EditItemIndex = e.Item.ItemIndex
bindfichegrid()
End Sub

but he never reach it!!

thx
 
I think the ItemCommand only fires if a button from a buttoncolumn is clicked.
You do not have a buttoncolumn but a TemplateColumn.
Try to attach an event when the button is created in the ItemDataBound event of the datagrid see code:

Private Sub ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles fichegrid.ItemDataBound
Dim intRow As Int16 = e.Item.ItemIndex
Dim objIMGBut As Object
' set events for the imagebuttons:
If intRow > -1 Then ' header and footer rows have an itemIndex of -1
Try
objIMGBut = e.Item.FindControl(&quot;fichemgbtnedit&quot;)
If IsNothing(objIMGBut) Then
objIMGBut = Nothing
Else
imgButEdit = CType(objIMGBut, ImageButton)
' attach an onclick event here
End If
Catch
End Try
End If
end Sub

I am not sure this is THE way to because I am just starting to use .net. I would be verry interested to see what IS the way to go so I will switch on email for this thread.
 
i have tried, but it doesn't works

i know in a buttoncolumn you can't work with a imagebutton only linkbutton or a pushbutton are allowed

 
If you are using visual studio.net you should put some brakpoints in the following funciton:

Private Sub ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles fichegrid.ItemDataBound
Dim intRow As Int16 = e.Item.ItemIndex
Dim objIMGBut As Object
' set events for the imagebuttons:
If intRow > -1 Then ' header and footer rows have an itemIndex of -1
Try
objIMGBut = e.Item.FindControl(&quot;fichemgbtnedit&quot;)
If IsNothing(objIMGBut) Then
objIMGBut = Nothing
Else
imgButEdit = CType(objIMGBut, ImageButton)
the following line will set the commandarg
when the button is clicked later you will know what row was clicked
imgButEdit.CommandArgument = intRow
End If
Catch
End Try
End If
end Sub
add this function in your code behind file:
Private Sub imgButEdit_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
Dim strClickedRow As String = sender.CommandArgument
' strClickedRow contains the number of the row that has been clicked so you can set the selectedindex to this number
End Sub





in your aspx page:
<asp:ImageButton id=&quot;fichemgbtnedit&quot; Runat=&quot;server&quot; CausesValidation=&quot;False&quot; commandName=&quot;Edit&quot; ImageUrl=&quot;~/studentIcons/edit.gif&quot; onClick=&quot;imgButEdit_Click&quot;></asp:ImageButton>




Greetings, Harm Meijer
 
I am only a beginner but this is what I use:

Data grid:

<asp:DataGrid runat=&quot;server&quot; id=&quot;dgitemlist&quot; visible=false
AutoGenerateColumns=&quot;False&quot;
Showheader=&quot;true&quot;
AllowPaging=&quot;True&quot;
PagerStyle-Mode=&quot;NumericPages&quot;
CssClass=&quot;maintextblack&quot;
OnPageIndexChanged=&quot;myDataGrid_PageChangerS&quot;
OnItemDataBound=&quot;myDataGrid_Format&quot;
OnItemCommand=&quot;dispDetails&quot;>
<AlternatingItemStyle BackCOLOR=#FFD3A3 />
<HeaderStyle BackCOLOR=#F37021 />
<FooterStyle BackCOLOR=#F37021 />
<Columns>
<asp:BoundColumn DataField=&quot;id&quot; visible=False />
...Other Columns...
<asp:TemplateColumn ItemStyle-Width=&quot;9&quot; itemstyle-backcolor=#F37021>
<ItemTemplate>
<asp:ImageButton ImageUrl=&quot;Images/button.gif&quot; CommandName=&quot;details&quot; ID=&quot;btnview&quot; Runat=&quot;server&quot; />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>


vbCode:
Sub dispDetails(sender as Object, e As DataGridCommandEventArgs)
Dim FAQIDColumn as TableCell = e.Item.Cells(0)
if FAQIDColumn.text =&quot;&quot; then
exit sub
end if
Dim intId as integer = FAQIDColumn.Text
dim strSQLType as String
...etc

to
end sub

Hope this is of help.
Jon
 
Michael with the funky e,

Your code is fine. The thing that went wonky for you was which data grid method you used. Your original code said:

<asp:ImageButton id=&quot;fichemgbtnedit&quot; Runat=&quot;server&quot; CausesValidation=&quot;False&quot; commandName=&quot;Edit&quot;

but in your code behind, you're using the ItemCommand.

Since your command name is Edit, you should be using the datagrids EditCommand event. Put your code in there and I bet it'll all work out for ya.

hth

D'Arcy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top