Link to home
Start Free TrialLog in
Avatar of dimensionav
dimensionavFlag for Mexico

asked on

How to use dynamic values on CommandArgument

HI
I am using the following control:

      <asp:LinkButton ID="lbComprar" runat="server" OnCommand="lbComprar_Click"
                      CommandArgument='<%=idproducto%>'
                      CommandName="Comprar" CssClass="MasDetalles" Text="Comprar"/>

What I want is to pass a dynamic/variable value to OnCommand method using CommandArgument, but the debug shows that the only value that its passed is a string equals to <%=idproducto%>.

¿Any ideas?

Thanks.
Avatar of Toms Edison
Toms Edison
Flag of India image

Try this

<asp:LinkButton ID="lbComprar" runat="server" OnCommand="lbComprar_Click"
                      CommandArgument='<%#=idproducto%>'
                      CommandName="Comprar" CssClass="MasDetalles" Text="Comprar"/>
Avatar of dimensionav

ASKER

Hi TechTiger007

Now The output VS2005 window shows:
error BC30201: expression expected
dimensionav,

This error could be due to some other thing. I tried this line of code and it showed up a link with no errors. Debug the code and let know where exactly is this error
HI

Here is my code:

        <asp:LinkButton ID="lbComprar"
                        OnCommand="lbComprar_Click"
                        CommandArgument='<%#=idproducto%>'
                        CommandName="Comprar" runat="server" Text="Comprar"/>

And as I said before is:
error BC30201: expression expected

Is presented just at the time of compiling, normal or compiling mode (avoids to execute the application).



That could be due to the = sign

copy and paste the code below and try it out
<asp:LinkButton ID="lbComprar" runat="server" CommandArgument='<%#idproducto%>'
                            CommandName="Comprar" OnCommand="lbComprar_Click" Text="Comprar">
                        </asp:LinkButton>                        

HI TechTiger007

I have attached the part of the code that uses the value passed by command argument and you could see that it represents just an empty string.

Here is script content in which you can see the variable "idproducto" as a global element and how it is used on the method:

<%@ Page Language="VB" MasterPageFile="~/menu_productos.master" Title="Detalle de Producto" %>
<%@ MasterType VirtualPath="~/menu_productos.master" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="system.Data.OleDb" %>
<%@ Import Namespace="DAV.TiendaEnLinea.v1.BusinessLogicLayer" %>
<%@ Import Namespace="System.Collections.Generic" %>
 
<script runat="server">

Protected CurrentSession As DAV.TiendaEnLinea.v1.BusinessLogicLayer.BLLSessionManager
Dim objDetalleDeProducto As List(Of BLLDetalleDeProducto)
Dim objConfiguraciones As List(Of BLLConfiguraciones) = BLLConfiguraciones.ObtieneConfiguraciones()
Dim idproducto As Integer = 0
Dim PaginaActual As Integer = 0

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
'Obtiene Instancia de objeto de sesion y valores de controles de masterpage
CurrentSession = DirectCast(Session("CurrentSession"), DAV.TiendaEnLinea.v1.BusinessLogicLayer.BLLSessionManager)
CurrentSession.IdProductoActual = CType(Request.QueryString("idproducto"), Integer)
PaginaActual = CType(CurrentSession.PaginaActual, Integer)
idproducto = CurrentSession.IdProductoActual
objDetalleDeProducto = BLLDetalleDeProducto.ObtieneDetalleDeProducto(idproducto)

If IsPostBack Then
End If
End Sub

Protected Sub lbComprar_Click(ByVal sender As Object, ByVal e As CommandEventArgs)
'Agrega producto al carrito de compras
CurrentSession.IdProductoActual = CType(e.CommandArgument, Integer)
CurrentSession.AgregaElementoEnCarrito(CurrentSession)
Response.Redirect("carrito.aspx")
End Sub

</script>

-012.jpg
ASKER CERTIFIED SOLUTION
Avatar of Toms Edison
Toms Edison
Flag of India 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
Avatar of Minion1
Minion1

This command wouldn't actually execute because lbComprar is not declared on Page_Load event.