Referencing ATTRIBUTES property of a datagrid commandbutton

Posted on 2005-05-09
Last Modified: 2008-03-06
I am trying to open a new child window and pass parameters back and forth. I understand that in order to accomplish this from a button, the following code could be used to call a custom javascript procedure that opens the new window:

      Button1.Attributes("onclick") = "javascript:OpenDialog()"

Now for my problem. How do I set the ATTRIBUTES property for a command button within a datagrid? I created a datagrid, added a template column and attached a commandbutton to it, named the commandbutton uniquely, and can't seem to figure out how to reference it (I'm a novice with using datagrid template columns, and ASP.NET in general). Here was the line of code I was playing with (it obviously doesn't work):

      dg.Columns(0).cmdSelect.Attributes("onclick") = "javascript:OpenDialog()"

If you need more info, please reply. Thanks in advance for any assistance.
Question by:big1975foot
    LVL 14

    Accepted Solution

    Hey BigFoot,

    You cannot access the button directly. At run-time you should use DataGrid's ItemDatabound event and place there code like this (writing from directly, not pasting from a working code, so syntax errors may ocure):

    If e.Item.ItemType = ListItemType.Item Or _
                 e.Item.ItemType = ListItemType.AlternatingItem Then
       Dim sCmd as System.Web.UI.WebControls.Button
       sCmd=CType(e.Item.Cells(0).FindControl("cmdSelect"), System.Web.UI.WebControls.Button)
       sCmd.Attributes("onclick") = "javascript:OpenDialog()"
    End if

    This code assumes that button, named "cmdSelect" is put in the first (0) column of the datagrid and this column is a template column.

    Hope this helps

    Author Comment

    Hey Ramunickas, It works great! Thanks for your assistance.
    LVL 14

    Expert Comment

    Very pleased to hear that :)

    Good luck!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now