如何从数据网格中获取用户电子邮件地址并将其填充到 asp.net 中的电子邮件中

How to take users email address from a datagrid and populate it in an email in asp.net

目前,一旦用户单击位于网格视图内的图像按钮,我的应用程序就能够打开带有默认收件人的 Outlook 电子邮件。其aspx代码如下;

<asp:ImageButton ID="LinkEmail" OnClick="openClient" CommandArgument='<%# Eval("customerId") %>'
                                            ToolTip="Send a Email to this customer" ImageUrl="~/SiteElements/Images/email_icon.jpg" 
                                            Width="16px" Height="16px" runat="server" />

我有一个 ID 为 "custEmail" 的绑定字段数据字段,它会在数据网格中显示客户的电子邮件地址(如果有链接到他们的帐户)。用于打开 Outlook 应用程序的代码取自 this question,我唯一添加的是电子邮件发送到的地址;

Protected Sub openClient(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim Out1 As Object
    Out1 = CreateObject("Outlook.Application")
    If Out1 IsNot Nothing Then
        Dim omsg As Object
        omsg = Out1.CreateItem(0) '=Outlook.OlItemType.olMailItem'
        omsg.To = "sample.email@test.com"
        'set message properties here...'
        omsg.Display(True)

    End If

我想要做的是将示例电子邮件替换为数据网格中为客户显示的电子邮件。当我尝试这样做时;

omsg.To = custEmail

后面的代码没有找到这个绑定字段数据字段。

如何让我的代码隐藏看到 "custEmail" BoundField,以便我可以 "omsg.To" 使用客户的电子邮件地址?

我建议您在 ImageButton 中将客户电子邮件地址作为 CommandArgument 传递并添加另一个 属性 CommandName.

<asp:ImageButton ID="LinkEmail" OnClick="openClient" 
       CommandName="SendEmail"
       CommandArgument='<%# Eval("CustomerEmail") %>' 
       ToolTip="Send a Email to this customer" 
       ImageUrl="~/SiteElements/Images/email_icon.jpg" 
       Width="16px" Height="16px" runat="server" />

接下来在 GridView 中添加 OnRowCommand 事件:

<asp:GridView ID="GridView1" runat="server" 
        OnRowCommand="GridView1_RowCommand">
    </asp:GridView>

在您的代码隐藏中:

Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs)

    If e.CommandName = "SendEmail" Then
        Dim CustomerEmailAddress = e.CommandArgument

        Dim Out1 As Object
        Out1 = CreateObject("Outlook.Application")
        If Out1 IsNot Nothing Then
            Dim omsg As Object
            omsg = Out1.CreateItem(0) '=Outlook.OlItemType.olMailItem'
            omsg.To = CustomerEmailAddress
            'set message properties here...'
            omsg.Display(True)

        End If
    End If

End Sub