我们希望在更新时发送电子邮件。
如果gridview的autogenerateEditButton设置为true,我该怎么做?
以下是一个例子:
<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>
我的电子邮件代码位于名为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
答案 0 :(得分:0)
您需要btnSendEmail_Click()
处理GridView的RowEditing
event。在您的ASP代码中,您应该:
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
OnRowEditing="btnSendEmail_Click"
...
AutoGenerateEditButton="True">
然后在你的代码隐藏中:
Protected Sub btnSendEmail_Click(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
'...code to send email...
答案 1 :(得分:0)
为什么不创建一个钩子来处理Gridview的RowUpadated事件,而不是使用“发送电子邮件”按钮并依赖用户手动点击它?
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代码:
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
编辑评论 我使用参数作为示例向您展示如果要传递它,如何从gridview中提取值。像这样:
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