0

I am having problem with implementing editable GridView using ObjectDataSource on CodeBehind; I wonder what missing pieces I need to add in my code behind (events):

ASPX Code:

<asp:GridView ID="grdPlayer" runat="server" AutoGenerateColumns="False" 
            AutoGenerateEditButton="true" AutoGenerateDeleteButton="true" 
            onrowediting="grdPlayer_RowEditing" 
            onrowcancelingedit="grdPlayer_RowCancelingEdit" 
onrowupdating="grdPlayer_RowUpdating">
          <Columns>
                <asp:BoundField DataField="ID" Visible="false"/>
                <asp:BoundField DataField="FirstName" HeaderText="First Name" 
                    SortExpression="FirstName" />  
                <asp:BoundField DataField="LastName" HeaderText="LastName"   
                    SortExpression="LastName" />  
                <asp:BoundField DataField="Age" HeaderText="Age"   
                    SortExpression="Age" />  

          </Columns>  


        </asp:GridView>

ASPX.CS Code Behind:

  using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;

    public partial class _Default : System.Web.UI.Page
    {
        ObjectDataSource dataSource = new ObjectDataSource();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                dataSource.TypeName = "Cricket.CricketBL";
                dataSource.UpdateMethod = "UpdatePlayer";
                Parameter p1 = new Parameter("ID", DbType.Int32);
                Parameter p2 = new Parameter("FName", DbType.String);
                Parameter p3 = new Parameter("LName", DbType.String);
                Parameter p4 = new Parameter("Age", DbType.Int32);
                dataSource.UpdateParameters.Add(p1);
                dataSource.UpdateParameters.Add(p2);
                dataSource.UpdateParameters.Add(p3);
                dataSource.UpdateParameters.Add(p4);

                bindGridView();

            }
        }

        #region Grid Events
        protected void grdPlayer_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grdPlayer.EditIndex = e.NewEditIndex;
        }

        protected void grdPlayer_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            grdPlayer.EditIndex = -1;
            bindGridView();


        }



        protected void grdPlayer_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grdPlayer.EditIndex = -1;
            bindGridView();
        }
        #endregion

        void bindGridView()
        {
            dataSource.SelectMethod = "GetAllPlayer";
            dataSource.TypeName = "Cricket.CricketBL";
            grdPlayer.DataSource = dataSource;
            grdPlayer.DataBind();
        }
    }
  1. Where should I put UpdateMethod code (currently in Page_Load)?
  2. Which events should I implement in code behind so that my edited grid row calls UpdateMethod with correct NewValues?
  3. When I click on Edit link it does not render GridView in Edit mode (text boxes,etc); I have to click it twice to do so; why?
  4. BIG QUESTION: Clicking on Update link does not invoke ObjectDataSource UpdateMethod?
zee
  • 221
  • 1
  • 3
  • 8

2 Answers2

0

Zee,

Your approach is only good for learning phase. For the best practice or in the production phase, we should always make a clear separation between several layers of the application. They are chiefly Data Access Layer(DAL), Business Logic Layer(BLL) and Presentation Layer(In Web Pages). Such concept is often called persistence ignorance. As explained here

The CRUD methods that call the repository class and the two constructors make it possible to use the business-logic class with whatever back-end data store you choose. The business-logic class does not need to be aware of how the class that it's calling persists the data. (This is often called persistence ignorance.)

I strongly recommend you to go through this awesome tutorial and continue working further and you will be enlightened!

Neo182
  • 612
  • 2
  • 10
  • 21
0

Zee,

To answer your 4 questions.

1 AND 4:

Add "onRowUpdated" to your gridview properties in your .aspx file. Say onRowUpdated="grdPlayer_RowUpdated". Add your code for Updating here.

2 AND 3:

Instead of using a BoundField, why not use a TemplateField. It would have EditItemTemplate and ItemTemplate like this:

<asp:TemplateField HeaderText="FirstName">
    <EditItemTemplate>
        <asp:TextBox ID="FName" runat="server" Text='<%# Bind("FirstName")%>'></asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="Label2" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="LastName">
    <EditItemTemplate>
        <asp:TextBox ID="LName" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="Label2" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age">
    <EditItemTemplate>
        <asp:TextBox ID="Age" runat="server" Text='<%# Bind("Age") %>'></asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="Label2" runat="server" Text='<%# Bind("Age") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

In your "grdPlayer_RowUpdating" method, add this:

int index = grdPlayer.EditIndex;

GridViewRow row = grdPlayer.Rows[index];
Int64 ID = (Int64)grdPlayer.DataKeys[index].Value;

TextBox FirstName = (TextBox)row.FindControl("FName");
TextBox Lastname = (TextBox)row.FindControl("LName");
TextBox Age = (TextBox)row.FindControl("Age");

e.NewValues["FirstName"] = FirstName.Text;
e.NewValues["LastName"] = Lastname.Text;
e.NewValues["Age"] = Age.Text;

I hope this helps.