简体   繁体   English

使用数据库值从后面的代码更改css

[英]using a database value to change css from code behind

I have been absolutely stumped on this for a couple of days now. 我已经对这件事感到非常难过了几天。 Its getting to the point where I am unable to think about it rationally any more. 它已经到了我无法理性思考它的地步。 Any guidance and wisdom any of you are willing to impart will be very much appreciated. 任何你愿意传授的任何指导和智慧都将非常感激。 My problem is that I have created a gridview, which loads from the database for each row a font name (its designation given by the font designers) and another column called FontFamily (which I need for the css). 我的问题是我创建了一个gridview,它从数据库中为每一行加载一个字体名称(由字体设计者给出的名称)和另一个名为FontFamily的列(我需要用于css)。 I can load the values from the db into the gridview easily enough but I am un able to use the 'FontFamily' value as my fontfamily css value. 我可以很容易地将db中的值加载到gridview中,但是我无法使用'FontFamily'值作为我的fontfamily css值。 I have searched the internet and have not been able to come up with anything useful so far. 我已经搜索过互联网,到目前为止还没有找到任何有用的东西。 I have tried changing the font through code behind but am unable to use databound value as css property value. 我试过通过后面的代码更改字体但是无法使用数据绑定值作为css属性值。 The over all goal of the page is to show user which custom fonts are available. 该页面的所有目标是向用户显示哪些自定义字体可用。 The font-family of lblFontExample should reflect the font listed in that row. lblFontExample的font-family应该反映该行中列出的字体。 Apologies if any of this sounds silly at all. 如果这些听起来很愚蠢,那就道歉了。 I have been pulling my hair out over this for a few days now over this and logic seems to be in rapid decline so I thought it would be about time to ask for some help so thankyou in advanced to anyone who does help. 我已经把头发拉了过来几天,而且逻辑似乎正在快速下降所以我认为现在是时候要求一些帮助了,所以要比任何有帮助的人先进。

My code behind: 我的代码背后:

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();

}}

My markup: 我的加价:

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

And finally my stored procedure: 最后我的存储过程:

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>

In code behind : 代码背后:

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

Font-Names example: Verdana, Times New Roman.. 字体名称示例:Verdana,Times New Roman ..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM