**尊敬的Sir / Ma' am,
Employee
表中有四列,EmpID,EmpName,Address,Status。我想通过CheckBox从前面更新状态列。在下面给出的代码中,我遇到' Index-out of range' 异常。请注意,在这种情况下,Emp_Status是整数类型。但是,如果我在编辑模式下逐行更新Emp_Status。在这种情况下,选中复选框,其中Emp_Status具有1个值,并且未选中其中为0.我的要求是,如果用户在分页中选中/取消选中一个复选框或多个复选框,则在此基础上,值也应在数据库中更新。
我不是软件开发人员,我只是一名学习者,到目前为止从未在IT方面工作过,所以只是学习。请帮帮我。 **
数据库表格结构
CREATE TABLE [dbo].[Employee](
[Employee_ID] [int] IDENTITY(1,1) NOT NULL,
[Employee_Name] [varchar](50) NULL,
[Employee_Address] [varchar](100) NULL,
[Emp_Status] [int] NULL,
PRIMARY KEY CLUSTERED
(
[Employee_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON
网络表单
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>Employee Name: </td>
<td><asp:TextBox ID="txt_EmployeeName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Employee Address: </td>
<td><asp:TextBox ID="txt_EmployeeAddress" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Button ID="btn_Submit" runat="server" Text="Submit" OnClick="btn_Submit_Click" style="width: 61px" /></td>
</tr>
<tr>
<td><asp:LinkButton ID="Link_Search" runat="server" Text="Go For Search"></asp:LinkButton></td>
</tr>
<tr>
<td>Enter Name or Email ID for Search: </td>
<td><asp:TextBox ID="txt_Search" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Button ID="btn_Search" runat="server" Text="Search" OnClick="btn_Search_Click" /></td>
</tr>
<tr>
<td>
<asp:GridView ID="GV_Product" runat="server" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" AutoGenerateColumns="false" ShowFooter="true" OnRowEditing="GV_Product_RowEditing" OnRowUpdating="GV_Product_RowUpdating" OnRowDeleting="GV_Product_RowDeleting" OnRowCancelingEdit="GV_Product_RowCancelingEdit" OnRowCommand="GV_Product_RowCommand" AllowPaging="true" PageSize="5" OnPageIndexChanging="GV_Product_PageIndexChanging">
<AlternatingRowStyle BackColor="Gainsboro" />
<Columns>
<asp:TemplateField HeaderText="Employee ID">
<ItemTemplate>
<asp:Label ID="lbl_ID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Employee_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employee Name">
<ItemTemplate>
<asp:TextBox ID="txt_Name" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Employee_Name") %>'></asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEdit_Name" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Employee_Name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAdd_Name" runat="server"></asp:TextBox>
<%--<asp:RequiredFieldValidator ID="txtName" runat="server" ControlToValidate="txtAdd_Name" ErrorMessage="Please enter Employee Name"></asp:RequiredFieldValidator>--%>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employee Address">
<ItemTemplate>
<asp:TextBox ID="txt_Address" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Employee_Address") %>'></asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEdit_Address" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Employee_Address") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAdd_Address" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" />
<asp:Button ID="btnDelete" runat="server" Text="Delete" CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this record?')" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btnUpdate" runat="server" Text="Update" CommandName="Update" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lblAdd" runat="server" Width="100px" Text="Add" CommandName="ADD"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:CheckBox ID="ChkBox" runat="server" Checked='<%# GetStatus(DataBinder.Eval(Container.DataItem,"Emp_Status").ToString() )%>' AutoPostBack="true"
Text='<%# Eval("Emp_Status").ToString()=="1"?"Active":"Inactive" %>' OnCheckedChanged="ChkBox_CheckedChanged"
/>
</ItemTemplate>
<%--<EditItemTemplate>
<asp:CheckBox ID="EditChkBox" runat="server" Checked='<%# GetStatus(DataBinder.Eval(Container.DataItem,"Emp_Status").ToString() )%>' Text='<%# Eval("Emp_Status").ToString()=="1"?"Active":"Inactive" %>'/>
</EditItemTemplate>--%>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
代码
protected void ChkBox_CheckedChanged(object sender, EventArgs e)
{
CheckBox ChkBox = (CheckBox)sender;
//int eID = -1;
//eID = Convert.ToInt32(GV_Product.DataKeys[((GridViewRow)ChkBox.NamingContainer).RowIndex].Value);
string sQuery = "UPDATE Employee SET Emp_Status = @Status WHERE Employee_ID = @ID";
string consString = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
using (SqlConnection conn = new SqlConnection(consString))
{
SqlCommand com = new SqlCommand(sQuery, conn);
com.Parameters.Add("@Status", SqlDbType.Int).Value =0;
com.Parameters.Add("@ID", SqlDbType.Int).Value = eID;
conn.Open();
com.ExecuteNonQuery();
}
BindGV();
}
protected void GV_Product_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GV_Product.PageIndex = e.NewPageIndex;
BindGV();
}
protected bool GetStatus(string str)
{
if (str=="1")
{
return true;
}
else
{
return false;
}
}
protected void BindGV()
{
string Table_Name = "Employee";
BAL obj = new BAL();
DataTable dt = obj.Select(Table_Name);
GV_Product.DataSource = dt;
GV_Product.DataBind();
}
数据绑定类
public class BAL
{
public DataTable Select(string table)
{
string query = "Select *from " + table;
DAL obj = new DAL();
DataTable dt = new DataTable();
dt = obj.Select(query);
return dt;
}
}
public class DAL
{
public DataTable Select(string query)
{
SqlConnection Con = new SqlConnection(ConString);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = query;
cmd.Connection = Con;
Con.Open();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
Con.Close();
return dt;
}
}