无法确定在网格视图复选框的状态 [英] Can't determine status of checkbox in Grid View
问题描述
我在它复选框的GridView。但我在与确定给定行的一个复选框被选中或不实的问题。
I have a GridView with Checkbox within it. But I'm having real problem with determining if a check box of a given row is checked or not.
我需要检索从行一定的价值,并把它变成code。但是,当我通过GridView的行迭代程序不进入if语句将检查该checkBox'x状态。
I need to retrieve a certain value from a row and put it into code. But when I iterate through GridView Rows the program doesn't enter the if statement which checks the checkBox'x status.
下面是后端的code:
here is the code of backend:
Dim Primaryid As String = "Initial stage"
For Each gvr As GridViewRow In GridView1.Rows
If (CType(gvr.FindControl("CheckBox1"), CheckBox)).Checked = True Then
Primaryid = gvr.Cells(1).Text
End If
Next gvr
Dim exmess As String = "alert('" & Primaryid & "')"
Page.ClientScript.RegisterStartupScript(Me.GetType(), "ErrorAlert", exmess, True)
这是在GridView的code。
And here is the code of the GridView. I'm populating it automatically upon the loading of the page:
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" Width="1500px">
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
我会很感激,如果你能指出我在我的错误。
I would be very grateful if you can point me on my mistake.
推荐答案
您必须通过所有的行和放大器迭代;找到你的CheckBox控件,然后检查其选中状态。查看工作示例(我使用的在线转换器转换入VB)
You have to iterate through all the rows & find your checkbox control, then check for its checked state. Check out working example (I converted it into VB using online converter)
Protected Sub btnGetSelectedRows_Click(sender As Object, e As EventArgs)
Dim items = New StringBuilder()
For Each grow As GridViewRow In GridView1.Rows
Dim chkTemp As CheckBox = DirectCast(grow.FindControl("chkSelectRow"), CheckBox)
If chkTemp IsNot Nothing Then
If chkTemp.Checked Then
items.Append(String.Format("{0},", GridView1.DataKeys(grow.RowIndex)("ProductID").ToString()))
End If
End If
Next
If items.Length > 0 Then
Response.Write("You selected Ids:" & Convert.ToString(items))
End If
End Sub
和ASPX
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="ProductID">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelectRow" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ProductID" HeaderText="ProductID"
InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName"
SortExpression="ProductName" />
<asp:BoundField DataField="SupplierID" HeaderText="SupplierID"
SortExpression="SupplierID" />
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID"
SortExpression="CategoryID" />
<asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit"
SortExpression="QuantityPerUnit" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice"
SortExpression="UnitPrice" />
<asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock"
SortExpression="UnitsInStock" />
<asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder"
SortExpression="UnitsOnOrder" />
<asp:BoundField DataField="ReorderLevel" HeaderText="ReorderLevel"
SortExpression="ReorderLevel" />
<asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued"
SortExpression="Discontinued" />
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName"
SortExpression="CategoryName" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT * FROM [Alphabetical list of products]"></asp:SqlDataSource>
<br />
注意,我已经使用了
DataKeys
属性来访问该行的主键。我会建议你做同样与cellIndex中,当你改变未来你的GridView列从长远来看,无法接入单元格值。
property to access the primary key of the row. I would advise you to do the same as accessing cell values with cellIndex fails in the long run when you change columns on your gridview in future.
达。
这篇关于无法确定在网格视图复选框的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!