1

所以我的一个页面有这个问题,它基本上列出了指定学生的一堆评论。评论应该是可编辑的,但我在获取一行内容时遇到了问题(所以我可以在 a 中显示评论内容TextBox并允许编辑)。

问题是,每当我GridViewRowGridView这样的地方访问时: this.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);
        }
    }
4

1 回答 1

2

当您使用 a 时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;
于 2012-12-02T19:40:38.450 回答