gopinath

gopinath

  • NA
  • 3
  • 0

Having Problem in DATAGRID in ASP.NET

Aug 8 2005 8:04 AM
  
Hi,

Here is my code  for default.aspx.cs:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        bind_data();
       
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
      
    }
    protected void x_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void editdata(object source, DataGridCommandEventArgs e)
    {
        x.EditItemIndex = e.Item.ItemIndex;
        bind_data();
    }
    public void bind_data()
    {
        SqlConnection conn = new SqlConnection("server=CHN-W2K-SYS067;uid=sa;pwd=;Database=pubs");
        conn.Open();
        SqlCommand cmd = new SqlCommand("SELECT * FROM Titles", conn);
        x.DataSource = cmd.ExecuteReader();
        x.DataBind();
    }
    protected void canceldata(object source, DataGridCommandEventArgs e)
    {
        x.EditItemIndex = -1;
        bind_data();
    }
    protected void updatedata(object source, DataGridCommandEventArgs e)
    {
        TextBox newtitle = new TextBox();
       // string titleid = x.DataKeys[e.Item.ItemIndex].ToString();
        newtitle =(TextBox) e.Item.Cells[0].Controls[0];
        Response.Write(newtitle.Text);
       
    }
}

Here is the code for default.aspx :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<asp:DataGrid ID="x" AutoGenerateColumns="false" DataKeyField="title_id" runat="server" OnEditCommand="editdata" OnCancelCommand="canceldata" OnUpdateCommand="updatedata" OnSelectedIndexChanged="x_SelectedIndexChanged" GridLines="Horizontal" HorizontalAlign="Left" ShowFooter="True">

<Columns>

<asp:BoundColumn HeaderText="Title" DataField="Title" />

<asp:EditCommandColumn HeaderText="Edit" EditText="Edit" UpdateText="Update" CancelText="Cancel"></asp:EditCommandColumn>

<asp:ButtonColumn CommandName="Delete" Text="Delete"></asp:ButtonColumn>

</Columns>

<AlternatingItemStyle BackColor="#C0FFFF" />

<ItemStyle BackColor="#00C0C0" BorderColor="Black" />

</asp:DataGrid>

</form>

</body>

</html>

After editing the title in text box, If click update button, I am not able to get the updated title. I am getting only original title.
What may be the problem?

Thanks,
Gopinath.D


Answers (2)