基于绑定数据的GridView样式

发布于 2024-08-30 12:39:53 字数 230 浏览 11 评论 0原文

我希望 GridView 的行基于名为 IsObsolete 的绑定数据值添加删除线。我尝试这样做:

<RowStyle BackColor="#EFF3FB" Font-Strikeout='<%# Bind('IsObsolete') %>' />

但显然这无法解析。我不想在 GridView.DataBound() 中执行此操作。还有其他想法吗?

I would like the rows of my GridView to have strikethrough based on a bound data value called IsObsolete. I tried to do this:

<RowStyle BackColor="#EFF3FB" Font-Strikeout='<%# Bind('IsObsolete') %>' />

But obviously this doesn't parse. I'd rather not do this in GridView.DataBound(). Any other ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

审判长 2024-09-06 12:39:53

我通过在模板中一个控件的 DataBinding 事件上应用一种样式来实现此目的。示例:

<asp:GridView ID="grdYourGrid" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:TemplateField HeaderText="SomeTitle">
            <ItemTemplate>
                <asp:HyperLink ID="hrefYourLink" runat="server"
                    NavigateUrl="Somepage.aspx?id={0}" 
                    OnDataBinding="hrefYourLink_DataBinding"></asp:HyperLink>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

然后实现 OnDataBinding 事件:

protected void hrefYourLink_DataBinding(object sender, System.EventArgs e)
{            
    HyperLink link = (HyperLink)(sender);
    GridViewRow row = (GridViewRow)(link.Parent.Parent);
    if ((bool)(Eval("IsObsolete"))
    {
        row.CssClass = "StrikeThroughStyle";
    }
    link.Text = HttpUtility.HtmlEncode(((int)(Eval("ID"))).ToString());
    link.NavigateUrl = string.Format(link.NavigateUrl, Eval("ID").ToString());
}

这只是一个快速示例,其中包含一个带有链接的列,该链接也会根据数据绑定进行修改,但如果对其进行调整,您应该能够了解要点以满足您的需求。我喜欢在数据绑定上执行此操作,因为我在 aspx 代码中不进行内联绑定。

I do this by applying a style on the DataBinding event of one of my controls in a template. Example:

<asp:GridView ID="grdYourGrid" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:TemplateField HeaderText="SomeTitle">
            <ItemTemplate>
                <asp:HyperLink ID="hrefYourLink" runat="server"
                    NavigateUrl="Somepage.aspx?id={0}" 
                    OnDataBinding="hrefYourLink_DataBinding"></asp:HyperLink>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Then implement the OnDataBinding event:

protected void hrefYourLink_DataBinding(object sender, System.EventArgs e)
{            
    HyperLink link = (HyperLink)(sender);
    GridViewRow row = (GridViewRow)(link.Parent.Parent);
    if ((bool)(Eval("IsObsolete"))
    {
        row.CssClass = "StrikeThroughStyle";
    }
    link.Text = HttpUtility.HtmlEncode(((int)(Eval("ID"))).ToString());
    link.NavigateUrl = string.Format(link.NavigateUrl, Eval("ID").ToString());
}

This is just an quick example with a column with a link that gets modified based on the databinding as well but you should be able to get the gist of if an tweak it to suit your needs. I like doing it on the databinding because I do no binding inline in my aspx code.

貪欢 2024-09-06 12:39:53

由于 RowStyle 元素适用于整个网格,因此完成您想要的操作的唯一方法是为所有列设置 TemplateItems,并根据相同的数据值将 CssClass 应用到每列。

我不确定您避免执行此操作的 DataBound 事件的原因,因为这将是完成它的最简单方法。

您也可以尝试使用格式化函数和项目样式。从上面窃取一些代码并更改它:

<%

public string GetObsoleteClass(string obsolete)
{
    bool obs = Convert.ToBoolean(obsolete);
    obs ? return "myObsoleteClass" : return "myNotObsoleteClass";
}

%>
<asp:GridView ID="grdYourGrid" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:TemplateField HeaderText="SomeTitle">
            <ItemTemplate>
                <asp:HyperLink ID="hrefYourLink" runat="server"
                    NavigateUrl="Somepage.aspx?id={0}" 
                    OnDataBinding="hrefYourLink_DataBinding"></asp:HyperLink>
            </ItemTemplate>
              <itemstyle CssClass='<%# Eval("isObsolete") %>'>
              </itemstyle>
        </asp:TemplateField>
        <asp:boundfield
            sortexpression="LastName"
            datafield="LastName"
            headertext="LastName">
              <itemstyle CssClass='<%# Eval("isObsolete") %>'>
              </itemstyle>
        </asp:boundfield>
    </Columns>
</asp:GridView>

Since the RowStyle element is applicable to the entire grid, the only way to accomplish what you want would be to have TemplateItems set for all columns and apply a CssClass to each column based on that same data value.

I'm not sure for your reasoning for avoiding the DataBound event for doing this as that would be the simplest way to accomplish it.

You might also try using a formatting function and itemstyles. Stealing a tidbit of code from above and changing it:

<%

public string GetObsoleteClass(string obsolete)
{
    bool obs = Convert.ToBoolean(obsolete);
    obs ? return "myObsoleteClass" : return "myNotObsoleteClass";
}

%>
<asp:GridView ID="grdYourGrid" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:TemplateField HeaderText="SomeTitle">
            <ItemTemplate>
                <asp:HyperLink ID="hrefYourLink" runat="server"
                    NavigateUrl="Somepage.aspx?id={0}" 
                    OnDataBinding="hrefYourLink_DataBinding"></asp:HyperLink>
            </ItemTemplate>
              <itemstyle CssClass='<%# Eval("isObsolete") %>'>
              </itemstyle>
        </asp:TemplateField>
        <asp:boundfield
            sortexpression="LastName"
            datafield="LastName"
            headertext="LastName">
              <itemstyle CssClass='<%# Eval("isObsolete") %>'>
              </itemstyle>
        </asp:boundfield>
    </Columns>
</asp:GridView>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文