C#Gridview CheckBox字段VS(模板字段+复选框)

 Peter-周周周成德 发布于 2023-02-09 21:31

gridview从数据库中删除了一列"Product".

有了这个,我需要一个checkbox让用户gridview在它完成时对每个产品进行"检查" .

我对研究复选框字段VS(模板字段+复选框),并决定使用(模板字段+复选框) 对于gridview持有checkbox.

GridView Column [0] =产品名称GridView列[1] =复选框

在"检查"一些checkboxes之后,用户点击提交将触发下面的事件.

string checkedBy;        
foreach (GridViewRow row in grvCheckList.Rows)
{
   // Im not sure how to check if each checkbox has been "checked" 
   // or not as it is in the gridview  cell.

   // what I like to have is
      if((checkbox in column[1]).checked == true)
      { 
        checkedBy = // Staff name 
        // my codes to store the staff name into database with respective to the product listed in             the gridview row 
       }
      else
      { 
        checkedBy = "NULL"
        // my code to store "NULL" into database with respect to the product listed in the gridview        row
      }
}   

通常checkbox,我通常做的是下面

if(checkbox1.checked == true ) 
else if(checkbox2.checked == true )
else if(checkbox3.checked == true )
etc

所以我的问题是如何检查checkbox每行中是否已"检查",尽管gridview使用中的每一行都相同checkbox.

1 个回答
  • CheckBox字段:
    必须绑定到数据库字段并且是只读的.

    模板字段中的复选框: 可用作rocord选择器.

    带模板字段的示例:

    ASPX:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" />
                <asp:BoundField DataField="fname" HeaderText="fname" SortExpression="fname" />
                <asp:BoundField DataField="lname" HeaderText="lname" SortExpression="lname" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Table]"></asp:SqlDataSource>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
    

    代码背后:

     protected void Button1_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow item in GridView1.Rows)
            {
                CheckBox chk = (CheckBox)item.FindControl("CheckBox1");
                if (chk != null)
                {
                    if (chk.Checked)
                    {
                        // process selected record
                        Response.Write(item.Cells[1].Text + "<br>");
                    }
                }
            }
        }
    

    2023-02-09 21:39 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有