Hey guys, I need some help here for my Gridview in ASP.net. I am trying to make it editable, where someone can change a value and it will update in a database. However, I can't seem to pull the edited text. I keep getting the original text. I already searched with google and it did exactly what other people did, but it's still not giving me the edited text. Code is below for Gridview.
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1"
runat="server" OnRowDataBound="ItemDataBoundEventHandler1" bgcolor='#FFFBD6' onrowediting="GridView1_RowEditing"
onrowcancelingedit="GridView1_RowCancelingEdit" onrowupdating="GridView1_RowUpdating" >
<Columns>
<asp:commandfield showeditbutton="True" />
<asp:TemplateField HeaderText="CUSTCOL_16" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"CUSTCOL_16") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt" runat="server" Width="100px" Text='<%#DataBinder.Eval(Container.DataItem,"CUSTCOL_16") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
This is the script code.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridView2.aspx.cs" Inherits="WebApplication1.GridView2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<script runat="server">
//DataSet ds = new DataSet();
private void Page_Load(Object sender, EventArgs e)
{
Populate();
}
private void Populate()
{
SqlConnection dbconn = new SqlConnection();
dbconn.ConnectionString = "Data Source=comp1;Initial Catalog=DB1;" +
"Integrated Security=SSPI";
dbconn.Open();
SqlCommand dbcomm = new SqlCommand();
dbcomm.Connection = dbconn;
dbcomm.CommandTimeout = 0;
dbcomm.CommandType = CommandType.Text;
dbcomm.CommandText = "select * from dbo.adhoc";
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = dbcomm;
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
public void ItemDataBoundEventHandler1(object
sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover",
"this.style.backgroundColor='Silver'");
e.Row.Attributes.Add("onmouseout",
"this.style.backgroundColor='#FFFBD6'");
}
}
protected void GridView1_RowEditing(Object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex; //set to selected row
Populate();
}
protected void GridView1_RowCancelingEdit(Object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1; //set to no selection
Populate();
}
protected void GridView1_RowUpdating(Object sender, GridViewUpdateEventArgs e)
{
TextBox t = new TextBox(); ;
GridViewRow row = GridView1.Rows[e.RowIndex];
if (row != null)
{
t = (TextBox)GridView1.Rows[e.RowIndex].Cells[0].FindControl("txt");
if (t != null)
{
Response.Write("The Text Entered is" + t.Text);
//Trying to get this Response to have updated text, insted of original text. This is just for //testing purposes. After I know I am pulling the updated Text I will make it update //Database.
}
else
Response.Write("T IS NULL");
}
GridView1.DataBind();
}
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1"
runat="server" OnRowDataBound="ItemDataBoundEventHandler1" bgcolor='#FFFBD6' onrowediting="GridView1_RowEditing"
onrowcancelingedit="GridView1_RowCancelingEdit" onrowupdating="GridView1_RowUpdating" >
<Columns>
<asp:commandfield showeditbutton="True" />
<asp:TemplateField HeaderText="CUSTCOL_16" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"CUSTCOL_16") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt" runat="server" Width="100px" Text='<%#DataBinder.Eval(Container.DataItem,"CUSTCOL_16") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
This is the script code.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridView2.aspx.cs" Inherits="WebApplication1.GridView2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<script runat="server">
//DataSet ds = new DataSet();
private void Page_Load(Object sender, EventArgs e)
{
Populate();
}
private void Populate()
{
SqlConnection dbconn = new SqlConnection();
dbconn.ConnectionString = "Data Source=comp1;Initial Catalog=DB1;" +
"Integrated Security=SSPI";
dbconn.Open();
SqlCommand dbcomm = new SqlCommand();
dbcomm.Connection = dbconn;
dbcomm.CommandTimeout = 0;
dbcomm.CommandType = CommandType.Text;
dbcomm.CommandText = "select * from dbo.adhoc";
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = dbcomm;
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
public void ItemDataBoundEventHandler1(object
sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover",
"this.style.backgroundColor='Silver'");
e.Row.Attributes.Add("onmouseout",
"this.style.backgroundColor='#FFFBD6'");
}
}
protected void GridView1_RowEditing(Object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex; //set to selected row
Populate();
}
protected void GridView1_RowCancelingEdit(Object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1; //set to no selection
Populate();
}
protected void GridView1_RowUpdating(Object sender, GridViewUpdateEventArgs e)
{
TextBox t = new TextBox(); ;
GridViewRow row = GridView1.Rows[e.RowIndex];
if (row != null)
{
t = (TextBox)GridView1.Rows[e.RowIndex].Cells[0].FindControl("txt");
if (t != null)
{
Response.Write("The Text Entered is" + t.Text);
//Trying to get this Response to have updated text, insted of original text. This is just for //testing purposes. After I know I am pulling the updated Text I will make it update //Database.
}
else
Response.Write("T IS NULL");
}
GridView1.DataBind();
}