从网格视图向SQL表中插入多个ID,该视图允许选中多个复选框

时间:2018-10-09 10:14:51

标签: c# asp.net gridview onclick

我有一个网格视图,允许用户通过复选框从要向数据库表中插入dataid的选定行中选择多行。

我在aspx部分使用了一个foreach,看起来不错,但是单击按钮时没有插入任何内容。

Gridview代码。

<asp:GridView ID="GV_EyeBall" runat="server" AutoGenerateColumns="False" Font-Size="Small" Width="44%" >
    <RowStyle HorizontalAlign="Center" />
    <Columns>
        <asp:BoundField DataField="DataId" HeaderText="DataId" ItemStyle-Width="90px" ItemStyle-Wrap="false" ItemStyle-CssClass="checkIt" SortExpression="DataId" HeaderStyle-CssClass="text-center" />
        <asp:BoundField DataField="BusinessName" HeaderText="BusinessName" ItemStyle-Width="90px" ItemStyle-Wrap="false" ItemStyle-CssClass="checkIt" SortExpression="BusinessName" HeaderStyle-CssClass="text-center" ControlStyle-Font-Size="Small" />
        <asp:BoundField DataField="Industry" HeaderText="Industry" ItemStyle-Width="90px" ItemStyle-Wrap="false" ItemStyle-CssClass="checkIt" SortExpression="Industry" HeaderStyle-CssClass="text-center" />
        <asp:BoundField DataField="TelephoneNumber" HeaderText="TelephoneNumber" ItemStyle-Width="90px" ItemStyle-Wrap="false" ItemStyle-CssClass="checkIt" SortExpression="TelephoneNumber" HeaderStyle-CssClass="text-center" />
        <asp:TemplateField ItemStyle-Width="75px" ItemStyle-Wrap="false">
            <ItemTemplate>
                <asp:CheckBox ID="Removechk" runat="server" ItemStyle-Width="100px" HeaderText="Remove" />
                <%--<asp:HiddenField ID="DataID" runat="server" Value='<%# Eval("DataID") %>' />--%>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <HeaderStyle BackColor="Silver" ForeColor="Black" />
</asp:GridView>

aspx页面代码

protected void Btnsubmit_Click(object sender, EventArgs e)
{
    string str = string.Empty;
    string strname = string.Empty;
    foreach (GridViewRow gvrow in GV_EyeBall.Rows)
    {
        CheckBox chk = (CheckBox)gvrow.FindControl("Removechk");
        if (chk.Checked)
        {
            Calldatabase.InsertEyeballids(Convert.ToInt32(gvrow.Cells[0].Text));

        }
    }
}

页面加载

        protected void Page_Load(object sender, EventArgs e)
    {
        var eyeball = Calldatabase.Eyeball();

        GV_EyeBall.DataSource = eyeball;
        GV_EyeBall.DataBind();
    }

1 个答案:

答案 0 :(得分:1)

似乎您在单击Page_Load上的Submit按钮时再次绑定了网格视图。由于您尚未共享Page_Load方法,因此我根据您的问题实施了以下工作片段。

WebForm1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="tempApp13.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:GridView ID="gv" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" />
                <asp:BoundField DataField="Name" HeaderText="Name" />
                <asp:BoundField DataField="Job" HeaderText="Job" />
                <asp:BoundField DataField="Salary" HeaderText="Salary" />                
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="chkRemove" runat="server" HeaderText="Remove" />                        
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="btnSubmit_Click" />
        <asp:Literal ID="litMessage" runat="server" />
    </form>
</body>
</html>

WebForm1.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace tempApp13
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                gv.DataSource = new List<Employee>
                {
                    new Employee { ID = 1, Name = "John", Job = "Clerk", Salary = 25000 },
                    new Employee { ID = 2, Name = "Allen", Job = "Clerk", Salary = 20000 },
                    new Employee { ID = 3, Name = "Smith", Job = "Sales", Salary = 21000 },
                    new Employee { ID = 4, Name = "Martin", Job = "Sales", Salary = 35000 },
                    new Employee { ID = 5, Name = "Bruce", Job = "Analyst", Salary = 35000 },
                    new Employee { ID = 6, Name = "James", Job = "Clerk", Salary = 25000 },
                };
                gv.DataBind();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gv.Rows)
            {
                var chk = (CheckBox)row.FindControl("chkRemove");
                if (chk.Checked)
                {
                    litMessage.Text += row.Cells[1].Text + " ";
                }
            }
        }
    }

    public class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Job { get; set; }
        public int Salary { get; set; }
    }
}

我已经获取了一个样本数据源员工,并将其与Page_Load上的网格视图绑定,在这里我检查其不是IsPostBack。因此,在初始加载时,数据已绑定到网格视图,但不会在页面回发时再次绑定。其余代码相似并且非常简单。

希望有帮助

感谢您提供Page_Load方法。检查以下代码:

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack) {
        var eyeball = Calldatabase.Eyeball();

        GV_EyeBall.DataSource = eyeball;
        GV_EyeBall.DataBind();
    }
}