当 asp:LinkBut​​ton 被点击时调用 JavaScript 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6407133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:34:05  来源:igfitidea点击:

Call JavaScript function while the asp:LinkButton been clicked

c#javascriptjqueryasp.net.net

提问by Jin Yong

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

只是想知道,是否可以在单击时在 asp:LinkBut​​ton 下调用 JavaScript。

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?

示例:我有以下代码,我希望它在被单击时调用 JavaScript 函数 (test())。我怎么能做到?

    <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>

回答by suryakiran

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

您还可以在设计本身上添加所需的属性作为 OnClientClick,而不是在后面的代码中绑定它。

 <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

现在可以添加Javascript功能

<script language="javascript" type="text/javascript">
function test()
{
    //add the required functionality
    alert('Hi');
}
</script>

回答by Pranay Rana

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

您需要为您的链接按钮分配 id 才能使您的代码中缺少的以下代码正常工作。

 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_RowDataBondevent easily as i show in above code.

GridView1_RowDataBond正如我在上面的代码中所示,您可以轻松地将 javascript 附加到事件中的按钮控件。

回答by Bibhu

<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()");
}