ASP.NET listview更改选定的行颜色

时间:2017-07-04 10:32:48

标签: c# asp.net

我想要做的是:当我点击“查看”按钮时,应突出显示所选行。

我用Google搜索了几个小时但却无法理解我错过的内容:

<tr height="30%">
    <td valign="top">
        <div runat="server" id="area" style="height:300px; overflow:scroll">
            <asp:Label ID="lbl_Result" runat="server"></asp:Label>
            <asp:ListView ID="lv_MessageSubject" runat="server" vertical-align="top" OnSelectedIndexChanged="lv_MessageSubject_SelectedIndexChanged" OnSelectedIndexChanging="lv_MessageSubject_SelectedIndexChanging" DataKeyNames ="MessageID" OnPagePropertiesChanged="lv_MessageSubject_PagePropertiesChanged" OnPagePropertiesChanging="lv_MessageSubject_PagePropertiesChanging" OnItemDataBound="lv_MessageSubject_ItemDataBound">
            <LayoutTemplate>
            <table border="1">
                <tr runat="server">
                    <th runat="server"></th>
                    <th runat="server">Message</th>
                    <th runat="server">Sender</th>
                </tr>
                <tr runat="server" id="itemPlaceholder" />
            </table>
        </LayoutTemplate>
        <ItemTemplate>
            <tr>
            <td><asp:Button ID="cmd_View" runat="server" CommandName="Select" Text="View" Height="21px" /></td>
            <td><div runat="server" id="area"><asp:Label ID="Label2" runat="server" Text='<%#Eval("Message") %>' /></div></td>
            <td><asp:Label ID="Label3" runat="server" Text='<%#Eval("Sender") %>' /></td>
            </tr>
        </ItemTemplate>
        </asp:ListView>
        <asp:DataPager ID="DataPager" runat="server" PagedControlID="lv_MessageSubject"
            PageSize="10">
            <Fields>
                <asp:NextPreviousPagerField ShowFirstPageButton="True" ShowNextPageButton="False" />
                <asp:NumericPagerField />
                <asp:NextPreviousPagerField ShowLastPageButton="True" ShowPreviousPageButton="False" />
            </Fields>
        </asp:DataPager>
        </div>
        </td>
    </tr>

我被告知使用ItemDataBound,即使我不知道所选更改如何触发ItemDataBound。这是我试过的代码:

protected void lv_MessageSubject_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (((ListView)sender).SelectedIndex == e.Item.DisplayIndex)
        {
            var ctrl = (HtmlContainerControl)e.Item.FindControl("area");
            ctrl.Attributes["style"] = "background-color:Navy; color:White; padding:0px;";
        }
    }

当我点击“查看”按钮时,结果是ItemDataBound没有触发。

1 个答案:

答案 0 :(得分:0)

ItemDataBound听起来确实不是一个正确的想法。由于您的按钮设置为触发命令,因此ItemCommand看起来更合适:

<asp:ListView ... OnItemCommand="lv_MessageSubject_ItemCommand"

...

<asp:Button ID="cmd_View" runat="server" CommandName="Highlight"

...

protected void lv_MessageSubject_ItemCommand(Object sender, ListViewCommandEventArgs e)
{
    if (e.CommandName == "Highlight")
    {
        var ctrl = (HtmlContainerControl)e.Item.FindControl("area");
        ctrl.Attributes["style"] = "background-color:Navy; color:White; padding:0px;";
    }
}

请注意,事件具有不同的名称 - “选择”是special,因为它调用了一些内置功能,您可能不应该干扰它。