服务器控件 - 嵌入图像 - 在css中引用

时间:2012-12-18 11:40:28

标签: c# asp.net custom-controls

我已经成功创建了一个嵌入式css&amp ;;的服务器控件。嵌入式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文件中引用图像嵌入资源?

1 个答案:

答案 0 :(得分:0)

在此处回答:https://stackoverflow.com/a/1196850/1402923

引用此处:

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

你可以在你的CSS文件中使用上面的语句   用WebResourceAttribute注册你的CSS,你可以设置   “PerformSubstitution”为真正的

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



[assembly, WebResource("Default.css","text/css", PerformSubstitution=true)]
[assembly, WebResource("xyz.jpg","image/jpg")]