How to launching email client on LinkButton click event?

我是研究僧i 提交于 2019-11-29 14:03:18

Consider that the mailto functionality is a function that needs to happen client side. You are going to need javascript to do it. Depending on when you want the mailto to happen you have two choices.

If you want it to happen as soon as the LinkButton is clicked then just add to the LinkButton's OnClientClick event:

<asp:LinkButton runat="server" ID="btnEmail" Text="Send Email"
    OnClientClick="window.open('mailto:someone@somewhere.com','email');">
</asp:LinkButton>

If you want it to happen AFTER the server side code has run your are going to have wire up the javascript event to run when the new page starts up:

// At the end of your LinkButton server side OnClick event add the following code:
ClientScript.RegisterStartupScript(this.GetType(), "FormLoading",
    "window.open('mailto:someone@somewhere.com','email');", true);

Hope that helps.

I've accomplished this using the OnClientClick event of the LinkButton.

You can use:

<asp:LinkButton runat="server" ID="btnEmail" Text="Send Email"
    OnClientClick="window.location.href = 'mailto:someone@something.com?subject=Email Subject';">
</asp:LinkButton>

You can also do this in code, in case you need to load an email address from a database or something:

btnEmail.OnClientClick = "window.location.href = 'mailto:someone@something.com?subject=Email Subject';";
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!