Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to Call javascript function by clicking on Gridview link button

How to Call javascript function by clicking on Gridview link button

Scheduled Pinned Locked Moved ASP.NET
javascripthelptutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cheguri
    wrote on last edited by
    #1

    Dear friends, I have a linkbutton in gridview. When I click on that linkbutton, I need to get some data based on the row which I clicked and this data to be sent to a javascript function. Please help me how to achieve this. It's very urgent. Regards, Cheguri.

    L 1 Reply Last reply
    0
    • C cheguri

      Dear friends, I have a linkbutton in gridview. When I click on that linkbutton, I need to get some data based on the row which I clicked and this data to be sent to a javascript function. Please help me how to achieve this. It's very urgent. Regards, Cheguri.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      For that you need to use "ItemTemplate" column for your Link Button and write according to following code.

      You just need to use LinkButton instead of button. HTH

      Jinal Desai - LIVE Experience is mother of sage....

      N 1 Reply Last reply
      0
      • L Lost User

        For that you need to use "ItemTemplate" column for your Link Button and write according to following code.

        You just need to use LinkButton instead of button. HTH

        Jinal Desai - LIVE Experience is mother of sage....

        N Offline
        N Offline
        NeverHeardOfMe
        wrote on last edited by
        #3

        This isn't going to help with calling a funciton dependent upon the underlying data row.... Assuming you want to create a specific Javascript call for each row, dependent on (say) the row ID of the underlying dataview, you would do better to use a ButtonColumn: <asp:ButtonColumn ButtonType="LinkButton" CommandName="doesnt_matter" Text="whatever" /> and add your JavaScript fucntion in the page's head: function fname(r) { alert('You clicked row with ID# ' + r); } and bind this in the ItemDataBound event: If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then Dim dr As DataRow = e.Item.DataItem.Row Dim js As String Dim btn As LinkButton = e.Item.Cells(X).Controls(0) ' replace "X" by the appropriate column index js = "fname(" & CStr(dr("ID")) & ")"; return false;" btn.Attributes.Add("onclick", js) End If NB the extra "return false" statement - this prevents the link-button from posting the entire form back to the server. btw, OP: re "It's very urgent." Please don't put this sort of thing in your posts. That is no-one's problem but yours.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups