开发者

How can I send an email with autoGenerateEditButton set to True?

开发者 https://www.devze.com 2023-04-07 18:49 出处:网络
We would like an email sent out when an update is made. How can I do this when autogenerateEditButton of gridview set to true?

We would like an email sent out when an update is made.

How can I do this when autogenerateEditButton of gridview set to true?

Here is an example:

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" 
    AutoGenerateColumns="False" DataKeyNames="ID" AllowPaging="True" 
    OnRowDataBound="gvRowDataBound" **onRowUpdated="btnSendEmail_Click"** AutoGenerateEditButton="True">
    <Columns>
 开发者_开发知识库       <asp:BoundField DataField="date_stamp" HeaderText="Entry Date" ReadOnly = "true"
            SortExpression="date_stamp" />
    </Columns>
</asp:GridView>

My email code is on codebehind called sub btnSendEmail_Click().

    Protected Sub btnSendEmail_Click(ByVal sender As Object, ByVal e As GridViewUpdatedEventArgs)
        Dim cnn As SqlConnection
        'Dim param As SqlParameter
        Dim cmd As SqlCommand

        Dim sqlStr As String = ""
        Dim sqlStrD As String = ""

        Dim connStr As String = ConfigurationManager.ConnectionStrings("Database_DBConnectionString").ConnectionString
more - not posted
more - not posted


Instead of having a 'Send Email' button and rely on the user to manually click it, why not create a hook to handle the Gridview's RowUpadated event?

  void GridView1_RowUpdated(Object sender, GridViewUpdatedEventArgs e)
  {

    // Indicate whether the update operation succeeded.
    if(e.Exception == null)
    {
      int index = GridView1.EditIndex;
      GridViewRow row = GridView1.Rows(index);
      //example to pull the data from a cell to send it to your function
      SendEmail(row.Cells(0).Text);

      Message.Text = "Row updated successfully. Email Sent!";
    }
    else
    {
      e.ExceptionHandled = true;
      Message.Text = "An error occurred while attempting to update the row. No email sent.";
    }

  }

VB code:

Private Sub GridView1_RowUpdated(sender As Object, e As GridViewUpdatedEventArgs)

    ' Indicate whether the update operation succeeded.
    If e.Exception Is Nothing Then
        Dim index As Integer = GridView1.EditIndex
        Dim row As GridViewRow = GridView1.Rows(index)
        'example to pull the data from a cell to send it to your function
        SendEmail(row.Cells(0).Text)

        Message.Text = "Row updated successfully. Email Sent!"
    Else
        e.ExceptionHandled = True
        Message.Text = "An error occurred while attempting to update the row. No email sent."
    End If

End Sub

EDIT to comment I used the parameter as an example to show you how to pull the value from the gridview if you wanted to pass it. Something like this:

SendEmail

Protected Sub SendEmail(ByVal RowNumber as Integer)
    Try
        Const ToAddress As String = "ThierEmail@domain.com"
        Const FromAddress As String = "YourEmail@domain.com"
        Dim Subject As String = "Row Updated"

        Dim mm As New MailMessage(FromAddress, ToAddress)
        mm.Subject = Subject
        mm.IsBodyHtml = False

        mm.Priority = MailPriority.High
        mm.Body =  String.Format("Row ID {0} was updated.",RowNumber)

        'Send the email
        Dim smtp As New SmtpClient()
          smtp.Send(mm)
    Catch ex As Exception
        'You should catch your error here
        Throw ex
    End Try
End Sub


You will need to have btnSendEmail_Click() handle the GridView's RowEditing event. In your ASP code, you should have:

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
     OnRowEditing="btnSendEmail_Click"
     ...
     AutoGenerateEditButton="True">

Then in your codebehind:

Protected Sub btnSendEmail_Click(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
    '...code to send email...
0

精彩评论

暂无评论...
验证码 换一张
取 消