表employeeTable包含
EmployeeID int主键。 FirstName varchar(50), LastName varchar(50), 标题varchar(50), 国家varchar(50)
创建了GridView编辑,删除和更新功能。 但点击更新时它没有更新。 我的代码:
<asp:GridView ID="GridView1" runat="server" GridLines="None" AutoGenerateColumns="false"
AlternatingRowStyle-BackColor="#EEEEEE" EditRowStyle-BorderColor="Red"
onrowcancelingedit="GridView1_RowCancelling"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating" DataKeyNames="EmployeeID">
<Columns>
<asp:TemplateField Visible="false" HeaderText="EmployeeID">
<ItemTemplate>
<asp:Label runat="server" ID="EmployeeID" Text='<%#Eval("EmployeeID")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LastName">
<ItemTemplate>
<asp:Label runat="server" ID="LastName" Text='<%#Eval("LastName") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtLastName" Text='<%#Eval("LastName") %>' />
<asp:RequiredFieldValidator runat="server" ID="rfdLastName"
ControlToValidate="txtLastName" ValidationGroup="var1" ErrorMessage="*" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title">
<ItemTemplate>
<asp:Label runat="server" ID="Title" Text='<%#Eval("Title") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtTitle" Text='<%#Eval("Title") %>' />
<asp:RequiredFieldValidator runat="server" ID="rfdTitle"
ControlToValidate="txtTitle" ValidationGroup="var1" ErrorMessage="*" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:Label runat="server" ID="Country" Text='<%#Eval("country") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtCountry" Text='<%#Eval("country") %>' />
<asp:RequiredFieldValidator runat="server" ID="rfdCountry"
ControlToValidate="txtCountry" ValidationGroup="var1" ErrorMessage="*" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandName="Edit" />
<br />
<asp:LinkButton ID="btnDelete" Text="Delete" runat="server" CommandName="Delete" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="btnUpdate" Text="Update" runat="server" CommandName="Update" />
<asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
我的c#代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
namespace new1
{
public partial class WebForm7 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings ["vinkpConnectionString"].ConnectionString);
conn.Open();
SqlCommand comm = new SqlCommand("select EmployeeID,FirstName,LastName,Title,Country from employeeTable ", conn);
comm.ExecuteReader();
conn.Close();
SqlDataAdapter da = new SqlDataAdapter(comm);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGridData();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string s = GridView1.DataKeys[e.RowIndex].Value.ToString();
Label EmployeeID = GridView1.Rows[e.RowIndex].FindControl("EmployeeID") as Label;
TextBox LastName = GridView1.Rows[e.RowIndex].FindControl("txtLastName") as TextBox;
TextBox Title = GridView1.Rows[e.RowIndex].FindControl("txtTitle") as TextBox;
TextBox Country = GridView1.Rows[e.RowIndex].FindControl("txtCountry") as TextBox;
String UpdateQuery = string.Format("UPDATE employeeTable SET LastName,Title,Country WHERE EmployeeID='"+Convert.ToInt32(EmployeeID.Text)+"'",LastName.Text, Title.Text, Country.Text);
GridView1.EditIndex = -1;
BindGridData(UpdateQuery);
}
private void BindGridData(string Query)
{
string connectionstring = ConfigurationManager.ConnectionStrings["vinkpConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connectionstring))
{
conn.Open();
using (SqlCommand comm = new SqlCommand(Query +";select EmployeeID,FirstName,LastName,Title,Country from employeeTable ", conn))
{
SqlDataAdapter da = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
protected void GridView1_RowCancelling(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGridData();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string EmployeeID = GridView1.DataKeys[e.RowIndex].Value.ToString();
string Query = "delete employeeTable where EmployeeID = " + EmployeeID;
BindGridData(Query);
}
private void BindGridData()
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["vinkpConnectionString"].ConnectionString))
{
conn.Open();
using (SqlCommand comm = new SqlCommand("select EmployeeID,FirstName,LastName,Title,Country from employeeTable ", conn))
{
SqlDataAdapter da = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
}
为什么会出现错误?代码中的任何错误?当单击编辑文本框时出现。但是单击更新没有在Gridview中显示更新。
答案 0 :(得分:0)
每次触发page_load事件时,您都会在Page_Load中绑定GridView1,这会导致GridView
postback
上的更改丢失。绑定!IsPostBack
块中的网格以在回发时保持其状态
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings ["vinkpConnectionString"].ConnectionString);
conn.Open();
SqlCommand comm = new SqlCommand("select EmployeeID,FirstName,LastName,Title,Country from employeeTable ", conn);
comm.ExecuteReader();
conn.Close();
SqlDataAdapter da = new SqlDataAdapter(comm);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}