我有一个gridview,每个生成的行都有gridview按钮。当我点击按钮时,它会向用户发送一封电子邮件,但我需要能够读取其中一列(WHERE列)及其行值并将其包含在我的消息正文中
这是我的标记代码
<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@test.com");
mailMessage.From = new MailAddress("email2@test.com");
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;
}
}
}
答案 0 :(得分:0)
在“oncommand”事件处理程序中,首先找到包含到按钮的网格行的“所有者”
Button btn = (sender as Button);
GridViewRow grv = (GridViewRow)btn.NamingContainer;
然后,GridRowView允许您访问基础数据源中的行(DataItemIndex),然后您可以从下面的任何/所有字段中提取
DataView dview = (DataView)prmtest.Select(DataSourceSelectArguments.Empty);
string str = (string)dview[grv.DataItemIndex]["Location"];
或者,如果它只是你需要的位置,那么也许就像这样简单。
Button btn = (sender as Button);
GridViewRow grv = (GridViewRow)btn.NamingContainer;
string str = gvAssets.Rows[grv.DataItemIndex].Cells[4].Text; // Cell[4] being the Location Col.
... HTH
答案 1 :(得分:0)
有两种选择:
第一种方法是在CommandArguement中发送您需要的数据。像:
CommandArgument='<%#((GridViewRow)Container).RowIndex.ToString() +"|" + Eval("Anything").ToString() %>'
|符号在这里用作分隔符。然后,您可以使用分隔符符号分割字符串,如:
var arguements = CommandArguement.ToString().Split('|');
第二种方法是使用行索引转到该行,并搜索您希望在邮件中拥有的元素。