سؤال

So I have this problem with one of my pages, which basically lists out a bunch of comments for a specified student. The comments are supposed to be editable, but I'm having a problem with getting the contents of a row (so I can display the comment content in a TextBox and allow for edits).

The issue is that whenever I access the GridViewRow from the GridView like such: this.CommentList.Rows[e.NewEditIndex] It is returning a collection of cells, but whenever I try access the a cell like so: row.Cells[0].Text (where row is the selected GridViewRow object) it doesn't return any values.

Here is my .aspx code:

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

Here is my .aspx.cs code (that relates to the code above):

    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);
        }
    }
هل كانت مفيدة؟

المحلول

When you use a TemplateField you cannot use e.Row.Cells[index].Text to access a control text of the cell. I would simply add a Label to the panel.

But you can also try it this way:

Panel ViewCommentPanel = (Panel) e.Row.FindControl("ViewCommentPanel");
LiteralControl objPanelText = ViewCommentPanel.Controls[0] as LiteralControl;
TextBox Comment = (TextBox) row.FindControl("Comment");
Comment.Text = objPanelText.Text;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top