1

Currently my application is able to open up a Outlook email with a default recipient once a user has clicked Image button which is located inside a Grid View. The aspx code for this is as follows;

<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" />

I have a Bound Field Data Field with the ID "custEmail" which shows a customers email in the Data Grid if one is linked to their account. The code used to open the Outlook application was taken from this question and the only addition I have added is the address the email is being sent to;

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

What I would like to do is replace the sample email with the email showing for the customer in the data grid. When I try to do this like this;

omsg.To = custEmail

The code behind doesn't find this Bound Field Data Field.

How do I allow my code-behind to see the "custEmail" BoundField so I can popular "omsg.To" with a customers email address?

Community
  • 1
  • 1
JPM
  • 189
  • 1
  • 3
  • 14

1 Answers1

0

I suggest you to pass Customer Email address as CommandArgument in ImageButton and add another property 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" />

Next add OnRowCommand event in GridView:

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

In your code-behind:

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
Keval Gangani
  • 1,326
  • 2
  • 14
  • 28
  • Hi there. I have done as suggested but am receiving the following error; "BC30456: 'openClient' is not a member of 'ASP.customerupdate_aspx'.". Any ideas? – JPM Apr 29 '15 at 15:28
  • 1
    Just done that. It's working but is displaying the first field in the database "customerID", in the "To" section. The field I need it to show is "custEmail". – JPM Apr 29 '15 at 15:34
  • Update `CommandArgument='<%# Eval("custEmail") %>'` in ImageButton and then try. – Keval Gangani Apr 29 '15 at 15:36