0

我有一个包含三列员工姓名员工详细信息和员工年龄的网格视图。我想在每一行添加一个复选框,在选择每个复选框后,我想触发与该员工关联的插入查询。你能告诉我如何将这个动态功能添加到网格视图中吗?

另外,如果我们使用<%# EVAL %>我不知道如何使用复选框来实现它。

4

3 回答 3

1

ASPX:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataSourceID="SqlDataSource1">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
                <asp:BoundField DataField="details" HeaderText="details" 
                    SortExpression="details" />
                <asp:BoundField DataField="age" HeaderText="age" SortExpression="age" />
            </Columns>
        </asp:GridView>
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="OK" />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:connApps %>" 
            SelectCommand="SELECT [name], [details], [age] FROM [tblA]">
        </asp:SqlDataSource>
        <br />
        <asp:Label ID="Label1" runat="server"></asp:Label>

后面的代码:

protected void Button1_Click(object sender, EventArgs e)
{
    Label1.Text = "Selected item name:<br>";
    foreach (GridViewRow  item in GridView1.Rows)
    {
        CheckBox chk = (CheckBox)item.FindControl("CheckBox1");
        if (chk != null)
        {
            if (chk.Checked)
            {
                Label1.Text += item.Cells[1].Text + "<br>";
            }
        }
    }
}

输出:

在此处输入图像描述

于 2013-11-09T11:54:33.013 回答
0

您可以将 TemplateFields 用于 GridView:

<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:CheckBox ID="myCheckBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

并在代码后面使用下面的代码:

ClusterName = GV.Rows(1).Cells(2).FindControl("myLabelinTheGridViewTemplateField")
于 2013-11-09T12:09:59.687 回答
0

这是一个例子,

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#336699" BorderStyle="Solid" BorderWidth="1px"  
CellPadding="0" CellSpacing="0" DataKeyNames="CategoryID" Font-Size="10"
Font-Names="Arial" GridLines="Vertical" Width="40%">

        <Columns>            
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="chkStatus" runat="server"
                        AutoPostBack="true" OnCheckedChanged="chkStatus_OnCheckedChanged"
                        Checked='<%# Convert.ToBoolean(Eval("Approved")) %>'
                        Text='<%# Eval("Approved").ToString().Equals("True") ? " Approved " : " Not Approved " %>' />
                </ItemTemplate>                   
            </asp:TemplateField>

            <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />                   
            <asp:BoundField DataField="CategoryName" HeaderText="CategoryName"  />
        </Columns>

<HeaderStyle BackColor="#336699" ForeColor="White" Height="20" />

欲了解更多信息,请查看这里

于 2013-11-09T10:17:55.523 回答