2

Just wondering, could it be possible call a JavaScript under asp:LinkButton while it been clicked.

Example: I have the following code that I would like it to be calling a JavaScript function (test()), when it been clicked. How could I do it?

    <asp:ListView ID="lvTest" runat="server" DataSourceID="dsTest" DataKeyNames="TestID"
    OnItemCommand="lvTest_ItemCommand">
    <LayoutTemplate>
      <ul style="color:#fe8113;">
        <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
      </ul>
    </LayoutTemplate>
    <ItemTemplate>
      <li>
        <asp:LinkButton runat="server" CausesValidation="true" CommandName="" CssClass="orange"
          Text='<%# Eval("TestName")%>'/>
      </li>
    </ItemTemplate>
  </asp:ListView>
Samer_Azar
  • 413
  • 2
  • 9
  • 32
Jin Yong
  • 42,698
  • 72
  • 141
  • 187

4 Answers4

2

You can also add the required attribute as OnClientClick on the design itself, instead of binding it in the code behind.

 <asp:ListView ID="lvTest" runat="server" DataSourceID="dsTest" DataKeyNames="TestID"
    OnItemCommand="lvTest_ItemCommand">
    <LayoutTemplate>
      <ul style="color:#fe8113;">
        <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
      </ul>
    </LayoutTemplate>
    <ItemTemplate>
      <li>
        <asp:LinkButton runat="server" CausesValidation="true" CssClass="orange" OnClientClick="return test();"
          Text='<%# Eval("TestName")%>'/>
      </li>
    </ItemTemplate>
  </asp:ListView>

Now Javascript function can be added

<script language="javascript" type="text/javascript">
function test()
{
    //add the required functionality
    alert('Hi');
}
</script>
suryakiran
  • 1,976
  • 25
  • 41
1

You need to assign id to you linkbutton for following code to work which is missing in your code.

 protected void GridView1_RowDataBond(object source, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton btnAlertStatus = (LinkButton)e.Row.FindControl("btnAlertStatus");

                btnAlertStatus.Attributes.Add("onclick", "alert('test'); ");
        }
    }

You can attach javascript to the button control in the GridView1_RowDataBond event easily as i show in above code.

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
1
<asp:LinkButton ID= "lnkButton" runat="server" CausesValidation="true" CommandName="" CssClass="orange"
          Text='<%# Eval("TestName")%>'/>

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

 // your code goes here ..

}
</script>

In code behind file write this

protected void Page_Load(object sender, EventArgs e)
{
    lnkButton.Attributes.Add("onclick", "return test()");
}
Taterhead
  • 5,763
  • 4
  • 31
  • 40
Bibhu
  • 4,053
  • 4
  • 33
  • 63
0

Adding the onclientclick="JsFunctionNameHere()" to the <asp:LinkButton> worked like a charm for me.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
  • A good answer will always include an explanation why this would solve the issue, so that the OP and any future readers can learn from it. – Tyler2P Feb 13 '22 at 18:10