gpt4 book ai didi

asp.net - 无法确定 GridView 中复选框的状态

转载 作者:行者123 更新时间:2023-11-29 14:00:50 25 4
gpt4 key购买 nike

我有一个带有复选框的 GridView。但我在确定是否选中给定行的复选框时遇到了真正的问题。

我需要从一行中检索某个值并将其放入代码中。但是,当我迭代 GridView Rows 时,程序不会输入检查 checkBox'x 状态的 if 语句。

这是后端代码:

 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 的代码。我在加载页面时自动填充它:

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

如果您能指出我的错误,我将不胜感激。

最佳答案

您必须遍历所有行并找到您的复选框控件,然后检查其选中状态。查看工作示例(我使用在线转换器将其转换为 VB)

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 上的列时,会失败。

达米安。

关于asp.net - 无法确定 GridView 中复选框的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15084274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com