Sections
spotlight
     
     
WWWCoder.com Resource Directory

Adding Attributes to an Object Within a Datalist
11/9/2003 9:17:22 AM

In this article we cover the difference between adding attributes to objects directly and objects contained within a datalist. In this specific example we will add some Javascript behind an image button for a confirmation dialog.

On my 4ASPX.Net I wanted to make the user interface a bit easier for folks to use. One of the things I wanted to add was a message box to let people know that if they wanted to download an item that they need to be logged into the site first. This wasn't obvious to people at first, even though I would trap the event and display the logon on the left side of the site, it still wasn't obvious to people. My solution was to add a confirmation JScript alert box to the button.

Adding the attribute to the image button is a rather simple matter. On the product details page it is just one image button on the page which is added in the following way:

'first check for an authenticated user
'if they're not logged in then display the message.
If Not Request.IsAuthenticated Then
  'now add the JScript.
  btnMainBuy.Attributes.Add("onClick", "javascript:return _
      confirm('You must be logged into " _
      "this site in order to download or purchase items!');")
End If

By adding this code I now have the desired effect of displaying a message.

In addition to having a button for the download on the details page, I also list items in the directory in a data list control. This data list is bound to a data reader to display items for a particular category or by doing a keyword search. When changing items within a data list you have to deal with them a bit differently than going directly at the object ID and adding attributed like we did previously. Say I have my form set up the following way.

<asp:DataList id="DataList2" runat="server"
RepeatDirection="Horizontal"
RepeatLayout="Flow">
<ItemTemplate>
<asp:ImageButton ID="btnBuy" Runat="server"
AlternateText="Download or Buy Now!"
CommandArgument='<%# DataBinder.Eval(Container.DataItem,"ItemID") %>'
CommandName="Buy"
ImageUrl='Buy.Gif'
ImageAlign="Left">
</asp:ImageButton>
</ItemTemplate>
</asp:DataList>

Here what we're going to do is modify the image button while the data list is being bound to our data reader. Basically as each item is added to our data list we will add the JScript code behind the image button with our confirmation.

Private Sub DataList2_ItemDataBound(ByVal sender As Object, ByVal e As _
   System.Web.UI.WebControls.DataListItemEventArgs) _
   Handles DataList2.ItemDataBound
  'first check and see if they are logged on.
  If Not Request.IsAuthenticated Then
    'now check if this is an Item within the datalist.
    If e.Item.ItemType = ListItemType.Item Then
      'prepare a new image object and assign our datalist image object.
      Dim myImage As ImageButton = CType(e.Item.FindControl("btnBuy"), ImageButton)
      'add the JScript behind the button.
      myImage.Attributes.Add("onClick", "javascript:return confirm('You must be " & _
         "logged into this site in order to download or purchase items!');")
    End If
  End If
End Sub

Now when someone clicks on the button, it will present a JScript confirmation and the user can either select "Yes" to continue with our event or cancel it.

By: Patrick Santry, Microsoft MVP (ASP/ASP.NET), developer of this site, author of books on Web technologies, and member of the DotNetNuke core development team. If you're interested in the services provided by Patrick, visit his company Website at Santry.com.

Related Articles
   Related Document Binding the Contents of a Directory to a Datalist


Page Options:
format for printing  Format for Printer
email article  Email Page
add to your favorites   Add to Favorites
How would you rate the quality of this content?
Poor - - Excellent
Comments?
Overall Rating:
Comments Left:
Left on 7/12/2008 4:48:43 AM by Anonymous
Comments: bekar
Left on 10/20/2006 2:38:00 AM by Anonymous
Comments: Need to add attributes to an ImageButton predefined in an ItemTemplate.
Left on 11/19/2005 4:49:49 PM by Anonymous
Comments: The following needs to be added in order to write to every item in the datalist: Or e.Item.ItemType = ListItemType.AlternatingItem
Left on 11/19/2005 4:38:26 PM by Anonymous
Comments: My comment is from 11/17.  I just realized that the javascript is only written to the 1st item in the datalist...any ideas?
No ratings available.
Left on 11/17/2005 11:08:49 AM by Anonymous
Comments: I've been looking for this...thank you!
No ratings available.
Left on 11/18/2004 3:52:18 PM by Anonymous
Comments: it did not work with udf's. could you tell me why?
No ratings available.
Left on 9/30/2004 4:42:46 PM by Anonymous
Comments: just the stuff I needed!
Left on 7/27/2004 6:18:22 PM by Anonymous
Comments: Thank you very much!
Left on 7/19/2004 5:24:30 PM by Anonymous
Comments: thanks you
Left on 12/2/2003 2:14:29 PM by Anonymous
Comments:

Left on 12/2/2003 11:58:30 AM by Anonymous
Comments: It would have also been good to show and example with including a DataBinder.Eval in the javascript
     
     

 

     
     

 


 


Digg This
 


DotNetNuke Platinum Benefactor

     
     

Other family network sites: santry.com - katieandkarleigh.com

Powered by 

 

     
Copyright 20010 - Santry Technology Solutions, Box 172, Girard, PA 16417, (814) 774-0970
Privacy Statement | Terms Of Use