Link to home
Start Free TrialLog in
Avatar of Peter Nordberg
Peter NordbergFlag for Sweden

asked on

Error on linkbutton click

Hi,

I have this usercontrol where I have a linkbutton that I want to handle some code behind when I click on it. The usercontrol is a part of a datalist control. Unfortunately I get an error when I click it:
-----------------------------------
Unable to cast object of type 'System.EventArgs' to type 'System.Web.UI.ImageClickEventArgs'.
----------------------------------
If I use an imagebutton instead it all works fine.

This is my usercontrol code:
------------------------------------------------
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="buyLink.ascx.vb" Inherits="UserControls_Webshop_buy" %>

<asp:HiddenField ID="productID" runat="server" />
<asp:LinkButton ID="btnBuy" runat="server" Width="58px" CssClass="btnBuy" OnClick="NewButton_Click" CommandArgument='<%# Eval("productID") %>'>Köp</asp:LinkButton>
<%--<asp:ImageButton ID="btnBuy" runat="server" CssClass="btnBuy" OnClick="NewButton_Click" CommandArgument='<%#Eval("productID") %>' />--%>
<telerik:RadAjaxManagerProxy ID="RadAjaxManagerProxy1" runat="server">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="btnBuy">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="lblShopCartPrice" />
                <telerik:AjaxUpdatedControl ControlID="PanelCheckout" />
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManagerProxy>
-----------------------------------------------------------
and this is my code behind:
---------------------------------
Sub NewButton_Click()
        If Request.Cookies("orderID") Is Nothing Then
            Dim i As New orderDBL
            i.InsertOrder()

            Response.Cookies("orderID").Value = i.orderID
            Response.Cookies("orderID").Expires = DateTime.Now.AddSeconds(600)


            InsertProduct()

        Else

            InsertProduct()

        End If
    End Sub
-------------------------------------------

I would like to use the linkbutton instead of the imagebutton but I'm not sure how I can fix this error.

Peter
Avatar of safiint
safiint
Flag of Syrian Arab Republic image

add the link button and insure that its handler method take these parameters:

Sub NewButton_Click(ByVal sender As Object, ByVal e As EventArgs)
Avatar of Avodah
Sounds like the method that is handling the click event has an incorrect signature for LinkButton. The LinkButton requires a signature of:

Sub LinkButton_Click(sender As Object, e As EventArgs)

while the ImageButton requires a signature of:

Sub LinkButton_Click(sender As Object, e As ImageClickEventArgs)

DaTribe
delete the onclick event of imagebutton and go to its desine view.then double click on image button.you will get the new onlclick event there put your functionality.
do this:
<asp:ImageButton ID="btnBuy" runat="server" CssClass="btnBuy" OnClick="" CommandArgument='<%#Eval("productID") %>' />

and then double click on the imagebutton in design view of page.
Avatar of Peter Nordberg

ASKER

Hi and thanks for all your suggestions.
I've tried all three but get the same error message anyway. As I said, if I'm using an image button it works allright but not with the linkbutton.
Peter
This is what it looks like now

<asp:LinkButton ID="btnBuy" runat="server" Width="58px" CssClass="btnBuy" OnClick="btnBuy_Click" CommandArgument='<%# Eval("productID") %>'>Kp</asp:LinkButton>

Protected Sub btnBuy_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnBuy.Click
If Request.Cookies("orderID") Is Nothing Then
Dim i As New orderDBL
i.InsertOrder()
Response.Cookies("orderID").Value = i.orderID
Response.Cookies("orderID").Expires = DateTime.Now.AddSeconds(600)

InsertProduct()
Else
InsertProduct()
End If
End Sub
ASKER CERTIFIED SOLUTION
Avatar of Avodah
Avodah
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Notice I changed the ImageClickEventArgs to EventArgs

DaTribe
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
HI,

That worked. Thanks for your help!
Peter