c# - 使用数据库值从代码后面更改 css

标签 c# css asp.net

我已经被这个问题困扰了几天了。已经到了无法理性思考的地步。非常感谢你们任何人愿意传授的任何指导和智慧。 我的问题是我创建了一个 gridview,它从数据库中为每一行加载一个字体名称(由字体设计师指定)和另一个名为 FontFamily 的列(我需要它用于 css)。我可以很容易地将数据库中的值加载到 gridview 中,但是我无法将“FontFamily”值用作我的 fontfamily css 值。我已经在互联网上搜索过,但到目前为止还没有找到任何有用的东西。 我尝试通过隐藏代码更改字体,但无法将数据绑定(bind)值用作 css 属性值。 该页面的总体目标是向用户显示哪些自定义字体可用。 lblFontExample 的字体系列应该反射(reflect)该行中列出的字体。如果这听起来很愚蠢,我们深表歉意。几天来我一直在为此烦恼,逻辑似乎在迅速下降,所以我认为是时候寻求帮助了,所以提前感谢任何提供帮助的人。

我的代码背后:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.IO;
using System.Data;

public partial class Admin_addFont : System.Web.UI.Page
{
private string fontUploadDirectory;
private string cssUploadDirectory;
private string connectionString =
  WebConfigurationManager.ConnectionStrings["bncConn"].ConnectionString;

protected void Page_Load(object sender, EventArgs e)
{
    // ensure files are uploaded to the right folder
    fontUploadDirectory = Path.Combine(
        Request.PhysicalApplicationPath, "fonts");

    if (!this.IsPostBack)
    {
        BindGrid();

    }

}

protected void BindGrid()
{

    // define ado.net objects
    SqlConnection con = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand("ProductDetails.bnc_Fonts", con);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
    // define parameters
    cmd.Parameters.Add(new SqlParameter("@status", SqlDbType.VarChar, 50));
    cmd.Parameters["@status"].Value = "Display";

    // attempt to connect to db, read data, fill dataset and bind gridview. Catch exceptions and close the connection.
    try
    {
        con.Open();
        DataSet ds = new DataSet();
        adapter.Fill(ds, "Fonts");
        grdFonts.DataSource = ds;    
        grdFonts.DataBind();
    }
    catch (Exception err)
    {
        lblFontGrd.Text = err.Message;
    }
    finally
    {
        con.Close();
    }
}

protected void grdFonts_PageIndexChanging(object sender, GridViewPageEventArgs e)
{

    grdFonts.PageIndex = e.NewPageIndex;

    BindGrid();

}}

我的标记:

<%@ Page Title="" Language="C#" MasterPageFile="~/Admin/AdminMaster.master" AutoEventWireup="true" CodeFile="addFont.aspx.cs" Inherits="Admin_addFont" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="RightCol" runat="Server">
        <h1>Fonts</h1>
        <div>
        <h2>Currently available fonts</h2>
    <asp:Label ID="lblFontGrd" runat="server"></asp:Label>
    <asp:GridView ID="grdFonts" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False"
         OnPageIndexChanging="grdFonts_PageIndexChanging">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775"></AlternatingRowStyle>

        <Columns>
            <asp:TemplateField AccessibleHeaderText="ID" FooterText="ID" HeaderText="ID">
                <ItemTemplate>
                    <asp:Label ID="fontId" runat="server" Text='<%# Eval("FontId") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField AccessibleHeaderText="Font Name" FooterText="Font Name" HeaderText="Font Name">
                <ItemTemplate>
                    <asp:Label ID="lblfontName" runat="server" Text='<%# Eval("FontName") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:Label ID="lblfontNameEdit" runat="server" Text='<%# Eval("FontName") %>'></asp:Label>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField AccessibleHeaderText="Example" FooterText="Example" HeaderText="Example">
                <ItemTemplate>
                    <asp:Label id="lblfontExample" runat="server" Text="This is an example"></asp:Label>
                    <asp:HiddenField ID="txtFontEx" runat="server" Value='<%# Eval("FontFamily") %>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField AccessibleHeaderText="Discontinued?" HeaderText="Discontinued?" FooterText="Discontinued?">
                <ItemTemplate>
                    <asp:CheckBox ID="Discontinued" runat="server" Checked='<%# Eval("Discontinued") %>' Enabled="false" />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:CheckBox ID="Discontinued" runat="server" Checked='<%# Eval("Discontinued") %>' Enabled="true" />
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Edit">
                <ItemTemplate>
                    <span onclick="return confirm('Are you sure you want to delete?')">
                        <asp:LinkButton ID="btnDelete" Text="Delete" runat="server" CommandName="Delete" />
                    </span>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>

        <EditRowStyle BackColor="#999999"></EditRowStyle>

        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"></FooterStyle>

        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"></HeaderStyle>

        <PagerStyle HorizontalAlign="Center" BackColor="#284775" ForeColor="White"></PagerStyle>

        <RowStyle BackColor="#F7F6F3" ForeColor="#333333"></RowStyle>

        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333"></SelectedRowStyle>

        <SortedAscendingCellStyle BackColor="#E9E7E2"></SortedAscendingCellStyle>

        <SortedAscendingHeaderStyle BackColor="#506C8C"></SortedAscendingHeaderStyle>

        <SortedDescendingCellStyle BackColor="#FFFDF8"></SortedDescendingCellStyle>

        <SortedDescendingHeaderStyle BackColor="#6F8DAE"></SortedDescendingHeaderStyle>
    </asp:GridView>
</div>
<div>
    <asp:FileUpload ID="flupCss" runat="server" />&nbsp;
    <asp:Label ID="lblCss" runat="server" AssociatedControlID="flupCss" Text="Upload file with file ending: .css"></asp:Label>
    <br />
    <asp:FileUpload ID="flupEot" runat="server" />&nbsp;
    <asp:Label ID="lblEot" runat="server" AssociatedControlID="flupEot" Text="Upload file with file ending: .eot"></asp:Label>
    <br />
    <asp:FileUpload ID="flupTtf" runat="server" />&nbsp;
    <asp:Label ID="lblTtf" runat="server" AssociatedControlID="flupTtf" Text="Upload file with file ending: .ttf"></asp:Label>
    <br />
    <asp:FileUpload ID="flupSvg" runat="server" />&nbsp;
    <asp:Label ID="lblSvg" runat="server" AssociatedControlID="flupEot" Text="Upload file with file ending: .eot"></asp:Label>
    <br />
    <asp:FileUpload ID="flupWoff" runat="server" />&nbsp;
    <asp:Label ID="lblWoff" runat="server" AssociatedControlID="flupWoff" Text="Upload file with file ending: .woff"></asp:Label>
    <br />
    <asp:FileUpload ID="flupWoff2" runat="server" />&nbsp;
    <asp:Label ID="lblWoff2" runat="server" AssociatedControlID="flupEot" Text="Upload file with file ending: .woff2"></asp:Label>
    <br />
    <asp:Button ID="btnUploadFont" runat="server" Text="Add Font" />
</div>

最后是我的存储过程:

CREATE PROCEDURE [ProductDetails].[bnc_Fonts] 
-- Add the parameters for the stored procedure here
@Status varchar(50) = '', 
@FontId tinyint = '',
@FontName varchar(50) = '',
@FontFamily varchar(50) = '',
@Discontinued bit = ''
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

if (@Status = 'Display')
begin
select FontId, FontName, FontFamily, Discontinued 
from ProductDetails.Fonts 
where Discontinued = 0
order by FontName asc   
end
if (@Status = 'FontFam')
begin
select FontFamily from ProductDetails.Fonts
where FontId = @FontId
end
if (@Status = 'Add')
begin
insert into ProductDetails.Fonts (FontName, FontFamily, Discontinued)
values (@FontName, @FontFamily, @Discontinued)
end
if (@Status = 'Delete')
begin
UPDATE ProductDetails.Fonts
SET Discontinued = @Discontinued
where FontId = @FontId
end
END
GO

最佳答案

<asp:Label id="lblfontExample" Font-Names='<%# BuildFont(Eval("FontFamily").ToString()) %>' runat="server" Text='<%# Eval("FontName") %>'></asp:Label>

在后面的代码中:

public static string[] BuildFont(string font)
{
    string[] array = new string[1];
    array[0] = font;
    return array;
}

字体名称示例:Verdana、Times New Roman..

关于c# - 使用数据库值从代码后面更改 css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28656322/

相关文章:

html - 将导航栏移动到 div 的右侧

javaScript text.replace 仅替换我想要替换的单词之一

c# - Asp.net Identity 2.0 临时密码

c# - Asp.net 在 GridView.AllowPaging ="true"时生成错误的 SQL 请求

c# - 无法通过单击按钮发送电子邮件。 ASP.NET 和 C#

c# - 回复时如何维护/保留自定义电子邮件 header (X-header)

c# - ASP.NET 回发 x jQuery : What are the cons and pros?

c# - 通过替换它们的构造函数将工厂模式强制执行到一组特定的类型

javascript - 如何从 Razor 部分引用 javascript 变量?

asp.net - 获取 session cookie名称