How to give mail after selecting a link in gridview
To send an email after selecting a link in a GridView, you can use the RowCommand
event of the GridView. Here's an example:
ASPX:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkSendEmail" runat="server" CommandName="SendEmail" CommandArgument='<%# Eval("ID") %>'>Send Email</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code-behind:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "SendEmail")
{
int ID = int.Parse(e.CommandArgument.ToString());
// Send email using ID
SendEmail(ID);
}
}
private void SendEmail(int ID)
{
// Your email sending code here
// For example:
string toEmail = "[email protected]";
string subject = "Test Email";
string body = "This is a test email";
MailMessage mail = new MailMessage();
mail.To.Add(toEmail);
mail.Subject = subject;
mail.Body = body;
SmtpClient client = new SmtpClient();
client.Send(mail);
}
In this example, we've added a LinkButton
to the GridView's template field, and set its CommandName
to "SendEmail" and CommandArgument
to the ID of the row. When the link is clicked, the RowCommand
event is fired, and we can access the ID of the row using e.CommandArgument
.
In the SendEmail
method, you can use the ID to retrieve the relevant data and send the email.
Note that you'll need to replace the SendEmail
method with your own email sending code.