Hi all,
I'm using a datagrid that connects to a sql server database. The datagrid has paging enabled and has an edit column aswell.
The problem i have is this, when I click on the edit link the text box appear and i'm able to change the information but when I click on the update link the value reverts to what it was prior to the change.
I've seen other posts that suggest putting the DataGrid1.DataBind() into an if(!Page.IsPostBack) but that doesn't seem to work. If anything this seems to make things worse. When i click on either the Paging links or the edit links the datagrid disappears.
Any help is greatly appreciated - i have a feeling its something really simple.
----------------- ASPX Page -----------------
<%@ Page language="c#" Codebehind="view.aspx.cs" AutoEventWireup="false" Inherits="cms.hr.payscales.view" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>View all pay scales</title>
<meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content=" name="vs_targetSchema">
<link href=" type="text/css" rel="stylesheet">
</HEAD>
<body MS_POSITIONING="FlowLayout">
<form id="view" method="post" runat="server">
<asp:datagrid OnPageIndexChanged="DataGrid1_PageIndexChanged" OnCancelCommand="DataGrid1_CancelCommand" OnEditCommand="DataGrid1_EditCommand" OnUpdateCommand="DataGrid1_UpdateCommand" AllowPaging="true" PageSize="7" PagerStyle-Mode="NumericPages" PagerStyle-PageButtonCount="3" id="DataGrid1" runat="server" Width="60%" AutoGenerateColumns="False" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" BackColor="White" CellPadding="3" GridLines="Vertical" ForeColor="Black">
<AlternatingItemStyle BorderStyle="None" BackColor="#CCCCCC"></AlternatingItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="Black"></HeaderStyle>
<FooterStyle BackColor="#CCCCCC"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="Title" HeaderText="Grade Title"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Union">
<ItemTemplate>
<asp:Label ID="Union" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Union" %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" HeaderText="Edit" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
</Columns>
<PagerStyle HorizontalAlign="Left" ForeColor="Black" BackColor="#999999" PageButtonCount="4" Mode="NumericPages"></PagerStyle>
</asp:datagrid></form>
</body>
</HTML>
------------------- ASPX.CS -------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace cms.hr.payscales
{
/// <summary>
/// Summary description for view.
/// </summary>
public class view : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Data.SqlClient.SqlConnection sqlConn;
protected System.Data.SqlClient.SqlDataAdapter sqlDA;
protected System.Data.DataSet ds1;
private void Page_Load(object sender, System.EventArgs e)
{
BindGrid();
}
public void BindGrid(){
this.sqlConn = new SqlConnection("data source=localhost;initial catalog=hr payscales; integrated security=SSPI"
sqlConn.Open();
sqlDA = new SqlDataAdapter("select distinct Title, [Union] from Payscales", sqlConn);
ds1 = new DataSet();
sqlDA.Fill(this.ds1, "Payscales"
DataGrid1.DataSource = ds1;
DataGrid1.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
public void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
}
public void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
DataGrid1.DataBind();
}
public void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
// do sql update
TextBox tbTemp = (TextBox)e.Item.Cells[0].Controls[0];
this.sqlConn = new SqlConnection("data source=localhost;initial catalog=hr payscales; integrated security=SSPI"
string strCommand = "update Payscales set Title = '" + tbTemp.Text + "' where Title = 'test'";
SqlCommand update = new SqlCommand(strCommand, sqlConn);
this.sqlConn.Open();
update.ExecuteNonQuery();
this.sqlConn.Close();
DataGrid1.EditItemIndex = -1;
DataGrid1.DataBind();
}
public void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
DataGrid1.DataBind();
}
}
}
I'm using a datagrid that connects to a sql server database. The datagrid has paging enabled and has an edit column aswell.
The problem i have is this, when I click on the edit link the text box appear and i'm able to change the information but when I click on the update link the value reverts to what it was prior to the change.
I've seen other posts that suggest putting the DataGrid1.DataBind() into an if(!Page.IsPostBack) but that doesn't seem to work. If anything this seems to make things worse. When i click on either the Paging links or the edit links the datagrid disappears.
Any help is greatly appreciated - i have a feeling its something really simple.
----------------- ASPX Page -----------------
<%@ Page language="c#" Codebehind="view.aspx.cs" AutoEventWireup="false" Inherits="cms.hr.payscales.view" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>View all pay scales</title>
<meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content=" name="vs_targetSchema">
<link href=" type="text/css" rel="stylesheet">
</HEAD>
<body MS_POSITIONING="FlowLayout">
<form id="view" method="post" runat="server">
<asp:datagrid OnPageIndexChanged="DataGrid1_PageIndexChanged" OnCancelCommand="DataGrid1_CancelCommand" OnEditCommand="DataGrid1_EditCommand" OnUpdateCommand="DataGrid1_UpdateCommand" AllowPaging="true" PageSize="7" PagerStyle-Mode="NumericPages" PagerStyle-PageButtonCount="3" id="DataGrid1" runat="server" Width="60%" AutoGenerateColumns="False" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" BackColor="White" CellPadding="3" GridLines="Vertical" ForeColor="Black">
<AlternatingItemStyle BorderStyle="None" BackColor="#CCCCCC"></AlternatingItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="Black"></HeaderStyle>
<FooterStyle BackColor="#CCCCCC"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="Title" HeaderText="Grade Title"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Union">
<ItemTemplate>
<asp:Label ID="Union" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Union" %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" HeaderText="Edit" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
</Columns>
<PagerStyle HorizontalAlign="Left" ForeColor="Black" BackColor="#999999" PageButtonCount="4" Mode="NumericPages"></PagerStyle>
</asp:datagrid></form>
</body>
</HTML>
------------------- ASPX.CS -------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace cms.hr.payscales
{
/// <summary>
/// Summary description for view.
/// </summary>
public class view : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Data.SqlClient.SqlConnection sqlConn;
protected System.Data.SqlClient.SqlDataAdapter sqlDA;
protected System.Data.DataSet ds1;
private void Page_Load(object sender, System.EventArgs e)
{
BindGrid();
}
public void BindGrid(){
this.sqlConn = new SqlConnection("data source=localhost;initial catalog=hr payscales; integrated security=SSPI"
sqlConn.Open();
sqlDA = new SqlDataAdapter("select distinct Title, [Union] from Payscales", sqlConn);
ds1 = new DataSet();
sqlDA.Fill(this.ds1, "Payscales"
DataGrid1.DataSource = ds1;
DataGrid1.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
public void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
}
public void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
DataGrid1.DataBind();
}
public void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
// do sql update
TextBox tbTemp = (TextBox)e.Item.Cells[0].Controls[0];
this.sqlConn = new SqlConnection("data source=localhost;initial catalog=hr payscales; integrated security=SSPI"
string strCommand = "update Payscales set Title = '" + tbTemp.Text + "' where Title = 'test'";
SqlCommand update = new SqlCommand(strCommand, sqlConn);
this.sqlConn.Open();
update.ExecuteNonQuery();
this.sqlConn.Close();
DataGrid1.EditItemIndex = -1;
DataGrid1.DataBind();
}
public void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
DataGrid1.DataBind();
}
}
}