循环遍历 GridView 行并检查复选框控件

编程入门 行业动态 更新时间:2024-10-27 00:27:04
本文介绍了循环遍历 GridView 行并检查复选框控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我目前有一个 GridView,它显示来自学生表的数据,这是我的网格和关联的 SQLDataSource;

I currently have a GridView which displays data from a Student Table, here is my Grid and associated SQLDataSource;

                      <asp:GridView ID="GridView2" 
                        style="position:absolute; top: 232px; left: 311px;" 
                            AutoGenerateColumns="false" runat="server"
                        DataSourceID="SqlDataSource4">
                        <Columns>
                            <asp:TemplateField>
                            <ItemTemplate >
                            <asp:CheckBox runat="server" ID="AttendanceCheckBox" />
                            </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField>
                            <ItemTemplate>
                            <asp:Label ID="studentIDLabel" Text='<%# Eval("StudentID") %>' runat="server"></asp:Label>
                            </ItemTemplate>
                            </asp:TemplateField>                       
                            <asp:BoundField DataField="Name" HeaderText="Name" />
                        </Columns>
                     </asp:GridView>

                    <asp:SqlDataSource ID="SqlDataSource4" runat="server"
                        ConnectionString="<%$ ConnectionStrings:RegisterConnectionString %>" 
                        SelectCommand="SELECT [StudentID], [Name] FROM [Student] WHERE CourseID = @CourseID ">                         
                        <SelectParameters>
                            <asp:ControlParameter ControlID="CourseDropDownList" Name="CourseID" 
                                PropertyName="SelectedValue" Type="Int32" />
                        </SelectParameters>
                    </asp:SqlDataSource>

我在页面上有一个按钮,当用户单击该按钮时,我需要遍历 GridView 中的每一行,然后找到复选框,然后我需要检查复选框是否被选中.如果复选框被选中,我需要将标签模板字段中的值添加到数据库中的不同表中.我正在使用 C# 代码.非常感谢任何帮助,提前致谢!

I have a button on the page which when the user clicks the button I need to loop through each row in the GridView, then find the CheckBox then I need to check if the checkbox is checked or not. If the checkbox is checked I need to add the value in the Label Template Field to a different table in the database. I am using C# Code. Any help is much appreciated, thanks in advance!

推荐答案

Loop like

foreach (GridViewRow row in grid.Rows)
{
   if (((CheckBox)row.FindControl("chkboxid")).Checked)
   {
    //read the label            
   }            
}

这篇关于循环遍历 GridView 行并检查复选框控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-01 11:03:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/828307.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:遍历   控件   复选框   GridView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!