我正在尝试为每个生成的行添加编辑/更新/删除超链接,这样如果有任何错误,则用户可以编辑/更新/删除它。我已经找到了与我得到的代码有关的代码,但我无法做到。 我正在为用户显示数据,以便可以提交相同ID的多个行。这不是实际的代码,但在逻辑上类似。我还有其他几个用户输入的字段。
提前感谢您的帮助。
ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="gridViewTest.WebForm2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div align="center" style="margin-top:50px;">
<table align="center" style="border-collapse:collapse; width:500px;">
<tr>
<th align="left" colspan="4" style="color:#86ef12; border:1px solid #86ef12; padding:5px 10px;">Add Employees Details</th>
</tr>
<tr>
<td style="border:1px solid #86ef12;padding:5px 10px;"> Employ ID :</td>
<td style="border:1px solid #86ef12;"><asp:TextBox ID="txtempid" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="border:1px solid #86ef12;padding:5px 10px;"> Employ Name :</td>
<td style="border:1px solid #86ef12;"><asp:TextBox ID="txtempname" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="border:1px solid #86ef12;padding:5px 10px;"> Employ Salary :</td>
<td style="border:1px solid #86ef12;"><asp:TextBox ID="txtempsalary" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td colspan="4" style="padding:5px 10px; text-align:center;border:1px solid #86ef12;">
<asp:Button ID="btn_add" runat="server" Text=" ADD " onclick="btn_add_Click" />
</td>
</tr>
</table>
<br />
<asp:GridView ID="GridView1" runat="server" BackColor="White"
BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
ForeColor="Black" GridLines="Vertical">
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#006a4d" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#34b233" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="white" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>
</div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</form>
</body>
</html>
CODE背后:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace gridViewTest
{
public partial class WebForm2 : System.Web.UI.Page
{
public string cs = ConfigurationManager.ConnectionStrings["connect"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_add_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("EmpID");
dt.Columns.Add("EmpName");
dt.Columns.Add("EmpSal");
DataRow dr = null;
if (ViewState["emp"] != null)
{
for (int i = 0; i < 1; i++)
{
dt = (DataTable)ViewState["emp"];
if (dt.Rows.Count > 0)
{
dr = dt.NewRow();
dr["EmpID"] = txtempid.Text;
dr["EmpName"] = txtempname.Text;
dr["EmpSal"] = txtempsalary.Text;
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
else
{
dr = dt.NewRow();
dr["EmpID"] = txtempid.Text;
dr["EmpName"] = txtempname.Text;
dr["EmpSal"] = txtempsalary.Text;
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
}
ViewState["emp"] = dt;
}
protected void Button1_Click(object sender, EventArgs e)
{
//Create a new DataTable.
DataTable dtCustomers = new DataTable("Customers");
//Add columns to DataTable.
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
dtCustomers.Columns.Add(cell.Text);
}
//Loop through the GridView and copy rows.
foreach (GridViewRow row in GridView1.Rows)
{
dtCustomers.Rows.Add();
for (int i = 0; i < row.Cells.Count; i++)
{
dtCustomers.Rows[row.RowIndex][i] = row.Cells[i].Text;
}
}
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand submitData = new SqlCommand("insertEmp", con);
submitData.CommandType = CommandType.StoredProcedure;
submitData.Parameters.AddWithValue("@empInfo", dtCustomers);
con.Open();
try
{
submitData.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
con.Close();
}
}//end of button1_click
}
}