使用gridview检查员工在asp.net vb中发送电子邮件给他们

时间:2013-10-29 13:35:44

标签: asp.net email gridview checkbox

美好的一天

我有网格视图,其中显示员工数据。

      <asp:TabContainer ID="tcmanagers" runat="server" ActiveTabIndex="0" 
                    Width="100%">
        <asp:TabPanel ID="TabAdmins" runat="server" HeaderText="Admins" TabIndex="3">
                        <ContentTemplate>

                        <asp:Label ID="Label4" runat="server" Font-Bold="False" Font-Italic="False" CssClass="style3"
                        ForeColor="Blue" Width="290px">Select Admins to send:</asp:Label>

                         &nbsp;&nbsp;&nbsp; 
                         <asp:Button ID="Sendadmins" runat="server" Text="Send Email" Width="201px"/>
                         <br />
                         <br />
                         <asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" DataKeyNames="Employee_ID"
                            DataSourceID="SqlDataSource3" Width="633px" CssClass="grd">
                         <Columns>
                         <asp:BoundField DataField="Employee_ID" HeaderText="Employee_ID" ReadOnly="True"
                                   SortExpression="Employee_ID" Visible="False" />
                          <asp:BoundField DataField="Employee_Number" HeaderText="Employee Number" SortExpression="Employee_Number">
                         <ItemStyle Width="70px" Wrap="False" />
                         </asp:BoundField>
                         <asp:BoundField DataField="Employee_Surname" HeaderText="Surname" SortExpression="Employee_Surname">
                         <ItemStyle Wrap="False" />
                         </asp:BoundField>
                         <asp:BoundField DataField="Employee_Name" HeaderText="Name" SortExpression="Employee_Name">
                         <ItemStyle Wrap="False" />
                         </asp:BoundField>
                         <asp:BoundField DataField="Employee_Job_Title" HeaderText="Job Title" SortExpression="Employee_Job_Title">
                         <ItemStyle Wrap="False" />
                         </asp:BoundField>
                         <asp:BoundField DataField="Employee_Dept" HeaderText="Department" SortExpression="Employee_Dept">
                         <ItemStyle Wrap="False" />
                         </asp:BoundField>
                         <asp:BoundField DataField="Employee_EMail" HeaderText="Email Address" SortExpression="Employee_EMail">
                         <ItemStyle Wrap="False" />
                         </asp:BoundField>
                         <asp:TemplateField HeaderText="Check All"><HeaderTemplate>
                         <asp:CheckBox ID="chkAlladmin" runat="server" AutoPostBack="True" OnCheckedChanged="chkAllAdmin_CheckedChanged" />
                         </HeaderTemplate>
                         <ItemTemplate>
                         <asp:CheckBox ID="chkAdmin" runat="server" />
                         </ItemTemplate>
                         </asp:TemplateField>
                         </Columns><RowStyle Font-Bold="False" Font-Italic="False" Font-Names="Arial" Font-Size="10pt" />
                         <EditRowStyle Font-Names="Arial" Font-Size="10pt" /><HeaderStyle Font-Bold="False" Font-Italic="False" Font-Names="Arial" Font-Size="10pt"
                             Font-Underline="False" CssClass="grdhead" HorizontalAlign="Center" />
                             <AlternatingRowStyle CssClass="grdalt" />
                             <SelectedRowStyle BackColor="Fuchsia" />
                             </asp:GridView>
                                    <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
                                ProviderName="<%$ ConnectionStrings:ApplicationServices.ProviderName %>" SelectCommand="SELECT  dbo.lms_Employee.Employee_ID,dbo.lms_Employee.Employee_EMail,dbo.lms_Employee.Employee_Number, dbo.lms_Employee.Employee_Surname, dbo.lms_Employee.Employee_Name, dbo.lms_Employee.Employee_Job_Title,dbo.lms_Employee.Employee_Dept FROM dbo.aspnet_Membership INNER JOIN  dbo.aspnet_Users ON dbo.aspnet_Membership.UserId = dbo.aspnet_Users.UserId INNER JOIN  dbo.aspnet_UsersInRoles ON dbo.aspnet_Users.UserId = dbo.aspnet_UsersInRoles.UserId INNER JOIN   dbo.lms_Employee ON dbo.aspnet_Users.UserName = dbo.lms_Employee.Employee_UserName WHERE     (dbo.lms_Employee.Employee_Active = 'True') AND (dbo.aspnet_UsersInRoles.RoleId = '9ec9b26d-64a1-4736-8e82-fe6de7c7caac') and Employee_Number <> '100'">
                                </asp:SqlDataSource>
                          </ContentTemplate>
                    </asp:TabPanel>
                </asp:TabContainer>

现在我可以选择其中的所有数据,也可以选择以下代码一次选择一个数据。

Protected Sub chkAllAdmin_CheckedChanged(sender As Object, e As EventArgs)

    For Each gr As GridViewRow In GridView3.Rows
        Dim cb As CheckBox = DirectCast(gr.FindControl("chkAdmin"), CheckBox)
        If DirectCast(sender, CheckBox).Checked Then
            cb.Checked = True
        Else
            cb.Checked = False
        End If
    Next

End Sub

我想要做的是向所有选定的员工发送电子邮件。我有一个iis服务器和SQL服务器。所以我的数据库在sql server上,我的web应用程序在iis服务器上。

我不知道如何处理此任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

如果没有为您编码,这是我用于此类任务的策略:

  1. 将BoundField项转换为TemplateFields。执行此操作时,您可能会在ItemTemplate中为每个字段设置有意义名称的控件,例如“lblEmployeeEmail”
  2. 在按钮上单击,循环浏览GridView中的项目,对于每一行,使用FindControl()方法查找CheckBox(然后检查是否已选中)并使用FindControl()获取任何其他您可能需要的字段。 (电子邮件标签等)
  3. 以下是通用代码模式(取自learneveryday.net):

    foreach (GridViewRow gvrow in GridView1.Rows)
    {
       CheckBox CheckBox1 = (CheckBox)gvrow.FindControl("CheckBox1");
       if (CheckBox1.Checked)
       {
          // we write this code for find a label value
          Label lblLookFor = (Label)gvrow.FindControl("lblLookFor");
          string lookfor = lblLookFor.Text;
       }
    }
    

    当然,这仅包括如何获取已检查的员工以及如何从GridViewRows检索其电子邮件地址。我假设您已经知道如何发送电子邮件,但如果没有,请参阅the MSDN documentation