yjh20040604 2025-01-06 13:47 采纳率: 0%
浏览 6

为什么checkbox总是false,选中后无法执行删除操作

public partial class BookLending : System.Web.UI.Page
{
LendService lendSrv = new LendService();
protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {
        if (Session["ReaderId"] != null)
        {
            if (Request.QueryString["bookid"] != null)
            {
                int yonghubianhao = Convert.ToInt16(Session["ReaderId"]);
                int tushubianhao = Convert.ToInt16(Request.QueryString["bookid"]);
                lendSrv.AddBorrowRecord(yonghubianhao, tushubianhao, DateTime.Now,DateTime.MaxValue);
            }
        }
        else
        {
            Response.Redirect("~/Login.aspx");
        }
    }
    GridView1.DataSource = lendSrv.bookshowByid(Convert.ToInt16(Session["ReaderId"]));
    GridView1.DataBind();
}

protected void btnborrow_Click(object sender, EventArgs e)
{
    Response.Redirect("~/Bookchaxun.aspx");
}

protected void btndelete_Click(object sender, EventArgs e)
{
   

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox chkBook = new CheckBox();
            chkBook = (CheckBox)GridView1.Rows[i].FindControl("CheckBox12");
            if (chkBook != null)
            {
                if (chkBook.Checked == true)
                {
                    int bookId = int.Parse(GridView1.Rows[i].Cells[1].Text);
                    lendSrv.DeleteBorrowRecord(int.Parse(Session["ReaderId"].ToString()), bookId);
                }
            }
        }

    GridView1.DataSource = lendSrv.bookshowByid(Convert.ToInt16(Session["ReaderId"]));
    GridView1.DataBind();

}
  • 写回答

1条回答 默认 最新

  • 道友老李 JWE233286一种基于机器视觉的水表指针读数识别及修正的方法 专利发明者 2025-01-06 13:50
    关注
    让【道友老李】来帮你解答,本回答参考通义千问大模型编写,并整理提供,如果还有疑问可以点击头像关注私信或评论。
    如果答案让您满意,请采纳、关注,非常感谢!
    问题描述:请解释上面的代码是做什么的,以及其中的关键逻辑是什么? 回答: 这段代码是一个ASP.NET网页,用于图书借阅的操作。其中包括页面加载时的逻辑、点击借书按钮的处理和点击删除按钮的处理。 关键逻辑:
    1. 在页面加载时,会判断是否是首次加载页面,是否用户已登录并且是否传入了图书ID参数。如果条件都满足,则调用LendService类的AddBorrowRecord方法来添加借阅记录。
    2. 点击借书按钮时,会跳转到Bookchaxun.aspx页面。
    3. 点击删除按钮时,会遍历GridView1中的行,找到被选择的复选框,并删除选中行对应的借阅记录。 示例代码:
    public partial class BookLending : System.Web.UI.Page
    {
        LendService lendSrv = new LendService();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ReaderId"] != null)
                {
                    if (Request.QueryString["bookid"] != null)
                    {
                        int readerId = Convert.ToInt16(Session["ReaderId"]);
                        int bookId = Convert.ToInt16(Request.QueryString["bookid"]);
                        lendSrv.AddBorrowRecord(readerId, bookId, DateTime.Now, DateTime.MaxValue);
                    }
                }
                else
                {
                    Response.Redirect("~/Login.aspx");
                }
            }
            GridView1.DataSource = lendSrv.bookshowByid(Convert.ToInt16(Session["ReaderId"]));
            GridView1.DataBind();
        }
        protected void btnborrow_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Bookchaxun.aspx");
        }
        protected void btndelete_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox chkBook = new CheckBox();
                chkBook = (CheckBox)GridView1.Rows[i].FindControl("CheckBox12");
                if (chkBook != null)
                {
                    if (chkBook.Checked == true)
                    {
                        int bookId = int.Parse(GridView1.Rows[i].Cells[1].Text);
                        lendSrv.DeleteBorrowRecord(int.Parse(Session["ReaderId"].ToString()), bookId);
                    }
                }
            }
            GridView1.DataSource = lendSrv.bookshowByid(Convert.ToInt16(Session["ReaderId"]));
            GridView1.DataBind();
        }
    }
    
    评论

报告相同问题?

问题事件

  • 创建了问题 1月6日