2

我尝试在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' 的定义,并且找不到接受类型为'System.Web.UI.WebControls.Repeater' 的第一个参数的扩展方法'HeaderRow'(你是缺少 using 指令或程序集
引用?)

在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>
4

1 回答 1

1

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

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;
            }
        }
    }
}
于 2013-11-11T19:06:48.110 回答