I am writing some controls for an asp.net website and i want make this controls contain .ascx file and .dll file.
I dont want upload site for every control.
so is there a way that make this control add to site via code like adding module in DotNetNuke.
You may need to consider using WebParts:
http://dotnetslackers.com/articles/aspnet/UsingWebPartsInASPNet20.aspx
Or this link:
http://quickstarts.asp.net/QuickStartv20/aspnet/doc/webparts/intro.aspx
You need to create custom controls. You can do that by deriving your control class from "System.WebUI.Webcontols.Control". You need to override CreateChildControls method for the UI.
Related
I've got a aspx and aspx.cs file with some components. Now I want to reuse parts of that page in another page. My approach would be to pull out the duplicate part into a WebServerControl.
So before I waste more time yahoogling, is that even the right idea and if so, is there a way to use parts of the aspx file rather than doing it tediously in RenderContents with the HtmlTextWriter, WriteBeginTag, WriteAttribute and so on. That looks like a mess for complicated layout and sizeable amounts of controls.
What's the standard?
Depends.
The main driving factor is that if you need to reuse your control in multiple web applications, you should go with a Custom Control (.cs in C#).
Else, if you only intend to reuse your control in one web application, choose a User Control (.ascx).
This MSDN article is a good starting point.
UPDATE (since OP asked further details):
To embed JavaScript for a custom control, a common approach is
var initializeScript = string.Format("MyNamespace.initialize('{0}', {1});", ClientID, myScriptString);
Attributes.Add("onmouseover", initializeScript);
Suggest to write JavaScript code in a js file and not in .cs since the latter is a nightmare to maintain and debug. Hope this helps.
It sounds like what you want to do is bundle the items into a User Control. This will allow you to design the control by using existing .NET controls rather than rendering everything out from scratch.
All you need is to create an ASP.NET Web User Control
Taken from MSDN:
An ASP.NET Web user control is similar to a complete ASP.NET Web page
(.aspx file), with both a user interface page and code. You create the
user control in much the same way you create an ASP.NET page and then
add the markup and child controls that you need. A user control can
include code to manipulate its contents like a page can, including
performing tasks such as data binding.
I've an asp.net web application accessed by different users with different roles.
Now I've to enable/disable and show/hide controls (label, textbox, buttons in grid, third party controls) based on a logic involves context variables and users role.
I want to avoid to write IF statements in onLoad method.
I tried to create an xml file like this:
<root>
<page name="page1" mode="insert">
<control id="txtName" property="Visible" value="True" />
then in a basePage class I tried to cycle all Page.Controls to set property with propertyInfo.
Problems starts when I have ascx inside ascx or gridview with command buttons to disable.
It is possible to configure those control's behaviors in an external configuration file?
Is there a framework allowing this?
The .NET framework controls do not do this inherently. Though, the best way to do this is by creating a control library, override the controls you typically use, and then have each of those controls do a lookup to the XML file, and import the settings.
It's MUCH cheaper for the control to go to the repository, rather than the page to iterate through the controls and then do the work.
HTH.
Pop quiz hot shots...
I have a Visual Studio 2010 .NET 4 solution with 2 projects, The first project is a c# class library that contains a httphandler and a .aspx page. The .aspx page's build action has been set to "Embedded Resource".
The second project is an asp.net web application which references the first. The httphandler is wired up in the web.config.
I want the httphandler to serve the embedded .aspx page. How do I do this?
Thanks,
James
maybe this is relevant: http://www.west-wind.com/weblog/posts/2007/Jul/23/Loading-an-ASPNET-Page-Class-dynamically-in-an-HttpHandler
.aspx is just a specialized kind of HttpHandler in .NET. Don't forget that.
Thus, .aspx files (ASP.NET Web Pages) actually have implemented IHttpHandler and they have ProccessRequest method. There are two ways to do this:
Based on dynamic compilation nature of Web Forms and markup vs. code-behind, if you want the markup of the page to be compiled dynamically and be executed, you have to extract the page (through code) and save it on the disk. This extraction process can take place on Applciation_Start event.
If you don't like the extraction method, don't forget that you can remove markup entirely and do everything in code-behind (just like PHP or old ASP or ASP.NET MVC). Also remember that your page is actually a class from the point of OOP. Thus simply instantiate it in your HttpHandler and call it's ProcessRequest method, passing the current HttpContext into it.
The way I would do it is through a VirtualPathProvider, not a handler. You can set up and register a virtual path provider to serve pages from an embedded resource (or database, web service, or anything else you can think of).
http://support.microsoft.com/kb/910441
Is there a c# command to include another web page - the equivelant of the php require?
I know how to do server side includes but was looking for something code based.
Thanks
Thanks for the answers all. I think I might need to explain further. I have several sub-pages that I will be loading during the use of the site using an xmlhttp request. Initially however, I need to load the starting sub-page before the user has interacted with the site. I could do this with js, but that would require more overhead in server calls from the client for the initial load. I already use master pages, but this is a little different. Since this is done serverside initally but must remain able to be refreshed clientside, I don't think I can make these pages into controls can I? I am pretty new to .Net so I may be making my life harder than I need to.
I think what you may be looking for are MasterPages and UserControls. A MasterPage allows you to define a basic template that is "filled in" by the implementing pages by having the implementing page add it's own content to the ContentPlaceHolders defined on the MasterPage. A UserControl is a re-usable piece of markup and associated code that you can reference from your mark up or add dynamically to the page being rendered in codebehind.
The way ASP.NET is structured, you shouldn't really need to do this. Code is compiled, so all of your classes and functions should be accessible simply by referencing the relevant assembly or namespace, without having to include individual code files.
You might be looking for user controls, which allow you to create fragments of markup with their corresponding code behind, and then reference these in your page.
With ASP.NET MVC it looks like this:
<% Html.RenderPartial("LogOnUserControl"); %>
This way you can put another UserControl on your page.
you can use include in asp.net like php include from below mentioned code
<!--#include file="include/leftmenuscript.inc"-->
You can also use a master page, as someone stated below, which flushes out your basic layout and lets you define content place holders, which other pages can implement and fill in the content. Master pages are a popular approach for defining page elements that are consistent across all pages, like your nav there (also things like headers, footers, common scripts, CSS, etc.).
I have created a webpart in c# for sharepoint.
its basically a form with text boxes, literals, validators and buttons.
im not sure how to render this form to make it look pretty.
The layout etc is being done entirely within this c# class.
At the moment to get started im just overrinding CreateChildControls() method
and adding each form control using something like: this.Controls.Add(submitButton);
any ideas on how best to layout this form?
Thanks.
When creating custom webparts I also prefer to implement them by overriding the CreateChildControls() and Render() methods. In the Render() method I have full control of the html output and I can render my inner controls by calling this.someInnerControl.RenderControl(writer).
Having full control of the html output also makes it easy to style the html using CSS. As other people suggests, use an external CSS file and apply the styes to the class attribute on html elements or CssClass property on ASP.NET web control.
When I implement webparts, that does not require special branding, I prefer to reuse the CSS classes defined by SharePoint. This will ensure that my webpart is visually similar to the webpart provided by SharePoint and that I keep a consistent look and feel.
When using the SharePoint defined CSS styles, you should be aware of your html output. Some of the CSS classes requires a specific html structure to properly render. You can always use the browsers "View Source" to check the html of the SharePoint element you are trying to imitate.
I would recommend grabbing the source from an existing sharepoint page and using the styles defined by sharepoint. This link to the styles in 2003 is old, but still a good guide to get started. Most of the CSS class names haven't changed.
In my web parts I include css files in the solution and inject them in the page using something like:
this.Page.Header.RegisterCss("/_layouts/path/to/css/file.css", false);
You can override the RenderContents(...) method to manually render the HTML in anyway you want to. This includes adding any css includes, scripting includes, etc. that you want/use.
You can render your child controls to strings and then output them as well, but you probably should NOT call the base.RenderContents(...) method.
Just make sure you don't forget to render your child controls.
If it's important for you to see as you design, use the SmartPart which embeds a user control in a web part. (In case you didn't know, user controls can be designed using the tools within Visual Studio.)
If you prefer to hand-code, then you're on the right track. Simply create and set initial properties for your controls within the CreateChildControls() method and use this.Controls.Add() as you have been.
In both cases, where possible use the CssClass property so you can tinker with the look and feel in a CSS file without having to recompile. You could hard-code the CSS class names but it would be better to use the web part properties or an external config source to store these. Have a reference to the CSS file in your master page or inject it into the page using the other techniques mentioned in this answer.
The MSDN articles Web Parts in Windows SharePoint Services or Creating a Basic Web Part might also help.