在批量更新中将对象强制转换为CheckBox

本文关键字:转换 CheckBox 对象 更新 | 更新日期: 2023-09-27 17:59:00

到目前为止,我一直在关注这个问题,文本框一切都很好,但当尝试将其修改为复选框时,会出现错误:无法将类型为"System.Web.UI.WebControls.textbox"的对象强制转换为类型为"System.Web.UI.WebControls.checkbox"。

当我使用TextBox时,它是有效的,但我如何在批量更新中使所有复选框都可编辑?

这是背后的示例代码

private bool tableCopied = false;
        private DataTable originalDataTable;
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
                if (!tableCopied)
                {
                    originalDataTable = ((DataRowView)e.Row.DataItem).Row.Table.Copy();
                    ViewState["originalValueTable"] = originalDataTable;
                    tableCopied = true;
                }
        }
        protected void UpdateButton_Click(object sender, EventArgs e)
        {
            originalDataTable = (DataTable)ViewState["originalValueTable"];
            foreach (GridViewRow r in GridView1.Rows)
                if (IsRowModified(r))
                {
                    GridView1.UpdateRow(r.RowIndex, false);
                }
            tableCopied = false;
            GridView1.DataBind();
        }
        protected bool IsRowModified(GridViewRow r)
        {
            int currentID;
            string currentreservedate;
            string currentisapproved;
            currentID = Convert.ToInt32(GridView1.DataKeys[0].Value);

            currentreservedate = ((TextBox)r.FindControl("reservedateTextBox")).Text;
            currentisapproved = ((CheckBox)r.FindControl("isapprovedCheckBox")).Text;
            DataRow row = originalDataTable.Select(String.Format("reservationid = {0}", currentID))[0];
            if (!currentreservedate.Equals(row["reservedate"].ToString()))
            if (!currentisapproved.Equals(row["isapproved"].ToString()))
            {
                return true;
            }
            return false;
        }
    }
}

这是aspx标记

Pending Reservation<br />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>" 
            SelectCommand="SELECT dbo.BookReservation.reservationid, dbo.BookReservation.bookid, dbo.BookReservation.EmployeeID, dbo.BookReservation.reservedate, dbo.BookReservation.isapproved, dbo.BookReservation.reschedule, dbo.BookReservation.isdeleted, dbo.TblBooks.booktitle FROM dbo.BookReservation INNER JOIN dbo.TblBooks ON dbo.BookReservation.bookid = dbo.TblBooks.bookid" 
            DeleteCommand="DELETE FROM [BookReservation] WHERE [reservationid] = @reservationid" 
            InsertCommand="INSERT INTO [BookReservation] ([bookid], [EmployeeID], [reservedate], [isapproved], [reschedule], [isdeleted]) VALUES (@bookid, @EmployeeID, @reservedate, @isapproved, @reschedule, @isdeleted)" 
            UpdateCommand="UPDATE [BookReservation] SET [bookid] = @bookid, [EmployeeID] = @EmployeeID, [reservedate] = @reservedate, [isapproved] = @isapproved, [reschedule] = @reschedule, [isdeleted] = @isdeleted WHERE [reservationid] = @reservationid">
            <DeleteParameters>
                <asp:Parameter Name="reservationid" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="bookid" Type="Int64" />
                <asp:Parameter Name="EmployeeID" Type="String" />
                <asp:Parameter Name="reservedate" Type="DateTime" />
                <asp:Parameter Name="isapproved" Type="Boolean" />
                <asp:Parameter Name="reschedule" Type="Boolean" />
                <asp:Parameter Name="isdeleted" Type="Boolean" />
                <asp:Parameter Name="reservationid" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="bookid" Type="Int64" />
                <asp:Parameter Name="EmployeeID" Type="String" />
                <asp:Parameter Name="reservedate" Type="DateTime" />
                <asp:Parameter Name="isapproved" Type="Boolean" />
                <asp:Parameter Name="reschedule" Type="Boolean" />
                <asp:Parameter Name="isdeleted" Type="Boolean" />
            </InsertParameters>
        </asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="true" AllowSorting="true"
            DataKeyNames="reservationid, bookid, EmployeeID, reservedate " DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound" >
            <Columns>
                <asp:BoundField DataField="reservationid" HeaderText="reservationid" 
                    InsertVisible="False" ReadOnly="True" 
                    SortExpression="reservationid" Visible="False" />
                <asp:BoundField DataField="bookid" HeaderText="bookid" 
                    SortExpression="bookid" Visible="False" />
                <asp:BoundField DataField="booktitle" HeaderText="Title" 
                    SortExpression="booktitle" />
                <asp:BoundField DataField="EmployeeID" HeaderText="Reserved by" 
                    SortExpression="EmployeeID" />
                <%--<asp:BoundField DataField="reservedate" HeaderText="Date reserved"
                    SortExpression="reservedate" />--%>
                <asp:TemplateField HeaderText="Reserve Date" 
                    SortExpression="reservedate">
                    <EditItemTemplate>
                        <asp:TextBox ID="reservedateTextBox" runat="server" Text='<%# Bind("reservedate") %>'>
                        </asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:TextBox ID="reservedateTextBox" runat="server" Text='<%# Bind("reservedate") %>'>
                        </asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <%--<asp:CheckBoxField DataField="isapproved" HeaderText="Approved" 
                    SortExpression="isapproved" />--%>
                <asp:TemplateField HeaderText="Apprvoed" 
                    SortExpression="isapproved">
                    <EditItemTemplate>
                        <asp:TextBox ID="isapprovedCheckBox" runat="server" Text='<%# Bind("isapproved") %>'>
                        </asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:TextBox ID="isapprovedCheckBox" runat="server" Text='<%# Bind("isapproved") %>'>
                        </asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CheckBoxField DataField="reschedule" HeaderText="Reschedule" 
                    SortExpression="reschedule" />
                <asp:CheckBoxField DataField="isdeleted" HeaderText="Deleted" 
                    SortExpression="isdeleted" />
            </Columns>
        </asp:GridView>
        <br />
        <br />
    <asp:Button ID="UpdateButton" runat="server" Text="Update" OnClick="UpdateButton_Click" />
        <br />

非常感谢您的帮助!提前感谢!

在批量更新中将对象强制转换为CheckBox

我认为要回答这个问题,需要aspx文件的内容,但我会尽力提供帮助。很可能表单中的元素isapprovedCheckBox仍然设置为标记asp:Text。因此,系统无法将文本框转换为复选框。

你能把你的aspx内容也包括在内吗?