获取asp.net中的复选框值

时间:2013-05-13 13:54:46

标签: asp.net

我添加了这段代码,以便在我的网格中找到一个带有复选框的列:

<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 个答案:

答案 0 :(得分:2)

我要关闭内存,但我认为如果你为复选框定义了“Value”属性,那么你就可以在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

答案 1 :(得分:2)

基本上,您可以遍历GridView1.Rows并获取Checkboxes

注意:我在C#中编写并使用converter进行转换,因此我的VB代码可能有点奇怪。

enter image description here

<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

答案 2 :(得分:1)

在我看来,你可能不得不遍历细胞,而不是行;如果你在行数据绑定事件中做了它,那么在c#中它就像是(CheckBox)(e.Row.Cells [1]。.FindControl(“myCheckBox”))。

答案 3 :(得分: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("'", "");

检查一下,这肯定会有效。

谢谢。