0

我已经成功创建了一个带有嵌入式 css 和嵌入式 js 文件的服务器控件。

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FCC.Web.UI.CustomControl;
using System.Web.UI.HtmlControls;

[assembly: TagPrefix("CustomControls", "asp")]
namespace FCC.Web.UI.CustomControl
{
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    [DefaultProperty("Value")]
    [ValidationProperty("Value")]
    [ToolboxData("<{0}:DatePicker runat=server></{0}:DatePicker>")]
    public class DatePicker : CompositeControl, iDynamicControl
    {
    #region-- privates --
    private TextBox textbox1;
    private RequiredFieldValidator requiredFieldValidator1;
    private RegularExpressionValidator regularExpressionValidator1;
    private HtmlInputHidden hiddenField1;
    #endregion

    #region -- properties--

    [Bindable(true)]
    [Category("Styles")]
    [Themeable(true)]
    [DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Content)]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    [Localizable(true)]
    public Style TextBoxStyle
    {
        get
        {
            EnsureChildControls();
            return textbox1.ControlStyle;
        }
        set
        {
            EnsureChildControls();
            textbox1.ControlStyle.MergeWith(value);
        }
    }

    [Bindable(true)]
    [Category("Styles")]
    [Localizable(true)]
    public string TextBoxCssStyle
    {
        get
        {
            EnsureChildControls();
            return textbox1.CssClass;
        }
        set
        {
            EnsureChildControls();
            textbox1.CssClass = value + " calender";
        }
    }

    [Bindable(true)]
    [Category("Appearance")]
    [DefaultValue("")]
    [Localizable(true)]
    [Description("Text displayed on the textbox")]
    public String Value
    {
        get
        {
            EnsureChildControls();
            if (textbox1.Text.IsNullOrEmpty()) { return string.Empty; }
            else
            {
                DateTime date1 = DateTime.Parse(textbox1.Text);
                string datestring = string.Format("<day>{0}</day><month>{1}</month><year>{2}</year>", date1.Day.ToString(), date1.Month.ToString(), date1.Year.ToString());
                return datestring;
            }
        }
        set
        {

        }
    }

    [Bindable(true)]
    [Category("Appearance")]
    [DefaultValue("")]
    [Localizable(true)]
    public string InvalidDateErrorMessage
    {
        get
        {
            EnsureChildControls();
            return regularExpressionValidator1.ErrorMessage.IsNullOrEmpty() ? "Invalid date" : regularExpressionValidator1.ErrorMessage;
        }
        set
        {
            EnsureChildControls();
            regularExpressionValidator1.ErrorMessage = value;
        }
    }


    [Bindable(true)]
    [Category("Default")]
    [Localizable(true)]
    public bool IsRequired
    {
        get
        {
            EnsureChildControls();
            return requiredFieldValidator1.Enabled;
        }
        set
        {
            EnsureChildControls();
            requiredFieldValidator1.Enabled = value;
        }
    }

    [Bindable(true)]
    [Category("Default")]
    [DefaultValue("")]
    [Localizable(true)]
    [Description("Validation group for the textbox and inbuilt validations")]
    public string ValidationGroup
    {
        get
        {
            EnsureChildControls();
            return textbox1.ValidationGroup;
        }
        set
        {
            EnsureChildControls();
            regularExpressionValidator1.ValidationGroup = value;
            requiredFieldValidator1.ValidationGroup = value;
            textbox1.ValidationGroup = value;
        }
    }

    [Bindable(true)]
    [Category("Default")]
    [DefaultValue("")]
    [Localizable(true)]
    [Description("cssClasses that apply to all validators")]
    public string ValidationErrorCssClass
    {
        get
        {
            EnsureChildControls();
            return requiredFieldValidator1.CssClass;
        }
        set
        {
            EnsureChildControls();
            requiredFieldValidator1.CssClass = regularExpressionValidator1.CssClass = value;
        }
    }

    [Bindable(true)]
    [Category("Default")]
    [DefaultValue("")]
    public DateTime? Date
    {
        get
        {
            EnsureChildControls();
            if (textbox1.Text.IsNullOrEmpty()) { return null; }
            else
            {
                return DateTime.Parse(textbox1.Text);
            }
        }
        set
        {
            EnsureChildControls();
            textbox1.Text = !value.HasValue ? string.Empty : value.Value.ToString("dd-MMM-yyyy");
        }
    }

    [Bindable(true)]
    [Category("Appearance")]
    [DefaultValue("")]
    [Localizable(true)]
    [Description("Error message that appears when text box is empty")]
    public string RequiredErrorMessage
    {
        get
        {
            EnsureChildControls();
            return requiredFieldValidator1.ErrorMessage.IsNullOrEmpty() ? "Required Field" : requiredFieldValidator1.ErrorMessage;
        }
        set
        {
            EnsureChildControls();
            requiredFieldValidator1.ErrorMessage = value;
        }
    }

    #endregion

    protected override void OnLoad(EventArgs e)
    {
        EnsureChildControls();
        base.OnLoad(e);
    }

    protected override void CreateChildControls()
    {
        Controls.Clear();
        #region--hiddenField--
        hiddenField1 = new HtmlInputHidden();
        hiddenField1.ID = "hiddenVal1";
        hiddenField1.Attributes["class"]="hiddenVal";
        #endregion


        #region-- textbox--
        textbox1 = new TextBox();
        textbox1.ID = "textbox1";
        textbox1.Style[HtmlTextWriterStyle.VerticalAlign] = "middle";
        #endregion
        #region--required validation--
        requiredFieldValidator1 = new RequiredFieldValidator();
        requiredFieldValidator1.ID = "validator1";
        requiredFieldValidator1.ErrorMessage = RequiredErrorMessage;
        requiredFieldValidator1.Display = ValidatorDisplay.Dynamic;
        requiredFieldValidator1.ControlToValidate = textbox1.ID;
        requiredFieldValidator1.Enabled = false;
        requiredFieldValidator1.SetFocusOnError = true;

        #endregion
        #region--regular expression validator--
        regularExpressionValidator1 = new RegularExpressionValidator();
        regularExpressionValidator1.ID = "validator2";
        regularExpressionValidator1.Enabled = true;
        regularExpressionValidator1.ErrorMessage = InvalidDateErrorMessage;
        regularExpressionValidator1.Display = ValidatorDisplay.Dynamic;
        regularExpressionValidator1.ControlToValidate = textbox1.ID;
        regularExpressionValidator1.SetFocusOnError = true;
        regularExpressionValidator1.ValidationExpression = "^((((((0?[1-9])|([1-2][0-9])|(3[0-1]))-(([jJ][aA][nN])|([mM][aA][rR])|([mM][aA][yY])|([jJ][uU][lL])|([aA][uU][gG])|([oO][cC][tT])|([dD][eE][cC])))|(((0?[1-9])|([1-2][0-9])|(30))-(([aA][pP][rR])|([jJ][uU][nN])|([sS][eE][pP])|([nN][oO][vV])))|(((0?[1-9])|(1[0-9])|(2[0-8]))-([fF][eE][bB])))-(20(([13579][01345789])|([2468][1235679]))))|(((((0?[1-9])|([1-2][0-9])|(3[0-1]))-(([jJ][aA][nN])|([mM][aA][rR])|([mM][aA][yY])|([jJ][uU][lL])|([aA][uU][gG])|([oO][cC][tT])|([dD][eE][cC])))|(((0?[1-9])|([1-2][0-9])|(30))-(([aA][pP][rR])|([jJ][uU][nN])|([sS][eE][pP])|([nN][oO][vV])))|(((0?[1-9])|(1[0-9])|(2[0-9]))-([fF][eE][bB])))-(20(([13579][26])|([2468][048])))))$";


        #endregion

        Controls.Add(textbox1);
        Controls.Add(hiddenField1);
        Controls.Add(requiredFieldValidator1);
        Controls.Add(regularExpressionValidator1);
    }

    protected override void RecreateChildControls()
    {
        EnsureChildControls();
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        ClientScriptManager cs = this.Page.ClientScript;

        string jqueryResource = "FCC.Web.UI.CustomControl.jQuery1_8_3.js";
        cs.RegisterClientScriptResource(typeof(DatePicker), jqueryResource);

        string jqueryUIresource = "FCC.Web.UI.CustomControl.jquery-ui-1.9.2.smoothness_all.min.js";
        cs.RegisterClientScriptResource(typeof(DatePicker), jqueryUIresource);

        string calenderScriptResource = "FCC.Web.UI.CustomControl.calender.js";
        cs.RegisterClientScriptResource(typeof(DatePicker), calenderScriptResource);

        string templink = "<link rel='stylesheet' text='text/css' href='{0}' />";
        string csslocation = Page.ClientScript.GetWebResourceUrl(this.GetType(), "FCC.Web.UI.CustomControl.jquery-ui-1.9.2.smoothness_all.min.css");
        LiteralControl lc = new LiteralControl(string.Format(templink, csslocation));
        Page.Header.Controls.Add(lc);


    }

    protected override void Render(HtmlTextWriter writer)
    {
        AddAttributesToRender(writer);

        textbox1.RenderControl(writer);

        hiddenField1.RenderControl(writer);
        requiredFieldValidator1.RenderControl(writer);
        regularExpressionValidator1.RenderControl(writer);
        //writer.Write("DateControl");
    }
}
}

我的 CSS 具有指向服务器上存在的图像(背景图像)的链接。我想通过将这些图像嵌入到控件中来使其成为一个完整的控件。问题是我将如何引用 css 文件中嵌入的图像资源?

4

1 回答 1

0

这是在这里回答:https ://stackoverflow.com/a/1196850/1402923

在此引用:

<% = WebResource("image1.jpg") %>

您可以在 CSS 文件中使用上述语句,并且在使用 WebResourceAttribute 注册 CSS 时,可以将“PerformSubstitution”设置为 true

Default.css
body{
    background: <%=WebResource("xyz.jpg")%>
}



[assembly, WebResource("Default.css","text/css", PerformSubstitution=true)]
[assembly, WebResource("xyz.jpg","image/jpg")]
于 2013-07-19T08:46:52.083 回答