2013-05-13 40 views
1

伊夫添加该代码能在这个网格复选框列复选框值:获取在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 

如何获得复选框的正确值的任何想法?

+0

先定义值然后尝试取值! – 2013-05-13 14:03:49

+0

当我点击复选框然后点击发送时,该值就会被定义。我只想检查这些值,因为其中存在一个错误 – 2013-05-13 14:07:21

+0

显示代码,您正在定义复选框值 – 2013-05-13 14:09:47

回答

1

在我看来,你可能需要遍历单元格而不是行;如果你在行数据绑定事件中做了它,那么在c#中它将会像(CheckBox)(e.Row.Cells [1] .FindControl(“myCheckBox”))一样。

+0

在vb它应该是Dim chk As CheckBox = CType(gridView.Rows(i).Cells(j).FindControl(“ myCheckBox“),CheckBox),或者你可以嵌套foreach循环 – 2013-05-13 14:09:05

1

我在这里记住了,但是我想如果你为复选框定义了“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 
+0

你可以给我一个例子来说明如何定义数值和你的意思吗? – 2013-05-13 14:29:47

+0

我使用定义值的示例更新了我的代码。我也更正了我最初提到的收藏品的名称。 – Rick 2013-05-13 15:02:26

+0

我刚刚阅读你对另一个答案的评论。你想获得价值,还是只是试图确定它是否已被检查?要确定它是否已被检查,你只需看看“Checked”属性。在你的代码中,你会看到chk.Checked - 它会相应地为True/False。 – Rick 2013-05-13 15:28:09

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 
0

试试这个代码,这将给予选择的ID在CS代码。

<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> 

和单一选择

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代码

现在你可以得到所有选择复选框值使用

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

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

谢谢。