Gridview OnRowEditing事件处理程序未触发

时间:2013-05-07 13:46:18

标签: asp.net list gridview

我有一个使用list作为数据源的gridview,这个列表使用实体框架填充并传递给我的方法,其中这些数据被绑定到我的网格视图控件。我似乎在编辑行时遇到问题。

在设计器上我添加了网格视图的属性以具有OnRowEditing处理程序,并且我添加了一个用于编辑的按钮,但我的OnRowEditing事件处理程序未触发。断点没有击中。

我的Gridview控件

<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>

代码

我从Page_Init上的实体框架中检索数据。我也有全局变量

 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();

    }

我的绑定方法

 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

    }

我注意到的最后一件事是,当我将鼠标悬停在编辑行linkbutton上时,没有查询字符串,与我在网格底部的分页和标题相同。单击任何网格控件,用户可以:

http://localhost:8192/website/Company

而不是

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

摘要

我的gridview事件处理程序不会触发。我错过了一些让这项工作的东西吗?

1 个答案:

答案 0 :(得分:1)

您需要移动此代码:

GenerateGrid(list);

if(!Page.IsPostBack)区块内。

每次页面回发到服务器时(例如,当您单击编辑按钮时),此代码正在重建您的GridView以恢复其原始状态。这不允许RowEditing事件发生,因为你已经基本上销毁它并在Init期间重新添加它(在它有可能发生之前)。


再看一下你的代码,看来你正在使用IsPostBack来确定网格的内容。您需要修改该逻辑才能使其正常工作。也许您可以检查传递的查询字符串的内容(或查询字符串中/个字符的数量),以确定要传递给GetBasicNoteResults方法的参数。

您的代码基本上如下所示:

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);
}
相关问题