获取一行asp.net gridview onclick的值并发送电子邮件

时间:2013-11-20 19:16:26

标签: c# asp.net email

你可以从下面的aspx代码中看到我需要gridview行的值

1)我的gridview最后一列包含dropdownlist.it包含两个值(qualified / disqualifed)。选择qualded或disqaulified我需要获取gridview行的所有值....

2)获取所有值后,我需要将这些值发送到电子邮件...电子邮件应发送到电子邮件列的特定电子邮件ID,

例如,如果我从gridview中选择第一行,并且在第一行中选择ist下拉列表(合格/取消资格)。在从下拉列表中选择合格/取消资格后,我需要获取第一行的所有值。 。我需要通过电子邮件将第一行gridview的所有值发送到第一行的电子邮件地址.....

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
       BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px"
       CellPadding="4" CellSpacing="2" ForeColor="Black" Height="122px"
       Width="495px" onrowdatabound="GridView1_RowDataBound">
       <FooterStyle BackColor="#CCCCCC" />
       <RowStyle BackColor="White" />
       <Columns>
           <asp:BoundField HeaderText="Application No" DataField="appno" />
           <asp:BoundField HeaderText="Name" DataField="appname" />
           <asp:BoundField HeaderText="Company Name" DataField="cname" />
           <asp:BoundField HeaderText="Post Name" DataField="pname" />
           <asp:BoundField HeaderText="Email" DataField="email" />
           <asp:BoundField HeaderText="Mobile No" DataField="mobile" />
           <asp:BoundField DataField="mksobtain" HeaderText="Marks Obtain" />
           <asp:BoundField DataField="mkstot" HeaderText="Total Marks" />
           <asp:BoundField DataField="hrname" HeaderText="HR Name" />
           <asp:BoundField DataField="hrno" HeaderText="HR No" />
           <asp:TemplateField HeaderText="Qualification Process">
               <EditItemTemplate>
                   <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
               </EditItemTemplate>
               <ItemTemplate>
                   <asp:Label ID="Label1" runat="server"></asp:Label>
                   <asp:DropDownList ID="DropDownList1" runat="server" 
              AutoPostBack="True" >
                   </asp:DropDownList>
               </ItemTemplate>
           </asp:TemplateField>
       </Columns>
       <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
       <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
       <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
   </asp:GridView>

1 个答案:

答案 0 :(得分:0)

要触发电子邮件,您需要使用下拉列表中的SelectedIndexChanged事件或向网格视图标记添加Send Email按钮,并处理该按钮的click事件或创建CommandName属性。此选项决定了如何获取要从中获取值的行。

因为您在网格视图标记中使用BoundField,所以您需要通过行的Cells集合引用行的值,如下所示:

// Assuming you have the row object, either from e.Row or 
// finding the parent or grandparent of the click event sender
GridViewRow theGridViewRow;

// Get the application number value
string theApplicationNumber = theGridViewRow.Cells[0].Text;

// Do similar logic for each field you want the value of here

我建议您在网格视图标记中使用Cells s,而不是依赖TemplateField集合的索引值,如下所示:

<Columns>
    <asp:TemplateField HeaderText="Application No">
        <asp:Label id="LabelApplicationNumber" runat="server" 
                   Text="<%# Eval('appno') %>" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Name">
        <asp:Label id="LabelApplicationName" runat="server" 
                   Text="<%# Eval('appname') %>" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Company Name">
        <asp:Label id="LabelCompanyName" runat="server" 
                   Text="<%# Eval('cname') %>" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Post Name">
        <asp:Label id="LabelPostName" runat="server" 
                   Text="<%# Eval('pname') %>" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Email">
        <asp:Label id="LabelEmail" runat="server" 
                   Text="<%# Eval('email') %>" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Mobile No">
        <asp:Label id="LabelMobileNumber" runat="server" 
                   Text="<%# Eval('mobile') %>" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Marks Obtain">
        <asp:Label id="LabelMarksObtain" runat="server" 
                   Text="<%# Eval('mksobtain') %>" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Total Marks">
        <asp:Label id="LabelTotalMarks" runat="server" 
                   Text="<%# Eval('mkstot') %>" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="HR Name">
        <asp:Label id="LabelHRName" runat="server" 
                   Text="<%# Eval('hrname') %>" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="HR No">
        <asp:Label id="LabelHRNumber" runat="server" 
                   Text="<%# Eval('hrno') %>" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Qualification Process">
        <EditItemTemplate>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="Label1" runat="server"></asp:Label>
            <asp:DropDownList ID="DropDownList1" runat="server" 
                              AutoPostBack="True" >
            </asp:DropDownList>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

现在在您的代码隐藏中,您可以使用FindControl()方法从行中获取值而不是使用Cells[i]表示法,如下所示:

// Assuming you have the row object, either from e.Row or 
// finding the parent or grandparent of the click event sender
GridViewRow theGridViewRow;

// Find the application number label control in the row
var theApplicationNumberLabel = theGridViewRow.FindControl("LabelApplicationNumber") as Label;

// Make sure we found the label before we try to use it
if(theApplicationNumberLabel != null)
{
    // Get the value
    string theApplicationNumber = theApplicationNumberLabel.Text;
}

// Do similar logic for each control in the row you want the value of here