Click here to Skip to main content
15,886,019 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear All,

I want to make Web server custom control with Css and some images using C# and .net 4.5

[DefaultProperty("Text")]
[ToolboxData("<{0}:ServerControl1 runat="server">")]
public class ServerControl1 : WebControl
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null)? "[" + this.ID + "]" : s);
}

set
{
ViewState["Text"] = value;
}
}

protected override void RenderContents(HtmlTextWriter output)
{

Button mybutton = new Button();
TextBox _mytextBox = new TextBox();
Label mylabel = new Label();

Panel _mypanel = new Panel();

_mypanel.Controls.Add(mybutton);
_mypanel.Controls.Add(_mytextBox);
_mypanel.Controls.Add(mylabel);

Controls.Add(_mypanel);

//output.Write(Text);
}
}

and i want show the test.jpg file as background in this assembly and using test.css in this assembly.

Please anybody help

thanks
vilas
Posted
Updated 11-Aug-14 3:40am
v2

Found something that may help you here
and here

Brs,
--
 
Share this answer
 
add in assembly of Custom Web server project

//[assembly: System.Web.UI.WebResource("", "text/javascript")]
[assembly: System.Web.UI.WebResource("TestServerControl.Img.avm.jpg", "image/jpg")]
[assembly: System.Web.UI.WebResource("TestServerControl.JS.test.js", "text/javascript", PerformSubstitution = true)]
[assembly: System.Web.UI.WebResource("TestServerControl.CSS.test.css", "text/css")]


in Custom Web server control file add

protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
ClientScriptManager cs = this.Page.ClientScript;
// Page.ClientScript.RegisterStartupScript(typeof(TestServerControl.ServerControl1), "aaa", "TestServerControl.JS.test.js");
cs.RegisterClientScriptResource(typeof(TestServerControl.ServerControl1), "TestServerControl.JS.test.js");
string cssResource = "TestServerControl.CSS.test.css";
string cssResourceURL = Page.ClientScript.GetWebResourceUrl(typeof(TestServerControl.ServerControl1), cssResource);
HtmlLink cssLink = new HtmlLink();
cssLink.Href = cssResourceURL;
cssLink.Attributes.Add("rel", "stylesheet");
this.Page.Header.Controls.Add(cssLink);


}

also create

protected override void CreateChildControls()
{
base.CreateChildControls();

// Page.ClientScript.RegisterClientScriptResource(typeof(TestServerControl.ServerControl1), "TestServerControl.CSS.test.css");
// cs.RegisterClientScriptResource(typeof(TestServerControl.ServerControl1), "TestServerControl.CSS.test.css");
Button _button = new Button();
Button _button1 = new Button();
Image _img = new Image();
// _img.ImageUrl = this.Page.ClientScript.GetWebResourceUrl(typeof(TestServerControl.ServerControl1), "TestServerControl.Img.avm.jpg");

_img.ImageUrl = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "TestServerControl.Img.avm.jpg");
_button.Text = "Button1";
_button.ID = "Button1";
_button1.ID = "Button2";
// _button.Attributes.Add("Onclick", "javascript:alert('Button help ');");
_button.Attributes.Add("Onclick", "myfunction();");
// _button.Controls.Add(_img);
Label _mylabel = new Label();
_mylabel.Text = "Test Label ";
_mylabel.Attributes.Add("OnClick", "myfunction();");
_mylabel.Attributes.Add("CssClass", "mytext1");
_mylabel.Attributes.Add("CssClass", "mytext1");
TextBox _mytextbox = new TextBox();
_button1.Attributes.Add("CssClass", "mytext1");
this.Controls.Add(_button);
this.Controls.Add(_button1);
this.Controls.Add(_img);
this.Controls.Add(_mylabel);
this.Controls.Add(_mytextbox);
}


also Create Embeded Resource of your images , CSS and javascripts.

run application and enjoy
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900