2014-01-29 47 views
0

我有一个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> 
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 
    <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; 
     } 
    } 
} 

回答

0

有两个选项是:

第一种方法是发送您的CommandArguement需要的数据。如:

CommandArgument='<%#((GridViewRow)Container).RowIndex.ToString() +"|" + Eval("Anything").ToString() %>' 

|符号在这里用作分隔符。

var arguements = CommandArguement.ToString().Split('|'); 

第二种方法是去该行通过使用行索引和搜索您想在邮件内容:然后,您可以用分隔符一样拆分字符串。

0

在你“按需”事件处理程序,首先找到的是包含按钮网格行的“所有者”

Button btn = (sender as Button); 
GridViewRow grv = (GridViewRow)btn.NamingContainer; 

GridRowView然后给你在基础数据源使用下面

DataView dview = (DataView)prmtest.Select(DataSourceSelectArguments.Empty); 
string str = (string)dview[grv.DataItemIndex]["Location"]; 

替代地接入到行(DataItemIndex),它可以再拉任何/所有字段,如果它是从字面上你需要的位置,那么可能就像这样简单。

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 ...

+0

我不知道我明白我现在必须创建另一个按钮,因为电子邮件代码放在Row_Command中。 – user3058255

+0

将它放在Row_Command事件处理程序中 –

相关问题