如何维护复选框的状态? [英] How do I mainain the state of checkbox?

查看:42
本文介绍了如何维护复选框的状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 gridview 中有一个复选框列表.

I have a list of checkboxes in gridview.

您可以检查一个,也可以检查任意多个,最多 10 个.

You may check one or you may check as many as you wish, up to 10.

在分页应用中选中一个或多个框后,您会翻到下一页,这些框未选中.

Once you check a box or boxes in a paging app, you page to the next page, the boxes are unchecked.

如何让选中的框保持选中状态,直到用户取消选中它们?

How do I keep the checked boxes to remain checked until a user unchecks them?

我下面的代码不起作用.

The code I have below isn't working.

For Each r As GridViewRow In GridMaks.Rows
    If CType(r.Cells(0).FindControl("makos"), CheckBox).Checked Then
        Dim marko As String = baseHint & "&cup=" & r.Cells(4).Text
        CType(r.Cells(0).FindControl("marko"), CheckBox).Checked = True '// added this hoping to keep checkbox checked but it doesn't work
    End If
Next

很抱歉包含 2 种风格的 c# 和 vb.net.如果有帮助,我可以将 c# 转换为 vb.

Sorry for including 2 flavors c# and vb.net. I can convert c# to vb if it helps.

推荐答案

如果您在每次加载页面时都将数据绑定到网格,则复选框将不会保留,因为它们正在被重置.尝试执行类似于下面简单示例代码的检查,以便仅在请求不是回发时绑定数据.

If you're binding data to the grid each time a page loads, the checkboxes won't persist because they're being reset. Try performing a check like the simple sample code below to only bind data when the request is not a postback.

if (IsPostBack) { 
    // It is a postback, don't bind data
} else { 
    // It is not a postback, bind data
} 

此外,您可能会发现这篇关于分页时保持复选框状态的文章对以下方面有帮助你需要.在 GridView 的PageChanging"事件中,文章说将具有 checked 复选框的每一行的行索引存储在 List 中,然后将该 List 存储在 ViewState 中.当页面更改时,从 ViewState 中检索该 List 并重新填充 GridView 复选框.

Also, you may find this article regarding Persisting checkbox state while paging helpful for what you need. In the GridView's "PageChanging" event, the article says to store the row index of each row that has a checked checkbox in a List, then store that List within the ViewState. When the page changes, retrieve that List from the ViewState and repopulate the GridView checkboxes.

这篇关于如何维护复选框的状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆