single template server control - asp.net

Is it possible to have a custom server control with a single template (meaning the user can put any text they want) without having to require the an "ItemTemplate" like in a FormView control?
I would want the control in Source View to look like this
<foo:mycontrol runat="server" id="controlid">
User puts whatever html content they want here
</foo:mycontrol>
INSTEAD OF THIS
<foo:mycontrol runat="server" id="controlid">
<ItemTemplate>
User puts whatever html content they want here
</ItemTemplate>
</foo:mycontrol>
My custom server control needs to add 2 asp.net panel controls and the ajax collapsiblepanel control. one panel will be the expand/collapse panel and the other panel is what I would want to put the user text into and then have the collapsible panel collapse and hide the panel.
I know how to do this (at least I think I do) creating a composite server control and using ITemplate but that requires the child <ItemTemplate> tag in source view.
Any ideas?

I haven't tested this but I would think you could do this by inheriting from the literal or label control and then reading/writing to the Text property.
p.s. next time when you post a question check the preview to see if it's readable and format code with 4 spaces in-front so it's actually shown and syntax highlighted.

Hmm why don't you use an approach where you just specify the controls to be collapsed. For instance your declaration could look like
<foo:mycontrol runat="server" id="controlid" TargetControlId="pnlToCollapse" />
Internally, your mycontrol gets an instance of the specified TargetControlId by using the FindControl method (here's a recursive version). The same could be done for the 2nd panel you need.
Your server control does therefore just take configuration info and doesn't render anything, but controls the rendering of the other panels in this case. It is a much more flexible solution in my eyes.

Juri,
I should have clarified that this control might be used by non-developers or developers who we do not want to require too much knowledge of setting properties so I'm trying to develop a control where they would drag it on like a panel and just enter text and maybe set one property which would be the title. I was able to create a compositecontrol which I created a title panel, the collapsible panel and utilized the asp.net ajax collapsiblepanel control. I had to add an Template though which I would prefer not to do.
Darrell

Related

ASP.NET gridview user control

Everybody use the grid view control in his project extremely, at least I do so.
I put a nice style to my grid and I erased the borders and cell spacing etc .
I also bind it from my database by a stored procedure. I write the paging method and the page index changing method etc .
Now I want to package it in a user control to use this grid in any project or page,
because it's to frustrating to make all of this at every page and every project.
But when I put it in a user control, I couldn't make a new columns and template field from the html.
uc1:WebGrid runat="server" ID="WebGrid" />
<!-- i want to be able to do this -->
<!--
<columns>
TODO
</columns>
-->
The answer:
this link is a very helpful except it's in a vb code :(
Putting the grid view into a user control will lock you off from making <Columns> definitions in your markup where you include your user control, because the grid view definition is not accessible via properties of the user control.
From the description of the changes you made that you want to reuse elsewhere, it seems to me that you are better off creating a CSS class that can be applied to all grid views in your application instead of a user control.
As for the paging logic, you can have that be in a utility class that all grid views in your application can call to do their paging, if the paging logic is generic enough.
The bottom line is you have locked the structure of your grid view by putting it into a user control, because there is no mechanism to alter the structure. You could try to build properties that allow for the alteration of the structure, but it will not be the familiar <Columns> syntax most people are used to.
UPDATE:
If you want to "extend" the grid view, then that is a whole different story and this is what companies like Telerik, etc. do so they can use the base functionality of the grid view and then provide value added.
Check out this previous StackOverflow question for details on extending the GridView and then exposing it as a control to be used elsewhere in your application - use class extending gridview as control.

implementing mostly repeating content in ASP.NET WebForms' controls

The original problem is how to make the most maintainable code given that I need to have a menu bar that is very similar but not identical in most WebForms.
At first glance I thought I wanted to make a placeholder in a custom user control, which was placed in a master page, then access the placeholder, putting in the unique content within the WebForm and all is happy and maintainable. After reading through the forms and through trial and error, I realize there are some problems with every solution I have tried:
Putting a placeholder control in a user control makes the placeholder and each of its contents difficult to access in the web form (relying on nested instances of FindControl("ID")), events are difficult to bubble up (I assume I have to do this when the control is created in the code behind where it is referenced) or bind (not successful yet despite referencing: http://msdn.microsoft.com/en-us/library/t4z863dh(vs.71).aspx), and the code is difficult to maintain if I want to move/rename the control.
Nesting controls gives the same problem because I want to use one control - making small modifications on many pages. If I nest then I still have to have cross-control knowledge of each other which is difficult in ASP.NET. Regardless, if I nested user controls then I would also need a unique user control per WebForm right?
Copying and pasting the code to each WebForm while simple is also repetitive and error prone.
Apparently, I shouldn't even try to inherit user controls to make unique children: How (if at all) can you make an ASP.NET UserControl inherit from another UserControl?.
I'm new to ASP.NET. Is there some general strategy I should use to make good maintainable code in this scenario?
You need to use a Master Page, and put your menu in there...
http://msdn.microsoft.com/en-us/library/wtxbf3hh(v=vs.100).aspx
Then, for a data-driven menu, try to use one of the template controls like the Repeater to bind your data.
For example, I have a query that returns Page Name and Link - I bind this to a Repeater control to produce my Menu.
<asp:Repeater runat="server">
<HeaderTemplate><ul></HeaderTemplate>
<ItemTemplate><li><a href='<% Eval("Link") %>'><%# Eval("PageName") %></a></li></ItemTemplate>
<FooterTemplate></ul></FooterTemplate>
</asp:Repeater>

Extending .net control: How to prevent HTML atttribute from rendering

I've written a server control that extends the standard .net Gridview control.
However, by default the standard gridview control adds a border="0" attribute to the html markup that it produces, and I want to prevent this attribute from being rendered by my server control.
I could probably use ScriptManager to add a bit of Javascript that removes the attribute once the page that the control sits on has loaded, but that seems a bit clunky and I was hoping that somebody could tell me how to do it cleanly by, for example, adding this.Attributes.Remove("border"); to the PreRender event of the control or something similar (which didn't work by the way, or I wouldn't have to ask the question)
All you need to do is set Gridlines to "None"
refer: GridLines property of the GridView.
<asp:GridView GridLines="None" />
Ofcourse, if you want more control over the rendering, control adapters are what you are looking for.
I am using a devexpress gridview so it might be a bit different, but I noticed the same thing happening, and I added
grid.Attributes["border"] = "";
to the page load event, and the border no longer rendered in html. A bit of a hack but it worked for me.

How do I access Page controls from within my custom server control?

I am building a modal box as a custom server control and I want to have a property on the modal box TargetControlID that specifies the element that will show the modal when clicked. I've set the property up in the modal box and in the code behind I use the following code block (which I've tried in several different places
If (_targetControlId <> "") Then
Dim targetControl As WebControl = Me.Page.FindControl(_targetControlId)
targetControl.Attributes.Add("onclick", "test1();")
targetControl.Attributes.Add("onclick", "test2();")
End If
What happens is that targetControl always winds up to be NULL, and causes the page to crash when I tried to add attributes to it. I've double checked the spelling of the targetControlId and I am specifying a control that is runat="server". What is the proper way for a server control to access other controls on its containing page?
Thanks,
Mike
First of all, I should point out that the behavior you're looking for already exists in the ModalPopupExtender that comes with the free, open-source AjaxControlToolkit. I'd recommend you just use that. If you're still sure you want to write your own, then I'd recommend at least taking a look at their code to see how they go about it. ExtenderControlBase.FindControlHelper is a good place to start.

WebControl vs HtmlControl. Cos and pros using them in web forms application

In web forms application, for server code, when use WebControls and when use HtmlControls? For example if I want write some text inside of span tag, should I use:
<span id="someid" runat="server"></span>
or
<asp:Label id="someid" runat="server"></asp:Label>
The main difference is that HtmlControls only provide a way of addressing a part of the page during the page cycle, whereas WebControls are stateful.
In your example, if you assign some value to the Label text, it will keep it across PostBacks.
In my experience is far better to use HtmlControls if you can, they are much more lightweight and they don't fill up your ViewState. Do use WebControls when you need them to be stateful.
For example, you might want to use a Label for a page title, because you can only assign the value once (typically in Page_OnLoad inside a if (!IsPostBack) block). You might want to use an HTML span to provide some status feedback (where the status is updated at each postback, for example).
I would use the span approach. Whatever server control you use it will finally render as an html control.
If your functionality can be done using an html control better use that..
For a server control like data grid you might have to code more to achieve those functionality by using an html table.
In that case you can use a server control.
one behaviour of asp:button is it always render as input type=submit
and asp:imagebutton always render as input type=image

Resources