1

我添加了此代码以在我的网格中获得一个带有复选框的列:

<Columns>
    <asp:TemplateField HeaderText="Visitor"  HeaderStyle-Width="20" FooterStyle-HorizontalAlign="Center">
        <ItemTemplate>
            <asp:CheckBox ID="myCheckBox" runat="server"/>
        </ItemTemplate>
        <FooterStyle HorizontalAlign="Center" />
        <HeaderStyle Width="20px" />
        <ItemStyle HorizontalAlign="Center" />
    </asp:TemplateField>

我想获取复选框的值,但我得到“对象引用未设置为对象的实例”。错误。这是我尝试调用它的地方,但出现错误:

Dim checkb As String
For Each row As GridViewRow In orderGrid.Rows
    Dim chk As CheckBox = CType(row.FindControl("myCheckBox"), CheckBox)
    **checkb  = Request.Form("myCheckBox")**
Next row

知道如何获得复选框的正确值吗?

4

4 回答 4

2

基本上,您可以循环GridView1.Rows并获取Checkboxes.

注意:我用 C# 编写并使用转换器进行转换,所以我的 VB 代码可能有点奇怪。

在此处输入图像描述

<asp:GridView runat="server" ID="GridView1" 
    AutoGenerateColumns="False" DataKeyNames="Id">
    <Columns>
        <asp:TemplateField HeaderText="Visitor">
            <ItemTemplate>
                <asp:CheckBox ID="myCheckBox" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField HeaderText="FirstName" DataField="FirstName" />
        <asp:BoundField HeaderText="LastName" DataField="LastName" />
    </Columns>
</asp:GridView>
<asp:Button runat="server" ID="SubmitButton" 
    OnClick="SubmitButton_Click" Text="Submit" />

Public Class User
    Public Property Id() As Integer
        Get
            Return m_Id
        End Get
        Set
            m_Id = Value
        End Set
    End Property
    Private m_Id As Integer
    Public Property FirstName() As String
        Get
            Return m_FirstName
        End Get
        Set
            m_FirstName = Value
        End Set
    End Property
    Private m_FirstName As String
    Public Property LastName() As String
        Get
            Return m_LastName
        End Get
        Set
            m_LastName = Value
        End Set
    End Property
    Private m_LastName As String
End Class

Protected Sub Page_Load(sender As Object, e As EventArgs)
    If Not IsPostBack Then
        Dim collection = New List(Of User)() With { _
            New User() With { _
                .Id = 1, _
                .FirstName = "John", _
                .LastName = "Doe" _
            }, _
            New User() With { _
                .Id = 2, _
                .FirstName = "Marry", _
                .LastName = "Doe" _
            }, _
            New User() With { _
                .Id = 3, _
                .FirstName = "David", _
                .LastName = "Newton" _
            } _
        }

        GridView1.DataSource = collection
        GridView1.DataBind()
    End If
End Sub

Protected Sub SubmitButton_Click(sender As Object, e As EventArgs)
    For Each row As GridViewRow In GridView1.Rows
        Dim checkBox = TryCast(row.FindControl("myCheckBox"), CheckBox)
        If checkBox.Checked Then
            ' Get the ID of selected row
            Dim id = GridView1.DataKeys(row.DataItemIndex).Value
        End If
    Next
End Sub
于 2013-05-13T14:39:31.743 回答
2

我在这里要记错了,但是我认为如果您为复选框定义了“值”属性,那么您就可以在 PostBack 上访问它。但是,您必须在 Attributes 集合中显式查找它。

更新: 我最初说这将是“InputAttributes”集合。我测试了一下,发现我记错了。它实际上是您需要使用的“属性”集合。

Dim checkb As String
For Each row As GridViewRow In orderGrid.Rows
    Dim chk As CheckBox = CType(row.FindControl("myCheckBox"), CheckBox)
    Dim v as String = chk.Attributes.Item("value")
Next row

定义值的示例可以像在标记中定义它一样简单......

<asp:CheckBox ID="myCheckBox" runat="server" value="Red" />

如果您要填充数据绑定中的值,那么您可以使用 Eval() 函数...

<asp:CheckBox ID="myCheckBox" runat="server" value='<%# Eval("myID") %>' />

然后你也可以通过后面的代码来做到这一点。由于您使用了 GridView,因此在 GridView 的 RowDataBound 事件处理程序中,您可以获得对该行中 CheckBox 的引用并在那里定义其值。

CheckBox1.Attributes.Item("value") = "some_value"  ' Could be pulled from whatever data item is tied to your GridView
于 2013-05-13T14:18:49.970 回答
1

在我看来,您可能必须遍历单元格而不是行;不确定vb的副手,在c#中,如果您在行数据绑定事件中执行此操作,它将类似于 (CheckBox )(e.Row.Cells[1].FindControl("myCheckBox")) ;

于 2013-05-13T14:00:12.080 回答
0

试试这个代码,这将在cs代码中给出选定的id。

<asp:TemplateField HeaderText="All">
   <HeaderStyle Width="3%" HorizontalAlign="Center" VerticalAlign="Middle" />
         <ItemStyle HorizontalAlign="center" VerticalAlign="Middle" />
   <HeaderTemplate>
         <input id="chkAll" type="checkbox" name="chkAll" onclick="javascript: checkall();" />
   </HeaderTemplate>
   <ItemTemplate>
         <input id="chkBox" name="chkBox" type="checkbox" value="<%# DataBinder.Eval(Container.DataItem, "CategoryId") %>" onclick="javascript: checkManual();" />
   </ItemTemplate>
 </asp:TemplateField>

和用于单选的javascript函数

function checkManual() {
    var intCheckVal;
    intCheckVal = 1;
    for (intCounter = 0; intCounter < (document.getElementsByName('chkBox').length); intCounter++) {
        if (document.getElementsByName("chkBox")[intCounter].checked == false)
            intCheckVal = 0;
    }
    if (intCheckVal == 1)
        document.getElementById("chkAll").checked = true;
    else
        document.getElementById("chkAll").checked = false;
}

并检查所有

function checkall() {
    for (intCounter = 0; intCounter < (document.getElementsByName('chkBox').length); intCounter++) {
        document.getElementsByName("chkBox")[intCounter].checked = document.getElementById("chkAll").checked;
    }
}

现在在按钮单击事件的cs代码中,您可以使用获取所有选定的复选框值

string MultiIDs = Request.Form["chkBox"].Replace("'", "");

检查这个,这肯定会工作。

谢谢你。

于 2013-05-13T14:47:49.983 回答