Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Dynamic LinkButton

Reply
Thread Tools

Dynamic LinkButton

 
 
rn5a@rediffmail.com
Guest
Posts: n/a
 
      11-09-2006
Well there's another place where I am getting stuck. The stored
procedure that gets the no. of orders a user has placed (please have a
look at the VB class file in post #4 in this thread) also gets the
OrderID of each order. In other words, the stored procedure retrieves 2
columns. I did like to set the OrderID as the CommandArgument of each
LinkButton but as shown in post #4, I am creating the LinkButtons
dynamically OUTSIDE the While(sqlReader.Read) loop since creating the
LinkButtons inside the While loop would mean looping the For...Next
loop (to create the dynamic LinkButtons) within the While loop. Under
such circumstances, a user who has placed, say, 4 orders would see the
LinkButtons 1 2 3 4 thrice which is something which I don't want.

But how do I get the OrderID of each order OUTSIDE the While
(sqlReader.Read) loop?


r...@rediffmail.com wrote:
> Gozirra, that's EXACTLY what I was looking out for (the AddHandler
> lnkDynamic.Command, AddressOf lnkDynamic_Command line). Thanks a lot.
>
> You as well as Mark have repeatedly stressed on fetching the cart (if I
> am not mistaken, by "fetching the cart", you & Mark mean getting the
> no. of orders a user has placed & the details of each & every order) in
> the Page_Init sub & NOT in the Page_Load sub. But what I find is even
> if I place the ASPX code I have shown in post #4 in this thread in the
> Page_Load sub & NOT in the Page_Init sub, then not only do the
> LinkButtons get recreated after each PostBack but also the events
> associated with the LinkButtons (in this case, the Command event) fire
> correctly after every PostBack. So why have you (& Mark) stressed on
> fetching the cart in the Page_Init sub & NOT in the Page_Load sub?
>
> Could you please clarify this point?
>
>
> Gozirra wrote:
> > lets take everything that was already suggested and VB'ify it. That
> > will hopefully make things a little more clear. Take note of #1 below.
> > Your dynamic buttons' events will not fire if they are not recreated
> > during page_init. Thats why it is so important that you fetch the cart
> > during this page event instead of during load. Best I can do. Hope it
> > helps.
> >
> > 1) In Page_Init, not Page_Load, fetch your cart
> >
> > 2) For each element in the cart (order, product, whatever), create a
> > new button.
> >
> > Dim lnkDynamic As LinkButton
> >
> > foreach(<whatever> in <cart>)
> > lnkDynamic = new LinkButton()
> > lnkDynamic.EnableViewState = true
> > lnkDynamic.ID = "lnkDynamic_" + <identifier> 'Identifier can be
> > your order# or whatever your want
> > lnkDynamic.Text = "SomeText"
> > AddHandler lnkDynamic.Command, AddressOf lnkDynamic_Command
> > lnkDynamic.CommandName = "AnyTextYouWant"
> > lnkDynamic.Visible = false
> > Me.Controls.Add(lnkDynamic)
> > Next
> >
> > That will have created as many individual buttons as there are elements
> > in
> > your cart.
> >
> > 3) Create the event handler
> >
> >
> > Public Sub lnkDynamic_Command(sender As object, e As CommandEventArgs)
> > ' your code to process the individual elements goes here
> > ' use e.CommandName to work out which button was clicked
> > ' You could put the order number into the command name
> > ' you could also cast sender to a LinkButton and check its ID if
> > you want. many different ways.
> > If e.CommandName = "1" Then
> > 'Do stuff for 1
> > End If
> >
> > or maybe
> >
> > Dim lb As LinkButton
> > lb = DirectCast(sender, LinkButton) 'or whatever your favorite
> > method is
> > If lb.ID == "1" Then 'or whatever the heck you want to do
> > 'Some stuff
> > End If
> > End Sub
> >
> >
> > 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.


 
Reply With Quote
 
 
 
 
Gozirra
Guest
Posts: n/a
 
      11-13-2006
Why build the list outside of the while loop then? Try something like
this. Since I do not know exactly what data is coming back, its kind
of hard to determine exactly what all of the possibilities are. But
hopefully it'll give you an idea if you haven't already gotten
everything fixed.

While (sqlReader.Read)
If (sqlReader.GetValue(0) > 1) Then
iTotalOrder = sqlReader.GetValue(0)

lnkDynamic = new LinkButton()
lnkDynamic.EnableViewState = true
lnkDynamic.ID = "lnkDynamic_" + <identifier> 'Identifier
can be your order# or whatever your want
lnkDynamic.Text = "SomeText"
AddHandler lnkDynamic.Command, AddressOf lnkDynamic_Command

lnkDynamic.CommandName = "AnyTextYouWant"
'sqlReader.GetValue(1) perhaps
lnkDynamic.Visible = false
Me.Controls.Add(lnkDynamic)
Else
pnlLinks.Visible = False
End If
End While

 
Reply With Quote
 
 
 
 
rn5a@rediffmail.com
Guest
Posts: n/a
 
      11-13-2006
Gozirra, the main reason why I want to get the OrderID of each order
outside the While(sqlReader.Read) loop is because I want to pass the
OrderID to another sub named 'GetAddress' which expects 2 parameters -
the UserID & the OrderID. Depending upon the OrderID (& of course, the
UserID), the 'GetAddress' sub will retrieve the billing address &
shipping address pertaining to that OrderID. So even if I create the
dynamic links inside the While(sqlReader.Read) loop as you have
suggested, I still can't access the OrderID outside the While loop. In
order to access the OrderID outside the While loop, I will have to use
Session variables something like this:

Sub Page_Init(....)
iUserID = Request.Cookies("UserID").Value

While(sqlReader.Read)
If (sqlReader.GetValue(0) > 1) Then
iTotalOrders = sqlReader.GetValue(0)
'get the OrderID in Session variable
Session("OrderID") = sqlReader.GetValue(1)
lnkCount = New LinkButton
lnkCount.ID = "lnkCount" & iCount
lnkCount.Text = iCount.ToString
lnkCount.CommandName = iCount
lnkCount.CommandArgument = Session("OrderID")
AddHandler lnkCount.Command, AddressOf CommandCount
pnlLinks.Controls.Add(lnkCount)
iCount = iCount + 1
Session("TotalOrders") = iTotalOrders
Else
pnlLinks.Visible = False
End If
End While

GetAddress(iUserID, CInt(Session("OrderID")))
End Sub

This does serve my purpose & gets the correct billing & shipping
address corresponding to the UserID & OrderID but I did like to avoid
using Session variables.

Can you suggest some way by which I can avoid using Session variables
to store the OrderID?

Gozirra wrote:
> Why build the list outside of the while loop then? Try something like
> this. Since I do not know exactly what data is coming back, its kind
> of hard to determine exactly what all of the possibilities are. But
> hopefully it'll give you an idea if you haven't already gotten
> everything fixed.
>
> While (sqlReader.Read)
> If (sqlReader.GetValue(0) > 1) Then
> iTotalOrder = sqlReader.GetValue(0)
>
> lnkDynamic = new LinkButton()
> lnkDynamic.EnableViewState = true
> lnkDynamic.ID = "lnkDynamic_" + <identifier> 'Identifier
> can be your order# or whatever your want
> lnkDynamic.Text = "SomeText"
> AddHandler lnkDynamic.Command, AddressOf lnkDynamic_Command
>
> lnkDynamic.CommandName = "AnyTextYouWant"
> 'sqlReader.GetValue(1) perhaps
> lnkDynamic.Visible = false
> Me.Controls.Add(lnkDynamic)
> Else
> pnlLinks.Visible = False
> End If
> End While


 
Reply With Quote
 
Gozirra
Guest
Posts: n/a
 
      11-28-2006
Sorry I got really busy at work and wasn't able to check back in. Is
this still an issue for you? If not, everyone just ignore me.

Given the code example you have below, you want to pass the last
orderid retrieved to a subroutine when the loop has completed. So just
pull the last orderid from the commandargument of lnkCount. The last
time through the loop you will have the same value as you have in your
session variable in the code below. This way you do not have to use a
session variable. You could also create a local variable and assign
the value as you iterate the records. That may be a little cleaner and
easier to understand if someone has to come back and look at this
routine later.

wrote:
> Gozirra, the main reason why I want to get the OrderID of each order
> outside the While(sqlReader.Read) loop is because I want to pass the
> OrderID to another sub named 'GetAddress' which expects 2 parameters -
> the UserID & the OrderID. Depending upon the OrderID (& of course, the
> UserID), the 'GetAddress' sub will retrieve the billing address &
> shipping address pertaining to that OrderID. So even if I create the
> dynamic links inside the While(sqlReader.Read) loop as you have
> suggested, I still can't access the OrderID outside the While loop. In
> order to access the OrderID outside the While loop, I will have to use
> Session variables something like this:
>
> Sub Page_Init(....)
> iUserID = Request.Cookies("UserID").Value
>
> While(sqlReader.Read)
> If (sqlReader.GetValue(0) > 1) Then
> iTotalOrders = sqlReader.GetValue(0)
> 'get the OrderID in Session variable
> Session("OrderID") = sqlReader.GetValue(1)
> lnkCount = New LinkButton
> lnkCount.ID = "lnkCount" & iCount
> lnkCount.Text = iCount.ToString
> lnkCount.CommandName = iCount
> lnkCount.CommandArgument = Session("OrderID")
> AddHandler lnkCount.Command, AddressOf CommandCount
> pnlLinks.Controls.Add(lnkCount)
> iCount = iCount + 1
> Session("TotalOrders") = iTotalOrders
> Else
> pnlLinks.Visible = False
> End If
> End While
>
> GetAddress(iUserID, CInt(Session("OrderID")))
> End Sub
>
> This does serve my purpose & gets the correct billing & shipping
> address corresponding to the UserID & OrderID but I did like to avoid
> using Session variables.
>
> Can you suggest some way by which I can avoid using Session variables
> to store the OrderID?
>
> Gozirra wrote:
> > Why build the list outside of the while loop then? Try something like
> > this. Since I do not know exactly what data is coming back, its kind
> > of hard to determine exactly what all of the possibilities are. But
> > hopefully it'll give you an idea if you haven't already gotten
> > everything fixed.
> >
> > While (sqlReader.Read)
> > If (sqlReader.GetValue(0) > 1) Then
> > iTotalOrder = sqlReader.GetValue(0)
> >
> > lnkDynamic = new LinkButton()
> > lnkDynamic.EnableViewState = true
> > lnkDynamic.ID = "lnkDynamic_" + <identifier> 'Identifier
> > can be your order# or whatever your want
> > lnkDynamic.Text = "SomeText"
> > AddHandler lnkDynamic.Command, AddressOf lnkDynamic_Command
> >
> > lnkDynamic.CommandName = "AnyTextYouWant"
> > 'sqlReader.GetValue(1) perhaps
> > lnkDynamic.Visible = false
> > Me.Controls.Add(lnkDynamic)
> > Else
> > pnlLinks.Visible = False
> > End If
> > End While


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Linkbutton does not look like a linkbutton Sathyaish ASP .Net 3 09-08-2005 09:41 AM
Linkbutton does not look like a linkbutton Sathyaish ASP .Net Datagrid Control 1 09-08-2005 08:44 AM
Adding dynamic LinkButton to dynamic table =?Utf-8?B?QkxpVFpXaU5H?= ASP .Net 0 07-27-2005 12:21 AM
Repeator Dynamic Template - bubbleing LinkButton click event to containing Repeator. Pat Sagaser via .NET 247 ASP .Net 0 05-13-2004 04:58 AM
dynamic loading of images by linkbutton clicks sef ASP .Net 4 01-13-2004 09:39 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57