Home > Coding > Can’t access CommandArgument (LinkButton) in Javascript

Can’t access CommandArgument (LinkButton) in Javascript

Problem:
Cannot access CommandArgument value of ASP LinkButton from Javascript function

Solution:
Instead going for CommandArgument, there is a rather simple workaround to this. LinkButton has a “title” attribute and it can be used to pass values to Javascript function.

<script type="text/javascript">
function demo(sender){
      alert(sender.title);
}
</script>
<asp:LinkButton ID="LinkButton1" runat="server"
OnClientClick="javascript:return demo(this);"
CausesValidation="false"
CommandName="name"
Text='<%# Eval("[ name]") %>'
title='<%# Eval("[ name]") %>'
CommandArgument='<%# Eval("[Id]") %>'>click</asp:LinkButton>

Source: http://forums.asp.net/p/1414937/3121669.aspx#3121669

Categories: Coding Tags: , ,
  1. KennyKen
    September 1, 2009 at 9:50 pm | #1

    Genius, thanks!

    The property doesnt need to be called ‘title’ either, it can be ‘id’ or whatever you choose!

    Ta,

    Ken

  2. September 4, 2009 at 2:32 pm | #2

    @ken, thanks!

  1. No trackbacks yet.