我有一个gridview,其中每个生成的行都有gridview按钮。当我点击按钮它发送一封电子邮件给用户,但我需要能够读取其中一列(WHERE列)及其行值,并将其包含在我的邮件正文中单击gridview按钮后从列和行读取数据
这是我的标记代码
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" onrowcommand="GridView1_RowCommand"
DataSourceID="prmtest" Width="994px" BorderStyle="None" GridLines="None"
onselectedindexchanged="GridView1_SelectedIndexChanged"
HorizontalAlign="Center">
<RowStyle ForeColor="#4D5763" HorizontalAlign="Center" />
<Columns>
<asp:BoundField DataField="Name_of_Training" HeaderText="Name of training"
SortExpression="Name_of_Training" />
<asp:BoundField DataField="Column1" HeaderText="Date" ReadOnly="True"
SortExpression="Column1" />
<asp:BoundField DataField="Start_time" HeaderText="Start time"
SortExpression="Start_time" />
<asp:BoundField DataField="End_time" HeaderText="End time"
SortExpression="End_time" />
<asp:BoundField DataField="Location" HeaderText="Where"
SortExpression="Location" />
<asp:BoundField DataField="Seats" HeaderText="Available seats" SortExpression="Seats" />
<asp:HyperLinkField HeaderText="Training documents"
NavigateUrl="url.zip" Text="Training material" Target="_self">
<ItemStyle ForeColor="#E5003B" Font-Underline="false"/>
</asp:HyperLinkField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="false" CommandName="Book"
OnClientClick="alert('The booking will be made for?')" CommandArgument="<%#((GridViewRow)Container).RowIndex %>"
Text="Book" />
</ItemTemplate>
<ControlStyle Font-Bold="False" />
</asp:TemplateField>
</Columns>
<HeaderStyle ForeColor="#4D5763" BackColor="#DCDFE2" />
<EditRowStyle Font-Underline="False" />
</asp:GridView>
            
<asp:SqlDataSource ID="prmtest" runat="server"
ConnectionString="<%$ ConnectionStrings:dbprmtest %>"
SelectCommand="SELECT [Name of Training] AS Name_of_Training,
CONVERT(VARCHAR(10),[Date],20), [Start time] AS Start_time, [End time] AS End_time, [Location], [Seats] FROM [Sheet1$]">
</asp:SqlDataSource>
这是我的电子邮件代码
if (e.CommandName == "Book")
{
try
{
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("[email protected]");
mailMessage.From = new MailAddress("[email protected]");
mailMessage.Subject = "Test";
mailMessage.Body = "";
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "smtp.text.com;
smtpClient.Host = "test.com";
smtpClient.Port = 25;
smtpClient.Credentials = new NetworkCredential("test.test", "password");
smtpClient.Send(mailMessage);
btnemail.Text = "E-mail and Booking sent!";
}
catch (Exception ex)
{
Response.Write("Could not send the e-mail - error: " + ex.Message);
lblemail.Text = "Could not send the e-mail - error: " + ex.Message;
}
}
}
我不知道我明白我现在必须创建另一个按钮,因为电子邮件代码放在Row_Command中。 – user3058255
将它放在Row_Command事件处理程序中 –