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

如何将ASP.Net中Label4的Text值传入WhatsApp链接的phone参数?

解决方法

方法1:直接使用服务器端输出语法

将链接中的电话号码部分替换为服务器端输出表达式,直接获取Label的Text值:

<asp:Label ID="Label4" runat="server" Text="+905554443322" Font-Size="Small"></asp:Label>

<a class="float" href="https://api.whatsapp.com/send?phone=<%= Label4.Text %>" target="_blank"> 
  <i class="fa fa-whatsapp my-float"></i>
</a>

注:如果该代码位于数据绑定控件(如GridView)内部,需改用<%# Label4.Text %>绑定语法。

方法2:后台代码动态设置链接

给a标签添加runat="server"属性,在后台代码中拼接完整链接:

前端代码

<asp:Label ID="Label4" runat="server" Text="+905554443322" Font-Size="Small"></asp:Label>

<a id="whatsappLink" class="float" runat="server" target="_blank"> 
  <i class="fa fa-whatsapp my-float"></i>
</a>

后台C#代码

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        whatsappLink.HRef = $"https://api.whatsapp.com/send?phone={Label4.Text}";
    }
}

方法3:JavaScript动态修改链接

通过前端脚本获取Label的文本值,再拼接为完整的WhatsApp链接:

<asp:Label ID="Label4" runat="server" Text="+905554443322" Font-Size="Small"></asp:Label>

<a id="whatsappLink" class="float" href="#" target="_blank"> 
  <i class="fa fa-whatsapp my-float"></i>
</a>

<script>
window.onload = function() {
    const phoneNumber = document.getElementById('<%= Label4.ClientID %>').innerText;
    const link = document.getElementById('whatsappLink');
    link.href = `https://api.whatsapp.com/send?phone=${phoneNumber}`;
}
</script>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 20:25:39