Hi,
I understand the code for passing a parameter of one field item from a
button using the command argument. What I need to do is pass three
fields. For example
My button code currently is as follows:
--------------------------------------------------------------------------------------------
Inside a data grid
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton id=linkButton CommandArgument='<
%#databinder.eval(Container.dataitem, "intProductID") %>' Text="Buy"
Runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
--------------------------------------------------------------------------------------------------
My codebehind is
Private Sub itemInfo_itemCommand(ByVal source As Object, ByVal e As
System.web.UI.WebControls.DataGridCommandEventArgs) Handles
itemInfo.ItemCommand
'The command arguement of the button that was clicked
'In the datagrid contains the productID
Dim intProductID As Integer = e.CommandArgument
'Add the product to the shopping cart
ShoppingCart.AddProduct(intProductID)
End Sub
------------------------------------------------------------------------------------------
ShoppingCart.AddProduct(intProductID) is in a class document and is as
follows
Public Shared Function AddProduct(ByVal intProductID As Integer)
' Create the connection object
Dim connection As New SqlConnection(connectionString)
' Create and initialize the command object
Dim command As New SqlCommand("AddProductToCart", connection)
command.CommandType = CommandType.StoredProcedure
' Add an input parameter and supply a value for it
command.Parameters.Add("@CartID", SqlDbType.Char, 36)
command.Parameters("@CartID").Value = shoppingCartID
' Add an input parameter and supply a value for it
command.Parameters.Add("@intProductID", SqlDbType.Int)
command.Parameters("@intProductID").Value = intProductID
' Open the connection, execute the command, and close the
connection
Try
connection.Open()
command.ExecuteNonQuery()
Finally
connection.Close()
End Try
End Function
Now I not only need to pass IntProductID but also size and color. How
will I create three parameters using a button?
Would love ideas
Thanks
LK
tomisarobot@gmail.com - 05 Oct 2007 21:31 GMT
i hope there are better options, but here are 2 ideas.
concatenate your fields in the argument and split them on the other
end.
set the DataGrid's DataKeyField and use it to lookup your arguments
again later from a persisted object.
Elroyskimms - 06 Oct 2007 01:39 GMT
> Hi,
>
[quoted text clipped - 64 lines]
> Thanks
> LK
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton
id=linkButton
CommandArgument='<%# databinder.eval(Container.dataitem,
"intProductID") & "|" & databinder.eval(Container.dataitem,
"charProductColor") & "|" & databinder.eval(Container.dataitem,
"intProductSize") %>' Text="Buy"
Runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
In your ItemCommand event handler:
dim Args(2) as string = cstr(e.CommandName).Split("|")
dim ProductID as integer = cint(Args(0))
dim ProductColor as string = Args(1)
dim ProductSize as integer = cint(Args(2))
HTH,
-E