尝试更新GridView单元格中的文本时出现异常行为

本文关键字:异常 文本 更新 GridView 单元格 | 更新日期: 2023-09-27 17:59:18

我正试图在RowDataBound事件中的GridView单元格中准备一些文本:

  protected void gvPatientSearch_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRow dr = ((DataRowView)e.Row.DataItem).Row;
            if (Convert.ToBoolean(dr["Confidential"]))
            {
                String name = String.Copy(e.Row.Cells[1].Text);
                //the problem is the following line
                e.Row.Cells[1].Text = "<b><font color='red'>!</font></b>" + name;
            }
        }
    }

出于某种原因,似乎无论我做什么,原文都被删除了。当我删除注释下面的行时,名称显示正常,没有感叹号;当我添加附加代码时,由于某种原因,原始文本变成了一个空白字符串,我只得到了感叹号。我试过使用和不使用String.Copy——我不认为这是必要的,但我想我会尝试一下,以防万一。运气不好。

我认为这不相关,但行中的第一列是一个隐藏列,其中包含1或0。如果值是1,我想加上感叹号。数据在那里,这个部分被正确地调用了,我只是无法获得保持不变的名称。

你知道为什么会发生这种事吗?

编辑:下方的前端代码

<asp:GridView ID="gvPatientSearch" runat="server" Width="100%" CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="True" AllowSorting="True"
                            PageSize="25" OnPageIndexChanging="gvPatientSearch_PageIndexChanging" OnSorting="gvPatientSearch_Sorting" OnRowDataBound="gvPatientSearch_RowDataBound"
                            AutoGenerateColumns="false" PagerSettings-Position="TopAndBottom" PagerStyle-HorizontalAlign="Right" PagerSettings-Mode="NumericFirstLast" PagerSettings-PageButtonCount="5"
                            PagerSettings-FirstPageText=" First " PagerSettings-LastPageText=" Last " PagerSettings-PreviousPageText=" Previous " PagerSettings-NextPageText=" Next ">
                            <AlternatingRowStyle BackColor="White" />
                            <EditRowStyle BackColor="#2461BF" />
                            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                            <PagerStyle BackColor="#2461BF" ForeColor="White" />
                            <RowStyle BackColor="#EFF3FB" />
                            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                            <SortedAscendingCellStyle BackColor="#F5F7FB" />
                            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                            <SortedDescendingCellStyle BackColor="#E9EBEF" />
                            <SortedDescendingHeaderStyle BackColor="#4870BE" />
                            <Columns>
                                <asp:BoundField HeaderText="Confidential" DataField="Confidential" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol"></asp:BoundField>
                                <asp:HyperLinkField DataTextField="PatientName" DataNavigateUrlFields="PatientIdInternal" DataNavigateUrlFormatString="PatientProfile.aspx?InternalID={0}" HeaderText="Patient Name"
                                    SortExpression="PatientName" />
                                <asp:BoundField DataField="PatientID" HeaderText="Patient ID" SortExpression="PatientID" />
                                <asp:BoundField DataField="AccountNumber" HeaderText="Account Number" SortExpression="AccountNumber" />
                                <asp:BoundField DataField="DateOfBirth" HeaderText="Date Of Birth" SortExpression="DateOfBirth" DataFormatString="{0:d}" />
                                <asp:BoundField DataField="PracticeName" HeaderText="Practice Name" SortExpression="PracticeName" />
                                <asp:BoundField DataField="PatientIdInternal" HeaderText="Internal ID" SortExpression="PatientIdInternal" Visible="false" />
                            </Columns>
                        </asp:GridView>

在代码的这一部分,我可以看到Visual Studio中正确显示的所有其他字段的值,只有我试图修改的名称字段显示为空字符串。

尝试更新GridView单元格中的文本时出现异常行为

HyperLinkField被呈现为超链接控件,因此您无法直接检索单元格的文本。如果您想要文本,您必须从控件中检索,而不是从单元格:中检索

HyperLink myLink = e.Row.Cells[1].Controls[0] as HyperLink;
String name = myLink.text;

另一种选择是通过在代码后面执行DataBinder.Eval(e.Row.DataItem, "name"),直接从数据源获取名称。

我不知道为什么会发生这种情况,但我认为你可以通过使用这样的函数应用逻辑来解决它:

public string Exclaim(bool confidential, string originalText)
{
    if (confidential) {
        return "<b><font color='red'>!</font></b>" + originalText;
    } else {
        return originalText;
    }
}

在您的网格视图中,使用以下命令调用函数:

<asp:TemplateField>
    <ItemTemplate>
        <%# Exclaim(Eval("Confidential"), Eval("Name"))%>
    </ItemTemplate>
</asp:TemplateField>

我认为这会奏效。

我继续进行,并将有问题的行更改为:

e.Row.Cells[1].Text = "<b>font color='red'>!</font></b> " + Convert.ToString(dr["PatientName"]);

仍然不知道为什么另一种方式不起作用,如果其他人起了作用,我会把它标记为正确的。