2013-04-06 42 views
0

我遇到的問題是我的GridView的RowCommand不會觸發。我讀過數百萬篇文章,結果我覺得我更加困惑。所以,如果你能看到具體是什麼,我在這裏做錯了,請指出。內容頁面上的Gridview RowCommand不會觸發 - ViewState?

幾周前我曾問過類似的問題,但我使用了一個嵌套在數據列表中的gridview,並使用EntityDataSource的'包括'兄弟來顯示兄弟姐妹,它們是引用關係的多方面,顯示效果不錯,但搞清楚編輯更新和刪除是否是噩夢。所以我把它簡化了一下,但現在我停下來了,因爲我在回發的某個地方失去了控制。

我有一個母版頁用的ContentPlaceHolder:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.Master.cs" Inherits="HomelessStudent.Web.SiteMasterPage" ViewStateMode="Inherit" EnableViewState="True" %> 
     <asp:ContentPlaceHolder ID="ContentPlaceHolderAgent" runat="server"> 
     </asp:ContentPlaceHolder> 

在代理PAGE-

<asp:Content ID="Content" ContentPlaceHolderID="ContentPlaceHolderAgent" runat="server"> 
<asp:Panel ID="SiblingPanel" runat="server" ViewStateMode="Enabled" Visible="True">  
    <asp:GridView ID="SiblingGridView" runat="server" CssClass="grid" 
     AutoGenerateColumns="false" 
     DataKeyNames="Id" 
     ShowFooter="true" 
     OnDataBound="SiblingGridView_DataBound" 
     OnRowEditing="SiblingGridView_RowEditing" 
     OnRowUpdating="SiblingGridView_RowUpdating" 
     OnRowCommand="SiblingGridView_RowCommand" 
     OnRowDeleting="SiblingGridView_RowDeleting" ViewStateMode="Enabled" ClientIDMode="Static"> 
     <Columns> 
      <asp:TemplateField HeaderText="Name" SortExpression="SiblingName"> 
       <EditItemTemplate> 
        <asp:TextBox runat="server" Text='<%# Bind("SiblingName") %>' ID="TextBox1"></asp:TextBox> 
       </EditItemTemplate> 
       <ItemTemplate> 
        <asp:Label runat="server" Text='<%# Bind("SiblingName") %>' ID="Label1"></asp:Label> 
       </ItemTemplate> 
       <FooterTemplate> 
        <asp:TextBox runat="server" ID="NewSiblingName" ></asp:TextBox> 
       </FooterTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="Edit" ShowHeader="false"> 
       <EditItemTemplate> 
        <asp:LinkButton runat="server" Text="Update" CommandName="Update" ID="UpdateLinkButton" ></asp:LinkButton>&nbsp;<asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" CausesValidation="False" ID="LinkButton2"></asp:LinkButton> 
       </EditItemTemplate> 
       <ItemTemplate> 
        <asp:LinkButton runat="server" Text="Edit" CommandName="Edit" ID="EditLinkButton" ClientIDMode="Static"></asp:LinkButton> 
       </ItemTemplate> 
       <FooterTemplate> 
        <asp:LinkButton runat="server" CommandName="AddNew" Text="Add" ID="AddNewLinkButton"></asp:LinkButton>     </FooterTemplate> 
      </asp:TemplateField> 
      <asp:CommandField ShowDeleteButton="True" ShowHeader="True" HeaderText="Delete"></asp:CommandField> 
     </Columns> 
    </asp:GridView> 
</asp:Panel> 

後面的代碼(數據綁定在這裏,因爲只顯示屬於那些兄弟姐妹一個特定的記錄,該記錄的ID(Guid)在queryString中,也許...

  if (IsPostBack == false) 
     { 
      if (Request.QueryString["id"] == null) 
      { 
       if (Session["studentid"] == null) 
       { 
        Response.Redirect("StudentPage.aspx"); 
       } 
       else 
       { 
        referral = GetMostRecentReferral((String)Session["studentid"]); 
        PopulateUI(referral); 
       } 

      } 
      else 
      { 
       referral = GetThisReferral(Request.QueryString["id"]); 
       PopulateUI(referral); 
      } 
     } 

然後,在PopulateUI(轉診)

some stuff... 
FillSiblingGrid(); 
String studentId = ((referral.StudentID).ToString()).Trim(); 
getSelectedStudentDeatails(studentId); 
Session["referralid"] = (Guid)referral.Id; 

,並填寫兄弟姐妹併網

 private void FillSiblingGrid() 
    { 
     if (referral != null) 
     { 
      List<Sibling> siblings = new List<Sibling>(); 
      using (HomelessStudentDataEntities db = new HomelessStudentDataEntities()) 
      { 
       siblings = (from s in db.Siblings 
          where s.ReferralID == referral.Id 
          select s).ToList(); 
      } 
      if (siblings.Count > 0) 
      { 
       SiblingGridView.DataSource = siblings; 
       SiblingGridView.DataBind(); 
      } 
      else 
      { 
       int TotalColumns = SiblingGridView.Rows[0].Cells.Count; 
       SiblingGridView.Rows[0].Cells.Clear(); 
       SiblingGridView.Rows[0].Cells.Add(new TableCell()); 
       SiblingGridView.Rows[0].Cells[0].ColumnSpan = TotalColumns; 
       SiblingGridView.Rows[0].Cells[0].Text = "No siblings found"; 
      } 
     } 
    } 
+0

我懷疑問題出在ViewState上,所以我會將其添加到我的問題中。 – Jazzy 2013-04-06 17:02:46

+0

我花了一輩子的時間試圖弄明白這一點。事實證明,我曾愚蠢地重新請求Site.Master中的同一頁面,因此刪除了ViewState。一旦我糾正了這一情況,內容頁面就會按預期行事。 – Jazzy 2013-04-08 23:04:54

回答

0

我所面臨的類似問題。啓用網格視圖狀態解決了問題

相關問題