使用CheckBoxes在Asp.Net中发送电子邮件

时间:2013-11-11 19:01:11

标签: c# asp.net

我尝试在asp.net发送电子邮件。当管理员选择复选框然后他/她能够发送邮件到相应的电子邮件ID,电子邮件也存储在用户表的数据库中..但是当我构建代码时,它显示我错误

代码是

protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
{
    CheckBox chkAll =
           (CheckBox)Repeateremail.HeaderRow.FindControl("chkSelectAll");
    if (chkAll.Checked == true)
    {
        foreach (GridViewRow gvRow in Repeateremail.Items)
        {
            CheckBox chkSel =
                     (CheckBox)gvRow.FindControl("chkSelect");
            chkSel.Checked = true;
        }
    }
    else
    {
        foreach (GridViewRow gvRow in Repeateremail.Items)
        {
            CheckBox chkSel = (CheckBox)gvRow.FindControl("chkSelect");
            chkSel.Checked = false;
        }
    }
}

在这一行

CheckBox chkAll =
           (CheckBox)Repeateremail.HeaderRow.FindControl("chkSelectAll");

它显示标题中的错误 错误

  

'System.Web.UI.WebControls.Repeater'不包含                'HeaderRow'的定义,没有扩展方法'HeaderRow'                   接受'System.Web.UI.WebControls.Repeater'类型的第一个参数                可以找到(你错过了使用指令或程序集)                    参考?)

在html中我在标题模板中使用这样的

<td>
    Check
    <asp:CheckBox ID="chkSelectAll" runat="server"
                  AutoPostBack="true"
                  OnCheckedChanged="chkSelectAll_CheckedChanged"/>
    Send Mail To All ?                     
</td>

和项目模板

<td>
    <asp:CheckBox ID="chkSelect" runat="server"/>
</td>

1 个答案:

答案 0 :(得分:1)

您不需要使用FindControl()方法,因为您正在处理要检查属性值的控件的click事件,请尝试以下方法:

protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
{
    // Cast the sender to a CheckBox type
    CheckBox chkAll = sender as CheckBox;

    // The as operator will return null if the cast is not successful,
    // so check for null before we try to use it
    if(chkAll != null)
    {
        if (chkAll.Checked == true)
        {
            foreach (GridViewRow gvRow in Repeateremail.Items)
            {
                CheckBox chkSel =
                 (CheckBox)gvRow.FindControl("chkSelect");
                chkSel.Checked = true;
            }
        }
        else
        {
            foreach (GridViewRow gvRow in Repeateremail.Items)
            {
                CheckBox chkSel = (CheckBox)gvRow.FindControl("chkSelect");
                chkSel.Checked = false;
            }
        }
    }
}