0

我正在使用带有 C# 的 Visual Studio 2010。

我有一个问题,在我的一个 ascx 页面上,我创建了一个这样的复选框列:

<asp:TemplateField ItemStyle-HorizontalAlign="left" HeaderStyle-HorizontalAlign="Left" HeaderText="Beetald">
    <HeaderTemplate>
        <input type="checkbox" id="selectallTeachers" class="selectallTeachers" onclick="javasctipt:SelectheaderCheckboxesTeacher();" />
    </HeaderTemplate>
    <ItemTemplate>
        <input type="checkbox" id="chkPaid" class="caseTeacher" name="caseTeacher" runat="server" />
    </ItemTemplate>
</asp:TemplateField>

我创建了这样的javascript:

<script type="text/javascript">
    // Select/Deselect checkboxes based on header checkbox
    function SelectheaderCheckboxesTeacher() {
        if ($("#selectallTeachers").val() == 'on') {
            $('.caseTeacher').attr('checked', 'checked');
            $("#selectallTeachers").val('off');
        }
        else if ($("#selectallTeachers").val() == 'off') {
            $('.caseTeacher').removeAttr("checked");
            $("#selectallTeachers").val('on');
        }

        $(".caseTeacher").click(function () {
            if ($(".caseTeacher").length == $(".caseTeacher:checked").length) {
                $("#selectallTeachers").attr("checked", "checked");
                $("#selectallTeachers").val('off');
            } else {
                $("#selectallTeachers").removeAttr("checked");
                $("#selectallTeachers").val('on');
            }
        });
    }
    // if all checkbox are selected, check the selectallTeachers checkbox
    // and viceversa   
</script>  

对于一个buttonclick事件,我写了这个:

protected void btnDeleteSelectedMessages_Click(object sender, EventArgs e)
{
    if (currentGridView == null)
        setCurrentGridView();

    //controleer voor elke rij welke checkbox is geselecteerd
    foreach (GridViewRow row in currentGridView.Rows)
    {
        CheckBox cb = (CheckBox)row.FindControl("chkPaid");
        Guid messageID = (Guid)currentGridView.DataKeys[row.DataItemIndex].Value;

        //als de checkbox is geselecteerd het bericht verwijderen
        if (cb != null && cb.Checked)
        {
            if (currentGridView.ID.Equals("Messages"))
                b.BussinesMessageReceiver.DeleteMessageReceiver(messageID, MessageBoxPerson);
            else if (currentGridView.ID.Equals("MessagesSent"))
                b.BussinesMessage.DeleteMessageSender(messageID);
            else //MessagesDeleted
                b.BussinesMessage.DeleteMessage(messageID, (Page.Server.MapPath("~/Upload/") + messageID));
            continue;
        }
    }
    btnDeleteSelectedMessages.Enabled = false;
    selectLocation.Visible = false;
    clearSelectedMessageSession();

    //Update the GridView
    BindGridView();
}

它显示在线错误

CheckBox cb = (CheckBox)row.FindControl("chkPaid");

因为我是通过输入类型而不是直接通过复选框创建的。

4

1 回答 1

1

一个input type="checkbox"不是一个CheckBox。因此,您应该将其转换为HtmlInputCheckBox(或使用 a CheckBox)。

var cb = (HtmlInputCheckBox)row.FindControl("chkPaid");
于 2012-09-24T12:30:39.817 回答