2011-09-26 44 views
-1

我們希望在更新時發送電子郵件。如何發送autoGenerateEditButton設置爲True的電子郵件?

如何在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

取代具有「發送電子郵件」按鈕,並依靠用戶手動點擊它,爲什麼不創建:

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" 
    OnRowEditing="btnSendEmail_Click" 
    ... 
    AutoGenerateEditButton="True"> 
在你的代碼隱藏

然後一個鉤子來處理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 = "[email protected]" 
     Const FromAddress As String = "[email protected]" 
     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 
+0

謝謝zeroef;你很棒。一個問題。我發現你在SendEmail裏有一個參數 - SendEmail(row.Cells(0).Text)。這是否意味着我需要添加一個參數到我的btnSendEmail_Click子?如果是的話,我應該怎麼稱呼它? – Kenny

+0

@Kenny - 已更新的答案 – zeroef

0

你需要有btnSendEmail_Click()處理GridView的RowEditing event。在你的ASP代碼,你應該有:

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

賈斯汀,我不知道怎麼感謝你纔好迴應。我必須有這兩個參數嗎?我收到以下錯誤消息:\t未爲'Protected Sub btnSendEmail_Click(sender As Object,e As System.Web.UI.WebControls.GridViewEditEventArgs)'的參數'e'指定的參數''。 \t \t未爲'Protected Sub btnSendEmail_Click(sender As Object,e As System.Web.UI.WebControls.GridViewEditEventArgs)'的參數'sender'指定的參數''。 – Kenny

+0

此外,不應該onrowUpdating,因爲我想更新?我是新手,所以我只是好奇。 – Kenny

+0

這取決於您想要發送電子郵件的時間。 RowUpdating事件對我來說聽起來更合適。至於你的錯誤信息,你可以嘗試使'e'變成一個普通的'EventArgs'對象。 –

相關問題