我已经对这件事感到非常难过了好几天了。它已经到了我无法理性思考它的地步。任何你愿意传授的任何指导和智慧都将非常感激。 我的问题是我创建了一个gridview,它从数据库中为每一行加载一个字体名称(由字体设计者给出的名称)和另一个名为FontFamily的列(我需要用于css)。我可以很容易地将db中的值加载到gridview中,但我无法使用' FontFamily' value作为我的fontfamily css值。我已经搜索过互联网,到目前为止还没有找到任何有用的东西。 我试过通过后面的代码更改字体但是无法使用数据绑定值作为css属性值。 该页面的所有目标是向用户显示哪些自定义字体可用。 lblFontExample的font-family应该反映该行中列出的字体。如果这些听起来很愚蠢,那就道歉了。我已经把头发拉了过来几天,而且逻辑似乎正在快速下降所以我认为现在是时候要求一些帮助了,所以要比任何有帮助的人先进。
我的代码背后:
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" />
<asp:Label ID="lblCss" runat="server" AssociatedControlID="flupCss" Text="Upload file with file ending: .css"></asp:Label>
<br />
<asp:FileUpload ID="flupEot" runat="server" />
<asp:Label ID="lblEot" runat="server" AssociatedControlID="flupEot" Text="Upload file with file ending: .eot"></asp:Label>
<br />
<asp:FileUpload ID="flupTtf" runat="server" />
<asp:Label ID="lblTtf" runat="server" AssociatedControlID="flupTtf" Text="Upload file with file ending: .ttf"></asp:Label>
<br />
<asp:FileUpload ID="flupSvg" runat="server" />
<asp:Label ID="lblSvg" runat="server" AssociatedControlID="flupEot" Text="Upload file with file ending: .eot"></asp:Label>
<br />
<asp:FileUpload ID="flupWoff" runat="server" />
<asp:Label ID="lblWoff" runat="server" AssociatedControlID="flupWoff" Text="Upload file with file ending: .woff"></asp:Label>
<br />
<asp:FileUpload ID="flupWoff2" runat="server" />
<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
答案 0 :(得分:0)
<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 ..