Mark, I don't find Intellisense list any property named Command for the
LinkButton control as you have shown in your post but of course, the
LinkButton control does have a OnCommand property! So how do I get the
CommandName of each LinkButton?
Actually what I am doing is I am getting the total no. of orders a user
has placed from the database (using a stored procedure) & returning it
in a SqlDataReader. This is the function which does that in the VB
class file:
Namespace Shop
Public Class ShopCart
Private sqlConn As New SqlConnection(" .....")
Public Function GetOrderCount(B yVal UserID As Integer) As
SqlDataReader
Dim sqlCmd As SqlCommand
Dim sqlReader As SqlDataReader
sqlCmd = New SqlCommand("Cou ntOrders", sqlConn)
sqlCmd.CommandT ype = CommandType.Sto redProcedure
Try
With sqlCmd
.Parameters.Add ("@UserID", SqlDbType.Int). Value =
UserID
End With
sqlConn.Open()
sqlReader = sqlCmd.ExecuteR eader
If (sqlReader.HasR ows) Then
Return sqlReader
Else
Return Nothing
End If
Catch ex As Exception
Throw ex
End Try
End Function
End Class
End Namespace
This is the ASPX code that invokes the above function:
<%@ Import Namespace="Shop " %>
'importing other necessary Namespaces here
Sub Page_Load(..... )
Dim boShopCart As ShopCart
Dim lnkCount As LinkButton
Dim iCount As Integer = 1
Dim sqlReader As SqlDataReader
boShopCart = New ShopCart
sqlReader = boShopCart.GetO rderCount(iUser ID)
While (sqlReader.Read )
If (sqlReader.GetV alue(0) 1) Then
iTotalOrder = sqlReader.GetVa lue(0)
Else
pnlLinks.Visibl e = False
End If
End While
For iCount = 1 To iTotalOrder
lnkCount = New LinkButton
lnkCount.ID = "lnkCount" & iCount
lnkCount.Text = iCount.ToString & " &nbs p;"
lnkCount.Comman dName = iCount
pnlLinks.Contro ls.Add(lnkCount )
Next
End Sub
Assuming that a user has placed 5 orders, the above ASPX code is
displaying the 5 LinkButtons 1 2 3 4 5.....no problem till this
point but how do I get the CommandName of each LinkButton.
My main intention is to get the CommandName of the LinkButtons. Prior
to this, I was using HTML hyperlinks to show 1 2 3 4 5 & passing it
as a querystring. For e.g. the href value of the hyperlink would be,
say, ShowOrder.aspx? OrderID=23&Page Num=1 so that when the user clicks
1, 1 no longer remains a hyperlink.
Mark Rae wrote:
<rn**@rediffmai l.comwrote in message
news:11******** *************@k 70g2000cwa.goog legroups.com...
In a shopping cart app, suppose a user has placed 5 orders, I want to
show him 5 LinkButtons (one for each order) so that when he clicks the
first LinkButton, he would be shown the details of his first order.
Likewise if he clicks the second LinkButton, he will be shown the
details of the second order he had placed. The Text of the LinkButtons
will be 1 2 3 etc. So this user would see 1 2 3 4 5 as the
LinkButtons.
The problem is how do I add the LinkButtons dynamically. Do I have to
first create a user control & then using Page.LoadContro l add the
LinkButtons to the ASPX page? Or is there some other way out?
Can someone suggest me how do I go about it?
Dead easy.
When your page loads, I'm assuming that you already know how many orders are
in the cart - mayhe you're storing the cart in a Session variable, or
fetching it from a database or whatever - doesn't really matter... The
following assumes that you've fetched it into
1) In Page_Init, not Page_Load, fetch your cart
2) For each element in the cart (order, product, whatever), create a new
LinkButton() object:
LinkButton lnkDynamic;
foreach(<whatev erin <cart>)
{
lnkDynamic = new LinkButton();
lnkDynamic.Enab leViewState = true;
lnkDynamic.ID = "lnkDynamic _" + <identifier>;
lnkDynamic.Text = "SomeText";
lnkDynamic.Comm and += new CommandEventHan dler(lnkDynamic _Process);
lnkDynamic.Comm andName = <whatever>;
lnkDynamic.Visi ble = false;
this.Controls.A dd(lnkDynamic);
}
That will have created as many individual buttons as there are elements in
your cart.
3) Create the event handler
private void lnkDynamic_Comm and(object sender, CommandEventArg s e)
{
// your code to process the individual elements goes here
// use e.CommandName to work out which button was clicked
}
4) Place the individual dynamic buttons on your form wherever they need to
be e.g. in the cells of a GridView, Repeater etc and make them visible.
Obviously you will need to adapt the above to suit your particular needs,
but it should be enough to get you started.