You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将HTML锚点标签(Anchor Tag)关联到后端C#函数?

Alright, let's figure out why your anchor tag isn't triggering that server-side C# method, and fix it up.

The main issue here is that you're mixing two different ASP.NET paradigms: Razor Pages (you're using asp-page) and Web Forms (you're using runat="server" and onserverclick). These don't play nicely together, so let's pick the right approach based on what framework you're actually using.

If you're working with ASP.NET Razor Pages (most common for newer projects)

Razor Pages uses handler methods instead of onserverclick. Here's how to wire this up properly:

  1. Update your PageModel class (e.g., Notifications.cshtml.cs) with a POST handler (since you're modifying data with TRUNCATE, POST is the correct HTTP method):
public IActionResult OnPostRemoveCount()
{
    var cs = Database.Database.Connector();
    using var con = new NpgsqlConnection(cs);
    con.Open();
    var sql = "TRUNCATE TABLE counter ";
    using var cmd = new NpgsqlCommand(sql, con);
    cmd.Prepare();
    cmd.ExecuteNonQuery();
    
    // Redirect back to the Notifications page after the operation
    return RedirectToPage("/Notifications");
}
  1. Adjust your frontend code to trigger this handler. Since we're using POST, wrap a button (styled to look like a link) in a form:
<form method="post" asp-page-handler="RemoveCount">
    <button type="submit" class="btn btn-link p-0">
        <h5 class="font-italic text-left">Notifications</h5>
    </button>
</form>

If you absolutely need to use an <a> tag instead of a button, you can use JavaScript to submit the form:

<form id="clearCounterForm" method="post" asp-page-handler="RemoveCount">
    @Html.AntiForgeryToken() <!-- Required for Razor Pages POST requests -->
</form>

<a href="#" onclick="document.getElementById('clearCounterForm').submit(); return false;">
    <h5 class="font-italic text-left">Notifications</h5>
</a>

If you're working with ASP.NET Web Forms (older framework)

Web Forms does support runat="server" and onserverclick, but you don't need the asp-page attribute here. Instead:

  1. Fix your frontend anchor tag:
<a runat="server" onserverclick="RemoveCount_Click">
    <h5 class="font-italic text-left">Notifications</h5>
</a>
  1. Update your code-behind method to handle the click and redirect:
protected void RemoveCount_Click(object sender, EventArgs e)
{
    var cs = Database.Database.Connector();
    using var con = new NpgsqlConnection(cs);
    con.Open();
    var sql = "TRUNCATE TABLE counter ";
    using var cmd = new NpgsqlCommand(sql, con);
    cmd.Prepare();
    cmd.ExecuteNonQuery();
    
    // Redirect to your Notifications page
    Response.Redirect("~/Notifications.aspx");
}

The key takeaway is to stick to one framework's conventions—don't mix Razor Pages tag helpers with Web Forms server-side attributes, and you'll get this working smoothly.

内容的提问来源于stack exchange,提问作者anonD

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 03:43:15