2011-09-26 9 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()라는 코드 숨김에 있습니다. 당신은 btnSendEmail_Click() 가지고있는의 GridView의 RowEditing event을 처리해야합니다

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

감사합니다. 너는 위대하다. 하나의 질문. SendEmail - SendEmail (row.Cells (0). 텍스트) 안에 param이 있음을 확인합니다. 내 btnSendEmail_Click 하위 매개 변수를 추가해야합니까? 그렇다면 무엇을 불러야합니까? – Kenny

+0

@Kenny - 업데이트 된 답변 – zeroef

0

. 당신의 ASP 코드에서, 당신은해야합니다

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

저스틴, 난 당신의 응답을 충분히 감사 할 수 없다. 나는이 두 가지 주장을해야합니까? 다음과 같은 오류 메시지가 나타납니다. \t 'e'가 'Protected Sub btnSendEmail_Click (보낸 사람 : Object, e As System.Web.UI.WebControls.GridViewEditEventArgs)'매개 변수에 대해 지정되지 않았습니다. \t \t 매개 변수 '보낸 사람'에 'Protected Sub btnSendEmail_Click (보낸 사람 개체, e As System.Web.UI.WebControls.GridViewEditEventArgs)'인수가 지정되지 않았습니다. – Kenny

+0

또한 업데이트를 시도하고 있으므로 onrowUpdating하지 않아야합니까? 나는 초보자이기 때문에 나는 단지 호기심이 많다. – Kenny

+0

이메일을 보낼시기에 따라 다릅니다. RowUpdating 이벤트는 나에게 더 적절하게 들립니다. 오류 메시지에 관해서는'e'를 일반'EventArgs' 객체로 대신 사용해보십시오. –

관련 문제