ImageButton click

D

David Lozzi

Howdy,

New to .Net 2.0 and I'm about to go back to 1.1! So many little changes are
driving me nuts. I have an ImageButton in a GridView template column. When
clicking the image, it should send to another page with a specified query
string. The images appear great. I have done the following scenarios, all
with no results

1) Use OnClick of button specifying sub. Sub simply either
response.redirect(page) or server.transfer(page,true) and nothing.

2) Use the OnRowCommand of the gridview, specify a sub, and use the same
code as #1.

3) Created a whole new image button outside of the gridview with the onclick
event and still nothing.

For each of these the page blinks like it posts back to the server but thats
it. I know I'm missing something but what????

THANK YOU!
 
B

Bruno Alexandre

the changes from 1.x to 2.0 are more than many, and you can't give up easily
cause 2.0 is way better and faster than 1.x

for your problem do this:


inside the template item in your GridView your image must to be like:

<asp:ImageButton ID="img" runat="server"
ImageUrl='<%# Eval("tImageURL", "~/filterqueen.dk/images/products/{0}")
%>'
AlternateText="No Image."
CommandName='<%# Eval("tLocalCode_DK") %>'
CommandArgument='<%# Eval("tValue") & "<||>" & Eval("tName") %>'
OnCommand="img_Command" />

and you use the OnCOmmand from the image, not from the GridView (no need for
that) and you can use the commandName and COmmandArgument to get thoose
values, like:

Protected Sub img_Command(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.CommandEventArgs)

Dim aa As Array
Dim i As Integer = 0
Dim preco, nome As String

aa = Split(e.CommandArgument, "<||>")

preco = aa(0)
nome = aa(1)

If preco = String.Empty Then
preco = "0"
End If


If Session("FQ-PRD-showPrdValues") = True Then
ShoppingCart.addItem(e.CommandName, preco, nome)
lblMsg.Text = "<br/>Følgende varer er lagt i indkøbskurven (" &
e.CommandName & ")"
lblHowMany.Text = "Du har følgende " & ShoppingCart.itens & "
vare i din indkøbskurv"
lblMsg.Visible = True
End If


End Sub
 
D

David Lozzi

No such luck, here's my code for the gridview and the command. THANKS!

<asp:GridView ID="gvNav" runat="server" AutoGenerateColumns="true" ShowHeader="false" CellSpacing="4" BorderStyle="none"

BorderWidth="0" GridLines="None">

<Columns>

<asp:TemplateField><ItemTemplate><asp:ImageButton runat="server" ID="imgLink" CausesValidation="false" OnCommand="GoToDept" /></ItemTemplate></asp:TemplateField>

</Columns>

</asp:GridView>

Protected Sub gvNav_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvNav.RowDataBound

If e.Row.RowType = DataControlRowType.DataRow Then

Dim img As ImageButton = e.Row.FindControl("imgLink")

Dim fld As LeftNavItem = e.Row.DataItem

img.ImageUrl = LCase("~/leftnav_images/" & fld.Image & ".gif")

img.AlternateText = fld.Name

img.CommandArgument = fld.Image

End If

End Sub



Sub GoToDept(ByVal sender As Object, ByVal e As CommandEventArgs)

Response.Write("HELLO!")

End Sub
 
D

David Lozzi

No such luck, here's my code for the gridview and the command. THANKS!

<asp:GridView ID="gvNav" runat="server" AutoGenerateColumns="true" ShowHeader="false" CellSpacing="4" BorderStyle="none"

BorderWidth="0" GridLines="None">

<Columns>

<asp:TemplateField><ItemTemplate><asp:ImageButton runat="server" ID="imgLink" CausesValidation="false" OnCommand="GoToDept" /></ItemTemplate></asp:TemplateField>

</Columns>

</asp:GridView>

Protected Sub gvNav_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvNav.RowDataBound

If e.Row.RowType = DataControlRowType.DataRow Then

Dim img As ImageButton = e.Row.FindControl("imgLink")

Dim fld As LeftNavItem = e.Row.DataItem

img.ImageUrl = LCase("~/leftnav_images/" & fld.Image & ".gif")

img.AlternateText = fld.Name

img.CommandArgument = fld.Image

End If

End Sub



Sub GoToDept(ByVal sender As Object, ByVal e As CommandEventArgs)

Response.Write("HELLO!")

End Sub
 
R

ReyN

hello David

just curious

if you intend to let users navigate to another page when they click on
a row in a GridView, then use a hyperlinkfield ( or a hyperlink within
a templatefield ) instead

http://authors.aspalliance.com/aspxtreme/webforms/controls/addinghyperlinkfieldstogridview.aspx


buttonfields ( or buttons within a templatefield ( linkbuttons,
pushbuttons, and imagebuttons ) ) ALL are meant for posting back to the
same page, not for navigating to another url

http://authors.aspalliance.com/aspxtreme/webforms/controls/addingbuttonfieldstogridview.aspx
 
D

David Lozzi

I thought of that but I couldnt figure out how to specify the alt text of a
hyperlink image....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,815
Latest member
treekmostly22

Latest Threads

Top