Results 1 to 4 of 4

Thread: dynamic server control used 100s of times on one page - question

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Posts
    10

    dynamic server control used 100s of times on one page - question

    Hi all

    I am working on a server control that will be a basic "add to shopping cart" button. This button will be used anywhere from 0 to 75 times on one page ie on a search results page, you would get one button for each result.

    The problem I am having is handling the events that the cart button raises. The way I have it now, since the buttons are added dynamically to the page, I have to manually attach the handler using the addHandler method. I do this by looping through the page controls and testing each controls type to see if it is one of my cart buttons. When I find a match, I attach the handler and point it at my pages "cartbutton_clicked" method. This works perfectly. My question is - is there a better way? I don't like the idea of having to loop through the page controls every single time to find the buttons. It will get worse when I need to have custom hyperlinks and other misc controls that will be created dynamically and for which I need to add event handlers. The list of controls to loop through gets longer and longer, and I am quickly looking at close to 200 calls to addHandler().

    Am I doing this completely wrong? Any better ideas out there?

    Any advice is appreciated!

  2. #2
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: dynamic server control used 100s of times on one page - question

    Is it just a button or does it do more?

    I just add buttons with a CommandName containing the ID of the item that needs to be added to the basket. All buttons use the same event handler which grabs the ID from the CommandName property and then adds that item to the basket.

    This way you can create the button within the templates for DataGrids, Repeaters etc.

    Give a shout if you need any other help.

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Posts
    10

    Re: dynamic server control used 100s of times on one page - question

    Quote Originally Posted by dj4uk
    Is it just a button or does it do more?

    I just add buttons with a CommandName containing the ID of the item that needs to be added to the basket. All buttons use the same event handler which grabs the ID from the CommandName property and then adds that item to the basket.

    This way you can create the button within the templates for DataGrids, Repeaters etc.

    Give a shout if you need any other help.

    DJ
    Its just a button, but when I click it I need to pass two different pieces of information to the event handler. I do this by adding attributes to the button, then in the server control code when the button is clicked, I raise another event. This is the event that I manually attach the handlers for.

    Does your button inherit from WebControl or from ImageButton? Mine inherits from WebControl, and I am thinking this might be part of the problem!

  4. #4
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: dynamic server control used 100s of times on one page - question

    If you need more than one parameter passed to the handle use the CommandArgument property - the Button, LinkButton and ImageButton controls all have the CommandName and CommandArgument properties.

    All web controls inherit from WebControl.

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width