【问题标题】:C# Gridview CheckBox Field VS (Template Field + CheckBox)C# Gridview 复选框字段 AND(模板字段 + 复选框)
【发布时间】:2014-01-04 19:57:18
【问题描述】:

我有一个gridview,它从数据库中提取了一列“产品”。

这样,我需要一个checkbox 供用户在完成后对gridview 中的每个产品进行“检查”。

我研究了 CheckBox Field VS (Template Field + CheckBox) 并决定使用 (Template Field + CheckBox)gridview 保存@987654325 @。

GridView 列[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

所以我的问题是,尽管gridview 中的每一行都使用相同的checkbox,但我如何检查每一行中的checkbox 是否已被“检查”。

【问题讨论】:

    标签: c# asp.net gridview checkbox


    【解决方案1】:

    复选框字段:
    必须绑定到数据库的一个字段并且是只读的。

    模板字段中的复选框: 可以用作 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>");
                    }
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2011-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多