【问题标题】:Grid Checkbox value not changed, how to get value on button click?网格复选框值未更改,如何在按钮单击时获取值?
【发布时间】:2012-01-31 14:27:29
【问题描述】:

当我单击 gridview 内的复选框时,我在我的 Web 应用程序中使用 asp.net,然后我在按钮单击时检查它的值,它没有显示确切的值 这是asp代码

<asp:GridView ID="dgvMenu" runat="server" Width="100%" CssClass="grid" GridLines="None"
                                                AutoGenerateColumns="False">
                                                <Columns>

                                                    <asp:TemplateField>
                                                        <HeaderTemplate>
                                                            View
                                                        </HeaderTemplate>
                                                        <ItemTemplate>
                                                            <asp:CheckBox ID="View" runat="server" Checked="<%#Bind('View') %>" />
                                                        </ItemTemplate>
                                                        <ItemStyle Width="50px" HorizontalAlign="Center" />
                                                    </asp:TemplateField>


                                                    <asp:BoundField DataField="Menu" HeaderText="Menu Name">
                                                        <HeaderStyle HorizontalAlign="Left" CssClass="firstcol" />
                                                        <ItemStyle CssClass="firstcol" />
                                                    </asp:BoundField>
                                                </Columns>
                                            </asp:GridView>

这是它的 vb 版本,用于在按钮单击版本上获取其值

For Each item As GridViewRow In dgvMenu.Rows
            Dim MenuName As String = item.Cells.Item(1).Text
            Dim chkView As CheckBox = DirectCast(item.FindControl("View"), CheckBox)
        Next

我想检查它的值是选中还是未选中,以便我可以处理它的值

【问题讨论】:

  • GridView 下方的按钮在哪里?

标签: asp.net vb.net gridview checkbox


【解决方案1】:

我假设您即使在回发时也会绑定您的 GridView。你应该只绑定它if not PageIsPostback。

把这个放到Page_Load中:

Private Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        ' following function loads data and binds it to the GridView '
        BindGrid()
    End If
End Sub

如果您在每次回发时对 Grid 进行 DataBind,则无法重新加载 ViewState,因此所有更改的值都将丢失,并且无法正确触发事件。

【讨论】:

  • 感谢哥们,因为我是 Web 开发新手,所以我把所有的调试和控制都搞砸了
猜你喜欢
  • 1970-01-01
  • 2019-08-22
  • 2011-10-24
  • 2018-10-22
  • 1970-01-01
  • 2022-06-12
  • 1970-01-01
  • 2017-05-03
  • 1970-01-01
相关资源
最近更新 更多