Dropdown中的Onselect向相应用户发送邮件

时间:2014-09-29 11:50:33

标签: c# asp.net gridview

我有一个gridview,其中我有一个列名“ Selection ”。我希望当管理员选择“未选择”选项时,相应的用户应该收到一封关于他被拒绝的ID的电子邮件。请参阅gridview代码以供参考:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true" Width="920"
PageSize="5" OnPageIndexChanging="gv_Applicants_PageIndexChanging" OnRowCommand="gv_Applicants_RowCommand"
EmptyDataText="No Applicants Found."
AllowSorting="true"
OnSorting="gv_Applicants_Sorting"
OnRowDataBound="gv_Applicants_RowDataBound" RowStyle-CssClass="a12" AlternatingRowStyle-CssClass="a22" ForeColor="#333333" GridLines="None" CssClass="table_box" HeaderStyle-Height="35px">
<AlternatingRowStyle BackColor="#F0F0F0" />
<Columns>
    <asp:BoundField DataField="FirstName" HeaderText="First Name" HeaderStyle-Width="84" />
    <asp:BoundField DataField="LastName" HeaderText="Last Name" HeaderStyle-Width="106" />
    <asp:BoundField DataField="ContactNumber" HeaderText="Contact" HeaderStyle-Width="98" />
    <asp:BoundField DataField="Email" HeaderText="Email" HeaderStyle-Width="150" />

    <asp:TemplateField HeaderText="Position" SortExpression="Position" HeaderStyle-Width="107">
        <ItemTemplate>
            <%# Eval("Job.Position") %>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Location" SortExpression="Location" HeaderStyle-Width="100">
        <ItemTemplate>
            <%# Eval("Job.Location") %>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="AppliedDate" DataFormatString="{0:MMMM dd, yyyy}" HeaderText="Date of Application" ReadOnly="true" HeaderStyle-Width="121" />

    <asp:TemplateField HeaderText="Action" HeaderStyle-HorizontalAlign="Center">
        <ItemTemplate>
            <asp:LinkButton ID='lnkView' CommandName='v' Text='View' runat='server' CommandArgument='<%# Eval("ApplicantId") %>'></asp:LinkButton>
            |
                        <asp:LinkButton ID='lnkdel' CommandName='d' Text='Delete' runat='server' CommandArgument='<%# Eval("ApplicantId") %>' OnClientClick="return confirm('Are you sure to delete?');"></asp:LinkButton>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Selection">
        <ItemTemplate>
            <asp:Label ID="lblCountry" runat="server" Visible="true" />
            <asp:DropDownList ID="ddlCountries" runat="server">
                <asp:ListItem Text="None" Value="1"></asp:ListItem>
                <asp:ListItem Text="Selected" Value="2"></asp:ListItem>
                <asp:ListItem Text="Not Selected" Value="3"></asp:ListItem>
            </asp:DropDownList>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>


<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#D8DADA" Font-Bold="True" />
<HeaderStyle BackColor="#D8DADA" Font-Bold="True" />
<PagerStyle BackColor="#D8DADA" HorizontalAlign="Center" />
<RowStyle BackColor="white" BorderStyle="Solid" BorderColor="#a8a8a8" BorderWidth="1px" Height="35" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

Eidted code: -

 private void SendMail()
{
    StringBuilder sbuilder = new StringBuilder();
    sbuilder.Append("<div>");
    sbuilder.Append("<p>Thanks for applying at RBL. You have been rejected</p>");
    sbuilder.Append("</div>");

    string str = sbuilder.ToString();
    string ccEmail = "";
    Common objCom = new Common();
    string toId = ConfigurationManager.AppSettings["ddlEmail"].ToString();
    objCom.SendEMail(toId, "Rejection Mail", sbuilder.ToString(), ccEmail, false, "");
}


protected void ddlSelection_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList ddl = (DropDownList)sender;
    GridViewRow row = (GridViewRow)ddl.Parent.Parent;


    if (ddl.SelectedValue == "Not Selected")
    {
        SendMail();
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个,

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
   {

   if(e.Row.RowType == DataControlRowType.DataRow)
   {
      DropDownList ddl = e.Row.FindControl("ddlCountries") as DropDownList;
     if(ddl != null)
     {
        ddl.SelectedIndexChanged += new EventHandler(ddlCountries_SelectedIndexChanged);
     }
   }

 }

protected void ddlCountries_SelectedIndexChanged(object sender, EventArgs e)
{
     string selectedValue = ((DropDownList)sender).SelectedValue;
     if(selectedValue== "Not Selected")
     {
        // Write code here for sending mail
     }

 }