1
votes

I have a gridview that uses list as it's datasource, this list is populated using entity framework and passed to my method where this data is binded to my grid view control. I seem to be having a problem with editing a row.

On designer I have added properties for the grid view to have a OnRowEditing handler and I have added a button for edit but my OnRowEditing event handler isn't firing. The breakpoint doesn't hit.

My Gridview control

<asp:GridView runat="server" 
            ID="grdNotes" 
            OnRowCommand="grdNotes_RowCommand"
            AllowPaging="true" 
            AllowSorting="true" 
            EnableTheming="true" 
            AutoGenerateColumns="false" 
            OnPageIndexChanging="grdNotes_PageIndexChanging" 
            OnSorting="grdNotes_Sorting"
            AlternatingRowStyle-BorderColor="Yellow" 
            PageSize="3" 
            AlternatingRowStyle-BackColor="Yellow"
            OnRowEditing="grdNotes_RowEditing" 
            OnRowDeleting="grdNotes_RowDeleting" 
            DataKeyNames="NotesID" >
            <Columns>
                <asp:BoundField HeaderText="Title" DataField="NotesTitle" SortExpression="NotesTitle">
                    <ItemStyle Height="20px" Width="150px" />
                </asp:BoundField>

                <asp:BoundField HeaderText="Text" DataField="NotesText" SortExpression="NotesText">
                    <ItemStyle Height="20px" Width="250px" />
                </asp:BoundField>

          <%--      <asp:ButtonField CommandName="EditRow" DataTextField="Edit" HeaderText="Edit" />
                <asp:ButtonField CommandName="DeleteRow" DataTextField="Delete" HeaderText="Delete" />--%>

                <asp:CommandField ShowEditButton="true" />
                <asp:CommandField ShowDeleteButton="true" />
                <asp:CommandField ShowCancelButton="true" />
            </Columns>

        </asp:GridView>

Code behind

I retrieve the data from entity framework on Page_Init. I also have global variables

 private List<NotesModel> list = new List<NotesModel>();
 NotesModel nm = new NotesModel();

protected void Page_Init(object sender, EventArgs e)
    {
        NoteSearch ns = new NoteSearch(Business.ContextHelper.CurrentContext);
        string[] urlArray = Request.RawUrl.Split('/');
        string t = urlArray[4];
        string[] relatedID = t.Split('=');
        if (!IsPostBack)
        {
            //  urlArray[3] is profile type , relatedID[1] is ID
            list = ns.GetBasicNoteResults(nm, urlArray[3], relatedID[1]);
        }
        else
        {
            urlArray = Request.UrlReferrer.AbsoluteUri.Split('/');
            t = urlArray[6];
            relatedID = t.Split('=');

            list = ns.GetBasicNoteResults(nm, urlArray[5], relatedID[1]);
        }
        GenerateGrid(list);

        btnNotes.Text = "Notes: " + list.Count.ToString();

    }

My binding method

 private void GenerateGrid(List<NotesModel> list)
    {

        grdNotes.DataSource = list;
        grdNotes.DataBind();

        int count = grdNotes.Rows.Count;

        //// Hide headers we don't want to expose
        //grdNotes.HeaderRow.Cells[0].Visible = false;
        //grdNotes.HeaderRow.Cells[3].Visible = false;
        //grdNotes.HeaderRow.Cells[4].Visible = false;
        //grdNotes.HeaderRow.Cells[5].Visible = false;

        //for (int i = 0; i < count; i++)
        //{
        //    // Loop through rows and hide cells
        //    grdNotes.Rows[i].Cells[0].Visible = false;
        //    grdNotes.Rows[i].Cells[3].Visible = false;
        //    grdNotes.Rows[i].Cells[4].Visible = false;
        //    grdNotes.Rows[i].Cells[5].Visible = false;
        //}

        // Finally add edit/delete buttons for these click event handlers

    }

One final thing I noticed is when I hover over the edit row linkbutton, there is no query string, same with my paging at the bottom of the grid and my headers. Clicking on any of the grid controls take the user to:

http://localhost:8192/website/Company

and not

http://localhost:8192/website/Company/Advertiser/?id=8879

Summary

My gridview event handlers don't fire. Have I missed something to make this work?

1
Just curiouse, did my answer help you out at all? Did you ever figure out what the rpoblem was? - Josh Darnell

1 Answers

1
votes

You need to move this code:

GenerateGrid(list);

Inside the if(!Page.IsPostBack) block.

Each time your page posts back to the server (e.g. when you click the edit button), this code is rebuilding your GridView back to it's original state. This doesn't allow the RowEditing event to even occur, because you've essentially destroyed it and re-added it during Init (before it has a chance to occur).


Looking at your code some more, it appears you are using IsPostBack to determine the contents of the grid. You will need to modify that logic in order for this to work. Perhaps you can examine the contents of the query string being passed (or the number of / characters in the query string) to decide what parameters to pass to the GetBasicNoteResults method.

Your code will basically look like this:

if (!IsPostBack)
{
    if (Some logic to decide what parameters to pass)
    {
        list = ns.GetBasicNoteResults(nm, urlArray[3], relatedID[1]);
    }
    else
    {
        list = ns.GetBasicNoteResults(nm, urlArray[5], relatedID[1]);
    }
    GenerateGrid(list);
}