Linkbutton

Last post 04-27-2009 7:21 AM by NC01. 4 replies.

Sort Posts:

  • Linkbutton

    04-24-2009, 2:34 AM
    • Member
      point Member
    • suz
    • Member since 02-02-2009, 7:20 AM
    • Posts 54

    Hi all,

    I wrote a javascript function and i call on the click event of linkbutton(LinkButton2).I want the text of that linkbutton .How to achieve using javascript?Anyone pls give a solution.
    Thanks

    <asp:GridView

    ID="GridView1" runat="server">
    <asp:TemplateField HeaderText="Name" SortExpression="P_NAME">
    <ItemTemplate>
    <img src="images/open.gif" />
    <asp:LinkButton ID="LinkButton2" runat="server" OnClientClick="javascript:return addtab();" CausesValidation="false"
    CommandName="name" Text='<%# Eval("[ name]") %>' CommandArgument='<%# Eval("[Id]") %>'></asp:LinkButton>

    </ItemTemplate>
    </asp:TemplateField>
    </asp:GridView>


    <script language="javascript" type="text/javascript">
    function addtab(){

    alert("begin");
    var tabctrl1 = window.parent.igtab_getTabById("Tab2");
    tabctrl1.Tabs[2].setEnabled(true);
    tabctrl1.Tabs[2].setVisible(true);
    var b = document.getElementById("LinkButton2");
    alert(b.innerText); //this not working
    alert("end");

    }
    </script>
      function addtab(){ alert("begin"); var tabctrl1 = window.parent.igtab_getTabById("Tab2"); tabctrl1.Tabs[2].setEnabled(true); tabctrl1.Tabs[2].setVisible(true); var b = document.getElementById("LinkButton2"); alert(b.innerText); //this not working alert("end"); }

  • Re: Linkbutton

    04-24-2009, 3:09 AM
    • Star
      8,437 point Star
    • Steelymar
    • Member since 01-28-2009, 9:39 AM
    • Bulgaria
    • Posts 1,248

    use this :

      <asp:LinkButton ID="LinkButton2" runat="server" OnClientClick="javascript:return addtab(this);" CausesValidation="false"
    CommandName="name" Text='<%# Eval("[ name]") %>' CommandArgument='<%# Eval("[Id]") %>'></asp:LinkButton>

     

      function addtab(sender){

    alert(sender.value);

    Regards,
    Stefan Uzunov
  • Re: Linkbutton

    04-24-2009, 10:50 AM
    Answer
    • All-Star
      74,705 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 13,868

    Steelymar:

    use this :

      <asp:LinkButton ID="LinkButton2" runat="server" OnClientClick="javascript:return addtab(this);" CausesValidation="false"
    CommandName="name" Text='<%# Eval("[ name]") %>' CommandArgument='<%# Eval("[Id]") %>'></asp:LinkButton>

     

      function addtab(sender){

    alert(sender.value);

    Sorry but there is no "value" property in that control.

    That is because this: var b = document.getElementById("LinkButton2") will not work for controls in a Grid since the ID becomes something like: "GridView1_ctl02_LinkButton2" when the Grid is rendered.

    The easies way to fix that is by using the this pointer:
    <asp:LinkButton ID="LinkButton2" runat="server" OnClientClick="javascript:return addtab(this);" ...

    Change your JavaScript accordingly:
    <script type="text/javascript">
    <!--
    function addtab(linkButtonRef)
    {
     //alert('begin');
     var tabctrl1 = window.parent.igtab_getTabById('Tab2');
     tabctrl1.Tabs[2].setEnabled(true);
     tabctrl1.Tabs[2].setVisible(true);

     // var b = document.getElementById('LinkButton2');
     alert(linkButtonRef.innerText); //this not working
     //alert('end');
    }
    // -->
    </script>

    Also note that you are using the innerText property wich will only work in Internet Explorer.

    var elementValue = '';

    if ( elementRef.textContent )
    {
     // Firefox...
     elementValue = elementRef.textContent;
    }
    else if ( elementRef.innerText )
    {
     // IE...
     elementValue = elementRef.innerText;
    }
    else
    {
     // Default...
     elementValue = elementRef.innerHTML;
     var regExp = /<\/?[^>]+>/gi;

     elementValue = elementValue.replace(regExp, '');
    }

    NC...

  • Re: Linkbutton

    04-24-2009, 2:21 PM
    Answer
    • Star
      8,437 point Star
    • Steelymar
    • Member since 01-28-2009, 9:39 AM
    • Bulgaria
    • Posts 1,248

    Yes, you are right. I am sory for the oversight.

    After short investigation I found that LinkButton have title attribute and it can be used to pass values to function:

    <script type="text/javascript">

    function addtab(sender){

          alert(sender.title);

    }

    </script>

    <asp:LinkButton ID="LinkButton2" runat="server" OnClientClick="javascript:return addtab(this);" CausesValidation="false"
    CommandName="name" Text='<%# Eval("[ name]") %>'  title='<%# Eval("[ name]") %>' CommandArgument='<%# Eval("[Id]") %>'></asp:LinkButton>
     

    Thank you NC01 for the redress.

     

    Regards,
    Stefan Uzunov
  • Re: Linkbutton

    04-27-2009, 7:21 AM
    • All-Star
      74,705 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 13,868

    Steelymar:

    Yes, you are right. I am sory for the oversight.

    After short investigation I found that LinkButton have title attribute and it can be used to pass values to function:

    <script type="text/javascript">

    function addtab(sender){

          alert(sender.title);

    }

    </script>

    <asp:LinkButton ID="LinkButton2" runat="server" OnClientClick="javascript:return addtab(this);" CausesValidation="false"
    CommandName="name" Text='<%# Eval("[ name]") %>'  title='<%# Eval("[ name]") %>' CommandArgument='<%# Eval("[Id]") %>'></asp:LinkButton>
     

    Thank you NC01 for the redress.

    An asp:LinkButton does NOT have a "title" property. It has a ToolTip property, but I certainly would never use that to pass values to a function. That property is for displaying info to the user. You need to use either the textContent property (Firefox), the innerText property (IE), or the innerHTML property (works on all browsers).

    NC...

Page 1 of 1 (5 items)