2013-05-07 50 views
1

我有一个gridview使用列表作为它的数据源,这个列表填充使用实体框架,并传递给我的方法,这里的数据绑定到我的网格视图控件。我似乎在编辑一行时遇到问题。GridView的OnRowEditing事件处理程序没有触发

在设计师,我已经添加属性网格视图有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 

    } 

我注意到最后一件事是,当我将鼠标悬停在编辑行LinkBut​​ton的,没有查询字符串,同我在分页网格的底部和我的标题。点击任何网格控件的用户转到:

http://localhost:8192/website/Company 

,而不是

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

摘要

我的GridView控件的事件处理程序不火。我错过了什么来完成这项工作?

+0

只是curiouse,做我的回答帮助你出来呢?你有没有弄清楚这个标记是什么? – jadarnel27 2013-05-22 17:01:21

回答

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