最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Call JavaScript function while the asp:LinkButton been clicked - Stack Overflow

programmeradmin1浏览0评论

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>

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>
Share Improve this question edited May 16, 2013 at 7:50 Samer_Azar 4332 gold badges9 silver badges32 bronze badges asked Jun 20, 2011 at 5:28 Jin YongJin Yong 43.8k72 gold badges144 silver badges194 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 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>

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.

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

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

发布评论

评论列表(0)

  1. 暂无评论