Override FormView Templates - asp.net

By default the FormView control creates html like :
ID <asp:TextBox ID="IdTextBox" runat="server" Text='<%# Eval("ID") %>' />
<br />
Name <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Eval("Name") />
I prefer:
<ol class="form-layout">
<li><asp:Label AssociatedControl="IdTextBox" runat="server">ID:</aspLabel><asp
....
</ol>
My plan is to create a new control ( OrderedListFormView ) that inherits the FormView and overrides the method that generates the default "crap" html. I have been unable to find the method. Can anyone help? Do you have a better solution that costs $0 dollars?
I would prefer to change the default behavior at design time.

You sound like you have the ASP.NET form blues. Have you tried ASP.NET MVC? It gives you far better control of your rendered HTML, and you can mix it in with existing ASP.NET applications.

Try using Control Adapters to change the rendered HTML from a FormView, there is a tool kit and are pretty easy to code
http://weblogs.asp.net/scottgu/archive/2006/09/08/CSS-Control-Adapter-Toolkit-Update.aspx
http://msdn.microsoft.com/en-us/magazine/cc163543.aspx

Related

ASP.NET closing tag

When I use autocompletion in VisualStudio 2010 within my .aspx application, there are different default completions at closing control tags:
<asp:CheckBox />
<asp:Label></asp:Label>
Is there a explaination for this behaviour?
<asp:CheckBox></asp:CheckBox>
<asp:Label />
Wouldn't be invalid.
This is because ASP.NET's Label control is decorated with the ParseChildrenAttribute, with ParseChildren(false) while CheckBox isn't.
You can support the same behavior whith your custom controls, for example, with the following code, Visual Studio will behave like Label if you use MyControl in the web form editor:
[ParseChildren(false)]
public class MyControl : WebControl
{
...
}
The label closing is like that
<asp:Label runat="server"></asp:Label>
because usually we type something between
<asp:Label runat="server" ID="lblOne">better start programming now</asp:Label>
that is not the case for checkbox, that we type inside of it
<asp:CheckBox runat="server" Text="enable" ID="cbOne" />
We have on both elements the Text field, why on the one we prefer to write out side... Look at this example, on Label, or On other similar controls the text that we may have to write may include characters that are not allowed inside the Text Property, maybe a complex css style or what ever... The check box from the other side is only include a small text (yes, not, something like that)
<asp:Label ID="lblLabel" runat="server">
This is a <b>"label"</b>
<br />And one more line
</asp:Label>
and more example that compiles
<asp:Label ID="lblLabel" runat="server">
This is a <b>"label"</b>
<br />And one more line
<asp:Literal runat="server" ID="ltrOneMore">One more Control Inside</asp:Literal>
</asp:Label>
---but this is not compile--
<asp:Label ID="lblLabel2" runat="server"
Text="This is a <b>"label"</b>
<br /> and one more line"
/>
At the final end is a decision that the makes make - maybe we need to ask them for the real real reason.
Now this is also not compile
<asp:CheckBox runat="server" ID="cbMyChbx">one<asp:CheckBox>
check box when is render on page is use two controls, one input and one label, so they maybe need to help user to understand that the text is not going on the input control.
<asp:CheckBox />
Because the element has no content, you can close the tag with /> instead of using a separate closing tag
<asp:Label></asp:Label> or <asp:Label />
Displays static text on a Web Forms page and allows you to manipulate it programmatically.
Learn more about it Web Server Control
All the answers above are valid, but something additional. All the asp controls are eventually rendered as HTML controls and that also defines how the asp controls behave. For e.g. it is not necessary that text in a label is always set as
<asp:Label runat="server" ID="lblOne">better start programming now</asp:Label>
it can be also done as follows
<asp:Label runat="server" ID="lblOne" Text="better start programming"></asp:Label>
now both are correct format, so it is not valid to say that any control which needs content will have a separate closing tag. It also depends on how it rendered in HTML. for e.g by default asp Label is rendered as a span and doesnt conform to XHTML standards. Hope this makes it clear, always think of how it will be rendered and ASP tries to adhere to what eventually will be rendered.
cheers

Why can't controls in Templates be referenced via their parent?

I've been wondering to why you have to use FindControl to reference the checkbox in the Login1's LayoutTemplate. Example:
var login1CheckBox1 = (CheckBox)Login1.FindControl("CheckBox1");
I would expect to be able to do something along the lines of:
var login1CheckBox1 = Login1.LayoutTemplate.CheckBox1;
In the case of the Repeater below, it is obvious, because there can be n number of CheckBoxes.
But for the Login control, it doesn't seem to make sense. Why wouldn't this be implemented differently?
<asp:Login ID="Login1" runat="server">
<LayoutTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</LayoutTemplate>
</asp:Login>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:Repeater>
Does anyone have any light to shine on this?
A control added to a page via markup is defined in the designer partial class, generally, at design time.
A control added to a template is generally instantiated programmatically within the control's collection of controls.
Since the control added to the template does not exist at compile-time in the definition of that control, it would be rather impossible to achieve the syntax you're aiming for.
When creating a page in markup, we're using the IDE's facilities to generate a partial class. When defining a template in markup, we're simply setting the value of the ITemplate for that control.

ASP.NET server tags rendered in client HTML, not values?

Maybe I've forgotten how to use these, but I am going crazy trying to inject a server-side value into an HTML output. There are reasons why I am doing this inline, and not server-side, so please don't suggest that as a solution.
This code on the server side:
<asp:Label ID="Label1" runat="server" Text='<%= DateTime.Now.ToString() %>' />;
Renders as this in the client HTML sent to the browser:
<span id="Label1"> <%= DateTime.Now.ToString()></span>;
And it displays as big fat empty space, and nothing output to the interface.
If I change the ASP source to using the "#" character to define as data-binding syntax, then the rendered output to browser becomes:
<span id="Label1"></span>
EDIT:
Setting Label text was just a simplified object for the sake of asking the question. In real life, I am setting the CssClass attribute, which does not allow me to use the "wrapping" workaround some have suggested. I wanted to set a public property and have all the controls update from it dynamically on page load.
Ideally, since I already have all the controls laid out on the aspx page. Just looking to add an attribute. I wanted to have:
<asp:textbox ID='MyTxtBox1' CssClass='<% strVal1 %>' />
<asp:textbox ID='MyTxtBox2' CssClass='<% strVal1 %>' />
<asp:textbox ID='MyTxtBox3' CssClass='<% strOtherVal %>' />
<asp:textbox ID='MyTxtBox4' CssClass='<% strVal1 %>' />
Now what it looks like I need to do is repeat all my (250+) controls on the codebehind in a block of code that looks like:
MyTxtBox1.CssClass=strVal1
MyTxtBox2.CssClass=strVal1
MyTxtBox4.CssClass=strVal1
MyTxtBox3.CssClass=strOtherVal
I believe that may not work on a compiled Web Application as it's not interpreted at run-time like a C# "Web Site". However, I was able to get it to work wrapping the label around the value:
<asp:Label runat="server"><%= DateTime.Now.ToString() %></asp:Label>
Set the Label1.Text = value instead of trying to use server side attrs inside of the server control

Modifying default templates for FormView

I use the FormView control quite a bit, but I wish I had more control over the default templates.
When I drag a FormView from the toolbox onto my page and point it to a DataSource control it prepopulates the ItemTemplate, EditItemTemplate and InsertItemTemplates, but it doesn't do it very well.
For example, the InsertItemTemplate looks like this by default:
<InsertItemTemplate>
id:
<asp:TextBox ID="IDTextBox" runat="server" Text='<%# Bind("id") %>' />
<br />
Name:
<asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
...
</InsertItemTemplate>
But what I would prefer is a good old fashioned html table
<InsertItemTemplate>
<table>
<tr>
<td>
id:
</td>
<td>
<asp:TextBox ID="IDTextBox" runat="server" Text='<%# Bind("id") %>' />
</td>
<tr>
...
</table>
</insertItemTemplate>
I know I can use a DetailsView to get a table rendered out, but I end up modifying the form so much that I prefer to use the FormView. It's just that I would like Visual Studio to start me out a bit closer to where I want to end up.
I would imagine that there is a T4 template somewhere in the guts of VS that I might be able to modify to get this done.
Anybody had any luck with this kind of thing?
Wow interesting question, but I don't think it is modifiable or a T4 template; I personally think its in the FormView's control designer that the control implements. These types of design-time settings can be present there. Unfortunately, a lot of the features within the design-time framework aren't customizable like that, unless you have a third-party plugin.
However, you may be able to achieve what you are looking to do through a snippet, as depicted here: http://vbcity.com/blogs/mike-mcintyre/archive/2009/02/03/visual-studio-2010-net-4-0-snippets-in-aspx-page-markup.aspx
If you click the control itself within visual editor of visual studio, there is a little arrow, click that and there is an option there to stop using the default template.
Sorry to be vague, but I don't have Visual Studio on my home PC

ASP.Net Localization & Bound controls

When I localize an asp.net page that is using bound controls (DetailsView, etc) that has TemplateFields bound using the <%# Bind() #> syntax, after the localization all of the bindings are removed and I have to go back in & rebind everything. I'm creating the localized resource file by switching to design view, then Tools / Generate Local Resource from the menu.
Has anyone else seen this problem, and if so, do you have any suggestions for a workaround?
Before:
<asp:TemplateField HeaderText="First Name:">
<InsertItemTemplate>
<uc:FirstNameTextBox runat="server" ID="FirstName" ValidationGroup="Main" Text='<%# Bind("FirstName") %>' />
</InsertItemTemplate>
</asp:TemplateField>
After:
<asp:TemplateField HeaderText="First Name:" meta:resourcekey="TemplateFieldResource1">
<InsertItemTemplate>
<uc:FirstNameTextBox runat="server" ID="FirstName" ValidationGroup="Main" />
</InsertItemTemplate>
</asp:TemplateField>
Edit: Looks like its just my own UserControls that lose the binding. I tried adding the Bindable and Localizable(false) attributes to the properties, but that didn't seem to help.
Just found this... http://blog.smart-ms.ordina.nl/Generate+Local+Resource+Files.aspx
Seems to do the trick without mangling your ASPX file at all... I've not run it over a master page / user control yet.
Yes! I had this happen to me, but just on user controls as well. Is this a normal problem then? I don't know how to resolve it though.
Out of interest, is there any alternative to using Tools -> Generate Local Resource from the menu for building resource files??? I generally already attach my meta:resourcekey tags onto my localizable content controls and don't want it to change it for anything else.

Resources