asp.net user control deployment - asp.net

I'm using VS 2010. Is there a way to "deploy" a user control and it's code behind in a sort of binary (.ocx like) way so that I can just reference it from other projects and the html/code behind can't be edited?

No, you either cut and paste or look into using custom Server Controls. The custom server controls are basically a way of building the same .ascx style controls but doing it all in code without a UI portion. If done correctly, you can still have a UI drag and drop interface for your controls.
http://msdn.microsoft.com/en-us/library/zt27tfhy(v=vs.100).aspx

As ps2goat said, what you want are custom server controls. That being said, there is a way to create them from standard ascx controls with only some extra effort.
http://msdn.microsoft.com/en-us/library/aa479318.aspx

It should be possible (though not recommended) if you place .NET code into ASCX markup itself inside of <script runat="server"></script> tags.

Related

ASP .NET server control vs. HTML control

At the moment, in my team, there is a conflict when deciding to use HTML control or ASP .NET server control.
For instance, in order to implement a grid view list that supports reordering, we have two options:
Use HTML controls (ul, ol) and jQuery sortable. By using this approach, we can use the markup and style after slicing PSD and implement it rightaaway. We can fetch data by using jQuery ajax.
Use ASP .NET control like Telerik Grid (http://demos.telerik.com/aspnet-ajax/grid/examples/overview/defaultcs.aspx). Using this option, we have to change style of server control to match the design.
Considering performance and secutiry, please help me figure out which option is better?
Thanks in advanced.
#batto
telerik grid will wonderful you only not think for only this time . May be in future you need many enchantments in your project at that time you really need to use that feature's . So from my view you use Telerik controls .
For only display purpose Jquery grid is better option, it will increase the performance.
HTML control is a compromise between traditional ASP.NET programming and web control and it has a limited object-oriented interface. We usually use HTML control to have a smooth HTML code that will be generated and sent to the browser
If you want a powerful control, choose web control over HTML control!
It hope it helps you.
I would go with a server control. I would probably just use a
You have the ability to do what ever you want with them. If you want to do sorting using jquery then it should not be too different than how you sort ul's. If needed I can provide a sample. You also have the power of the server. If you tie other events such as add or remove then you will want the server control. Its a bit easier to pass data back and forth.

Dynamically loading asp.net controls with javascript

I am developing a series of ASP.NET controls that allow for a dynamically created sites where the layout gets generated from a class.cs file, the controls then get Loaded into an Placeholder in an update panel.
what I am hoping to achieve is using stuff like ajax to build the screen with javascript.
can you guys maybe point me in the right direction,
my google is not helping today.
I want to use stuff like ajax, json
thanks in advance
Javascript is a client-side scripting language and cannot be used to trigger loading of server-side controls.
You can use ASP.NET elements like etc., which have runat=server to trigger server side code, though.
Through AJAX? Not possible without page reload.

How to make a custom version of AjaxControlToolkit.TabContainer

I guess this is a lot of questions bundled into one post.
I want to build a wizard-like control which looks similar to the TabContainer
But I need certain customizations. These would be like I'd want to associate some help text with the TabPanel. So I imagine I'd want to write my markup like below for the tabpanel:
<cc1:MyTabPanel ID="mtp1" runat="server">
<HelpTextTemplate>
This is your step 1 which is about ...
</HelpTextTemplate>
<ContentTemplate>
Content goes here...
</ContentTemplate>
</cc1:MyTabPanel>
So what do you do to make markup like that...? And how would our control from code behind be able to access data between HelpTextTemplate - which may contain server controls and all?
Moreover, notice that there is a button called 'Save' in the above pic. The user simply drags and drops into the tab panel. And when the user double-clicks on it we have a method stub generated in the code behind (which belongs to the aspx page). How is all of this achieved?
And to cap the whole solution off, I realize we have to wire some javascript to simulate that tab functionality. There is css here too (Notice the images behind the tabs - the gradient, etc). The aspect that I am looking at is making this into a control that the users can use out-of-box just like the toolkit's tabcontainer control. Hence the css/javascript should kind of be bundled. How to achieve this?
Edit:
I am also interested in making the control designer (design-time interaction) part. I am looking for functionality the same way we have for the asp.net wizard control. I have found answers to some of the questions I had above will add it when I find time.
For embedding a script or image to the asp.net custom control I found a solution mentioned in the below site:
Embed js resource with custom asp.net control
What I suggest here it may sound too much, but I can not think other easy way for what you ask and the way you won it.
Grab the source code of the TabContainer, clone it, and make all your custom settings base on that source code. The first steps is to get the full source code of this asp.net toolkit and make a build that working. The second step is to add a clone of the TabControl, with new names. Then you work on this clone to make your changes as you wish for. The final step is to try to separate your custom control in a stand alone library if this is possible.
Download the latest version of the full asp.net ajax control toolkit
http://ajaxcontroltoolkit.codeplex.com/SourceControl/list/changesets
Here you can see online the source code for the TabContainer only
http://ajaxcontroltoolkit.codeplex.com/SourceControl/changeset/view/2c482e5ad6c4#Server%2fAjaxControlToolkit%2fTabs%2fTabContainer.cs
The control you are trying to build is not incredibly complex, but it does involve a number of different techniques.
I would suggest creating your own control from scratch rather then inheritting an existing one. Probably using CompositeControl as the base would be best since it gives you a lot of flexibility.
For HelpTextTemplate/ContentTemplate you'll want to create some ITemplate containers, take a look at this article http://msdn.microsoft.com/en-us/library/aa478964.aspx on how to set these up. Since you may want to access the contents/controls in HelpTextTemplate take a look at this article for how to access them: ASP.Net ITemplate - ways of declaring.
For the tabs, since this is custom, I would probably avoid AjaxControlToolkit. Instead I would include a reference to jQuery UI and use jQuery UI Tabs: http://jqueryui.com/demos/tabs/. Your CompositeControl just needs to output some divs, ul/li elements and you'll be good to go for making the tabs.
If you are fixated on using the AJAX Control Toolkit Tabs then you still can. You'll need to instantiate an instance in your custom control, add it to the control tree, and then use a technique like this: http://msdn.microsoft.com/en-us/library/0e39s2ck.aspx to transfer the contents of your template to the tab pages.
Being able to drag and drop a control from the toolbox onto your page is simple; if your server control library is already part of the same solution as your website then it will just show up. Worst case scenario you can use the Add Items option and add the DLL by browsing for it. As for how the Click event is created when you double click a button, that is done through an attribute on the class, take a look at this tutorial on setting up default events: http://msdn.microsoft.com/en-us/library/43sxkdeb.
As for embedding javascript into the library, these two questions cover how to do this specifically for jQuery UI, if you choose to go some other route it should still be pertinent: How to embed jquery library in asp.net custom server control?, http://forums.asp.net/t/1599621.aspx/1.
As for design time support, try reviwing Microsofts article on this (includes a sample): http://msdn.microsoft.com/en-us/library/aa478960.aspx or this CodeProject article on it: http://www.codeproject.com/Articles/9227/ASP-NET-Server-Control-Design-Time-Support.

ASP.NET (webforms): Using with MINIMAL server controls and substituting with JQUERY?

I am currently working with ASP.NET and the person who designed the form has used all Server Controls for things like TextBoxes and Dropdowns etc when really they are not providing postbacks.. Some of the dropdowns and textboxes are values that I need only in jQuery so as far as I can see there are no drawbacks to coverting these controls to standard html controls rather than ASP.NET server controls?
I suppose I will need to continue to have my GetDataGrid button as a server control because I will need it to postback (and receive PageLoad events etc - all asp.net events) to update the GridView? Or would it be possible to use the GridView (ASP.NET server control) from a Webmethod and call it via Jquery?
Of course in my webmethod I would need to the instance of the gridview to add the datasource - but I don't see how this would be possible without being in the ASP.NET events - or maybe I wrong?
The other thing I thought of was changing the GetGridView button to a standard HTML and calling the javascript postback from the client click event?? This way it would do a real postback and I would end up in Page_load.
Taking everything into effect i don't want to the change the GridView asp.net control as it funcions well as an asp.net server control but i am unsure how i would do this.
I remember a document being available that said "how to use asp.net webforms without server controls" but i can't seem to find it. I suppose using webforms like asp.net MVC - but i can't change the project to MVC - its out of my control.
I would love to hear some feedback with regards to how to do this or comments etc.
I find ASP.NET webforms to inject a lot of code smell into pages - I am using .NET 3.5 so a lot of the output is with tables etc...
If you use Request.Form["..."] then you can get the information which was filled in in standard html input fields.
Instead of keep on using the GridView control I suggest you take a look at either jqGrid or the new templating system that Microsoft put into place for jQuery (currently a plugin but expected to be part of core jQuery from version 1.5 on). These can bound to json which can be retrieved from a webmethod or pagemethod call to fill up the template with data.
Also i don't think its possible from asp.net (code behind) to receive values of an html >control without it having runat=server.
Use webmethods.
Set a client event (like 'onchange') on the html control and then in javascript function called when the event is fired you can use PageMethods to send your data to the code behind.
Some thoughts...
The GridView can't be created in a WebMethod and even if there was a way to get that to work, you'd be better off going with a genuine client side grid. As that's not an option, I don't think there is too much point in trying to make any major changes to your existing pages.
ViewState
Changing the textboxes, buttons etc to HTML versions, would gain you a little bit in reduced Viewstate size but add a bit of complexity in how you handle interactions with the page. You can add runat="server" to HTML controls which will give you control over what is rendered and still have access to the control on the server side.
.Net 4 gives you far more control over viewstate but unfortunately in 3.5 its not as easy.
The GridViews
You could wrap the GridViews in UpdatePanels. That's a 'cheap' way to add some interactivity to your pages although you won't be gaining anything in terms of performance.
It's also still possible to manipulate the Gridview using jQuery on the client-side. There a lots of tutorials, blog posts etc explaining how to do this on the Internet.
MVC with Webforms
Its also possible to mix ASP.Net MVC with Webforms in the same website. As it sounds like you are familiar weith MVC, you might want to consider this approach for any new pages. Here's a blog post explaining how to do this.
Update:
Here's a more recent article by Scott Hanselman on how to use MVC with an existing Webforms application.

What is the difference between UserControl, WebControl, RenderedControl and CompositeControl?

What is the difference, what is the official terms, are any terms obsolete in ASP.NET 3.5?
UserControl: A custom control, ending in .ascx, that is composed of other web controls. Its almost like a small version of an aspx webpage. It consists of a UI (the ascx) and codebehind. Cannot be reused in other projects by referencing a DLL.
WebControl: A control hosted on a webpage or in a UserControl. It consists of one or more classes, working in tandem, and is hosted on an aspx page or in a UserControl. WebControls don't have a UI "page" and must render their content directly. They can be reused in other applications by referencing their DLLs.
RenderedControl: Does not exist. May be synonymous to WebControl. Might indicate the control is written directly to the HttpResponse rather than rendered to an aspx page.
CompositeControl: Inbetween UserControls and WebControls. They code like UserControls, as they are composed of other controls. There is not any graphical UI for control compositing, and support for UI editing of CompositeControls must be coded by the control designer. Compositing is done in the codebehind. CompositeControls can be reused in other projects like WebControls.
You've forgotten the ServerControl.
In my understanding it is like that:
There are only two different kind of controls: UserControl and ServerControl
CompositeControls are kind of "advanced" UserControls. Find some more info on Scott Guthries Blog.
All of them are WebControls (because they are all derived from System.Web.UI.Control)
They are all rendered in any way so i would like to see them all as rendered controls.
From MSDN:
User Control
In ASP.NET: A server
control that is authored declaratively
using the same syntax as an ASP.NET
page and is saved as a text file with
an .ascx extension. User controls
allow page functionality to be
partitioned and reused. Upon first
request, the page framework parses a
user control into a class that derives
from System.Web.UI.UserControl and
compiles that class into an assembly,
which it reuses on subsequent
requests. User controls are easy to
develop due to their page-style
authoring and deployment without prior
compilation.
Server control
A server-side component
that encapsulates user interface and
related functionality. An ASP.NET
server control derives directly or
indirectly from the
System.Web.UI.Control class. The
superset of ASP.NET server controls
includes Web server controls, HTML
server controls, and ASP.NET mobile
controls. The page syntax for an
ASP.NET server control includes a
runat="server" attribute on the
control's tag. See also: HTML server
control, validation server controls,
Web server control.
Like Web Forms, user controls can be created in the visual designer or they can be written with code separate from the HTML. They can also support execution events. However, since Web user controls are compiled dynamically at run time they cannot be added to the Toolbox and they are represented by a simple placeholder when added to a page.
This makes Web user controls harder to use if you are accustomed to full Visual Studio .NET design-time support, including the Properties window and Design view previews. Also the only way to share the user control between applications is to put a separate copy in each application, which takes more maintenance if you make changes to the control.
Web custom controls are compiled code, which makes them easier to use but more difficult to create. Web custom controls must be authored in code. Once you have created the control you can add it to the Toolbox and display it in a visual designer with full Properties window support and all the other design-time features of ASP.NET server controls. In addition you can install a single copy of the Web custom control in the global assembly cache and share it between applications, which make maintenance easier.
Contrary to Will's response, it is possible to reuse UserControls in other projects by referencing a web deployment project.
Since I don't have enough reputation yet to comment, I'll add this as an answer, but it refers to Will's answer above.
From the link you included:
Composite controls are the right tool to architect complex components in which multiple child controls are aggregated and interact among themselves and with the outside world. Rendered controls are just right for read-only aggregation of controls in which the output doesn't include interactive elements such as drop-down or text boxes.
I believe the documentation is refering to UserControls that have been created by overriding the Render method as Rendered Controls. Thus, it is not a separate type as the question implies, but a way of implementing a UserControl; a pattern.

Resources