2012-12-02 26 views
1

所以我有一个我的页面,这基本上列出了一堆指定学生的评论这个问题。评论应该是可编辑的,但我在获取一行内容时遇到问题(所以我可以在TextBox中显示评论内容并允许进行修改)。GridViewRow没有返回正确的单元格

的问题是,每当我从GridView像这样的访问GridViewRowthis.CommentList.Rows[e.NewEditIndex] 它返回细胞的集合,但每当我尝试访问该小区像这样:row.Cells[0].Text(其中选定的行GridViewRow对象)它不返回任何值。

这里是我的.aspx代码:

<asp:GridView ID="CommentList" runat="server" AutoGenerateColumns="False" CellPadding="5" 
    ShowHeader="false" Width="100%" DataKeyNames="CommentId" OnRowDeleting="CommentList_RowDeleting" 
    OnRowCancelingEdit="CommentList_RowCancelingEdit" OnRowEditing="CommentList_RowEditing"> 
    <Columns> 
     <asp:TemplateField HeaderText="Student ID"> 
      <ItemTemplate> 
       <b>Author:</b> 
       <%# ((Comment)Container.DataItem).Author.Name %> 
       <br /> 
       <b>Date:</b> 
       <%# ((Comment)Container.DataItem).Created %> 
       <hr style="border-top: solid 1px black" /> 
       <div style="text-align: center;"> 
        <asp:Panel ID="OptionsPanel" runat="server"> 
         <asp:LinkButton ID="DeleteLinkButton" CommandName="Delete" runat="server" OnClientClick="return confirm('Are you sure you want to delete this comment?');">Delete</asp:LinkButton> 
         | 
         <asp:LinkButton ID="EditLinkButton" runat="server" CommandName="Edit">Edit</asp:LinkButton> 
        </asp:Panel> 
        <asp:Panel ID="EditOptionsPanel" runat="server" Visible="false"> 
         <asp:LinkButton ID="CancelEditLinkButton" runat="server" CommandName="Cancel">Cancel</asp:LinkButton> 
        </asp:Panel> 
       </div> 
      </ItemTemplate> 
      <ItemStyle CssClass="topLeftJustify" Width="200px" /> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Name"> 
      <ItemTemplate> 
       <asp:Panel ID="ViewCommentPanel" runat="server"> 
        <%# ((Comment)Container.DataItem).Content%> 
       </asp:Panel> 
       <asp:Panel ID="EditCommentPanel" runat="server" Visible="false"> 
        <asp:TextBox ID="Comment" runat="server" CssClass="textEntry" TextMode="MultiLine" 
         Width="100%" Height="100px"></asp:TextBox> 
       </asp:Panel> 
      </ItemTemplate> 
      <ItemStyle CssClass="topLeftJustify" /> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

这里是我的.aspx.cs代码(即关于上面的代码):

protected void CommentList_RowEditing(object sender, GridViewEditEventArgs e) 
    { 
     try 
     { 
      GridViewRow row = this.CommentList.Rows[e.NewEditIndex]; 
      ((Panel)row.FindControl("OptionsPanel")).Visible = false; 
      ((Panel)row.FindControl("EditOptionsPanel")).Visible = true; 
      ((Panel)row.FindControl("ViewCommentPanel")).Visible = false; 
      ((Panel)row.FindControl("EditCommentPanel")).Visible = true; 

      // problem is with this line. it doesn't show the contents of the "comment" 
      ((TextBox)row.FindControl("Comment")).Text = row.Cells[0].Text; 
     } 
     catch (Exception ex) 
     { 
      this.Messages.ChangeMessage(ex.Message, MessageType.Error); 
     } 
    } 
+2

我编辑了自己的冠军。请参见“[应的问题包括‘标签’,在他们的头衔?(http://meta.stackexchange.com/questions/19190/)”,这里的共识是“不,他们不应该”。 –

回答

2

当您使用TemplateField不能使用e.Row.Cells[index].Text访问单元格的控件文本。我只是简单地将Label添加到面板。

但你也可以试试这种方法:

Panel ViewCommentPanel = (Panel) e.Row.FindControl("ViewCommentPanel"); 
LiteralControl objPanelText = ViewCommentPanel.Controls[0] as LiteralControl; 
TextBox Comment = (TextBox) row.FindControl("Comment"); 
Comment.Text = objPanelText.Text; 
+0

谢谢,我不知道这一点。 – TheAJ