ASP.NET closing tag - asp.net

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

Related

How do I add a 'wrap' element to some asp tablerow elements in an ascx file so that I can hide/show them all with my vb code file?

I'm working with an older Server 2003 system and trying to create a new web form, based on other existing forms, which all follow certain conventions. (I'm guessing they were made with visual studio orignaly, but I'm not using this.) I'm not very fermiliar with asp/vb at all, I'm more of a PHP person, so I probably don't know the correct terms for lot a lot of the below.
I have an ascx file that contains my asp front end stuff in it. The main body has an asp table which is used when submitted the form to a database and sending it via email.
<asp:Table ID="inputTable" runat="server" CssClass="inputTable">
/stuff here
</asp:Table>
This contains various asp table rows like so:
<asp:TableRow id="someid" visible="false">
<asp:TableHeaderCell CssClass="LeftColumn">
<asp:Label ID="Label2" runat="server" Text="Some Field Label here"></asp:Label>
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator" ControlToValidate="txt_somefield" Display="Dynamic" EnableClientScript="true" ErrorMessage="You must select an answer"> *</asp:RequiredFieldValidator>
</asp:TableHeaderCell>
<asp:TableCell CssClass="RightColumn">
<asp:TextBox runat="server" ID="txt_somefield" MaxLength="32" Width="98%"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
Some of these will be text fields, others drop downs etc.
I want to be able to 'group' several of these fields together so I can then use by code file (asxc.vb) to then do something like
Protected Sub ddl_someotherid_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddl_someotherid.SelectedIndexChanged
If ddl_someotherid.SelectedValue.Contains("Yes") Then
somecollectionoffields.Visible = False
Else if ddl_someotherid.SelectedValue.Contains("No") Then
somecollectionoffields.Visible = True
End If
End Sub
If I try to put normal html elements in my asp file among the tags, then I will get an error such as System.Web.UI.WebControls.TableRowCollection must have items of type 'System.Web.UI.WebControls.TableRow'. 'div' is of type 'System.Web.UI.HtmlControls.HtmlGenericControl'.
So I understand that I need some kind of asp tag there instead. I'm looking for something that when the asp engine in the web server parses it, it comes out as something that will not mess up the html table the asp has generated, but can be used to hide/show several form elements based on the choices made in other drop down elements above in the form. (I understand the putting a div in between table rows in normal HTML would not be correct, it was just the last tag I tried before posting this.)
If this were PHP, I could just 'interupt' the HTML table with a PHP tag, do my calculations to determine if the set of fields should be shown, then close the tag, but I don't know how to do this in asp/vb.
I did do some searching and found some slightly different questions that shows a asp tag called a panel using something like <asp:panel> but this results in a similar error to above, just telling me that this is not allowed also.
I don't really want to close the overall <asp Table> tag to do this, because this is used to grab the form contents to put it in an email, in the vb file and I don't know how to re-write that to compensate.
Ok, शेखर has sort of helped me come to the realisation that I needed with his comment that
There is a server control (<asp:) corresponding to every html control.
Once I realised that all these asp: tags are just proxy's for actual html tags I realised that the asp parser is just trying to protect me from my own stupidity in creating invalid HTML at the other end. You cannot break up a table by enclosing some of it in a div in HTML, so asp won't let me do it either. I just needed to nestle one table inside another then show and hide the nestled one to achieve what I wanted. It was never about the vb side of things or the more advanced asp stuff.
So like this:
<asp:TableRow id="someid" visible="false">
<asp:TableHeaderCell >
<asp:Table ID="subinputTable" runat="server" CssClass="subinputTable">
<asp:TableRow id="testfield1" >
<asp:TableHeaderCell CssClass="LeftColumn">
<asp:Label ID="Label3" runat="server" Text="Test Field 1"></asp:Label>
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator3" ControlToValidate="txt_test1" Display="Dynamic" EnableClientScript="true" ErrorMessage="You must fill in this field"> *</asp:RequiredFieldValidator>
</asp:TableHeaderCell>
<asp:TableCell CssClass="RightColumn">
<asp:TextBox runat="server" ID="txt_test1" MaxLength="32" Width="98%"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:TableHeaderCell>
</asp:TableRow>
This then works just fine. I can't promice it's the best or most elegant solution, but it's what I was looking for.

.net ascx control not retaining value on postback (mojoPortal)

I'm making a custom module for mojoPortal CMS which needs to allow the client to add an affiliate into the database. As far as I can tell, this requires creating a .ascx file and then installing that using the administration toolbar in the Web interface to get it to a point where I can put it into a page, as http://www.mojoportal.com/hello-world-developer-quick-start.aspx.
The form is simple enough, but the values in the text boxes just stay empty when I submit, though the file upload works fine. The code:
<asp:Label ID="Label1" runat="server" Text="Company Name" AssociatedControlID="CompanyName">
</asp:Label>
<asp:TextBox ID="CompanyName" runat="server"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text="Company Description" AssociatedControlID="CompanyDescription"></asp:Label>
<asp:TextBox ID="CompanyDescription" TextMode="MultiLine" runat="server"></asp:TextBox>
<asp:Label ID="Label3" runat="server" Text="Company Logo" AssociatedControlID="CompanyLogo"></asp:Label>
<asp:FileUpload ID="CompanyLogo" runat="server" />
<asp:Button ID="SubmitButton" runat="server" Text="Add Affiliate" />
EnableViewState for the page and the controls is enabled
The text box is not set to ReadOnly, and there is no funky JavaScript dynamically modifying elements (at least, I didn't set any).
I can work around this by using HTML elements, and get the values using Request.Form. The information is actually there, I can see it in the Request.Form, but I would have to get that by something like Request.Form[CompanyName.ClientId.Replace("_","$")] or Request.Form[6] which both seem very messy and IIRC aren't really the way things are supposed to roll in .NET. Besides, having worked until 3 last night, I really want to know what the answer is now!
Any thoughts anyone?
What I had done was not created a click event for the button, relying on the fact that it was posting to the server (like I would in PHP). Oops! When I added a click event, then the text boxes retained their value when I was within that method.

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

Add a class along with class specified in the skin

We are using themes to style our control.
For textbox we have added like this in the skin
<asp:TextBox runat="server" CssClass="txt edit_txt" />
Now in some cases, we want to add another class along with this 2 classes.
when we added a textbox like this in aspx
<asp:TextBox ID="txtnameE" runat="server" MaxLength="30" CssClass="RequiredCheckClass"></asp:TextBox>
On rendering it didn't take the "RequiredCheckClass" class, but only the other 2 classes specified in the skin.
So is there any way to add a class along with the classes specified in the skin from an aspx page.
PS : now i am going to use #Curt suggestion or will use EnableTheming=false and will add all the required classes to this control. Please update if anyone got any other idea other than these....
One option would be to create another TextBox control in the Skin file like:
<asp:TextBox SkinID="Required" runat="server" CssClass="txt edit_txt RequiredCheckClass" />
And then use the following in your markup file:
<asp:TextBox ID="txtnameE" runat="server" MaxLength="30" SkinID="Required"></asp:TextBox>
Other than that I'm not sure. I've always avoided ASP.NET Themes since I found out how restrictive they are.
You can use the "class" property - just like you use it in a regular HTML element.
thats the bypass that i am using.
<asp:TextBox SkinID="Required" runat="server" class="RequiredCheckClass" CssClass="txt edit_txt" />

asp.net (4) listview gives me troubles with generating id's

i'm in a asp.net listview, in the itemtemplate.
<asp:ListView runat="server" ClientIDMode="Predictable" ClientIDRowSuffix="Texttranslations_key"ID="lvwTextitems">
This is my code in the itemtemplate:
<span runat="server" onclick="openDiv('<%= EditItemDiv.ClientID%>')" style="width: 450px;"><%# Eval("Translation")%></span>
<asp:panel runat="server" id="EditItemDiv" style="display:none">
<asp:TextBox runat="server" ID = "EditItemArea" TextMode ="MultiLine" Rows="12" Columns="50" Text="<%# Eval("Translation")%>">
</asp:TextBox>
Now i have two problems.
First the span: i want the clientID of the asp:panel in the function openDiv(), so i can create some show hide functionality.
However, i get this as result:
<span onclick="openDiv('<%= EditItemDiv.ClientID%>')" style="width: 450px;">
my code isn't seen as code, but as plain text, and i don't know why?
Second, this line gets me a runtime error (The server tag is not well formed):
<asp:TextBox runat="server" ID = "EditItemArea" TextMode ="MultiLine" Rows="12" Columns="50" Text="<%# Eval("Translation")%>">
Can somebody help me out?
ps
at first i used this code for the generation of the id's: "myid<%# Eval("Id")%>" but that didn't workout either...
ps
i'm always getting in to trouble when using the Eval and the <%# %>, so it's probably some stupid thing (i hope)
For the first part, you definitely need to be using a binding expression:
<%# EditItemDiv.ClientID %>
The <%= %> scriptlet will have no context for each item. I assume you were "paraphrasing" the syntax you say you tried, so what didn't work before?
The "server tag is not well formed" is because you are trying to use double-quotes inside double-quotes. Change the outer to single-quotes:
Text='<%# Eval("Translation")%>'>
Basically, you can't nest similar quote types. Inline script will usually demand you use double-quotes, since single-quotes have a different meaning in c#, but you can use either double or single for markup parameter quoting. The upshot is that if you need to have inline script, use single quotes to wrap the markup parameter, which frees you to use double-quotes inside it.
If you need further single quotes in the output, e.g. to render a javascript parameter, just use '. You could also use " if you wanted to render double-quotes.
OnClientClick='openDiv('EditItem(<%# Eval("something") %>');'
As stated in my comment and by jamietre to fix the binding problem you need to change the code from:
Text="<%# Eval("Translation")%>"
to
Text='<%# Eval("Translation")%>'
As for the problem with the onclick of the span, it should work as you want if you just remove the runat="server" portion. I am not sure why, but it seems that adding this causes the controls to encode the onclick property.
If you need the runat="server" on the span then I will attempt to find another solution, but there are not guarantees.

Resources