Put strings together in aspx file - asp.net

I have this code
<asp:CheckBox ID="ddd" Text='Accept terms runat="server">
This adds a link on "Accept terms". But I only want the link to be on the word "terms". I have tried different stuff, but none works. Most of the examples I found on the internet, were where the strings where pulled up from the database, and were not static. That didnt work. Basically I want this:
<asp:CheckBox ID="ddd" Text='Accept' + 'terms runat="server">
Notice the Accept + terms.
Another struggle I have, is that this same code:
<asp:CheckBox ID="ddd" Text='Accept terms runat="server">
makes the checkbox and text "Accept terms" be on 2 lines, but I want them on one line. What am I doing wrong?

You're almost there
<asp:CheckBox ID="ddd" Text='Accept terms'
runat="server"/>
You missed a ' at the end of you Text attribute. I've also moved the anchor to term as it seems that's what you're looking for.

Change your code to:
<asp:CheckBox ID="ddd" Text='Accept terms' runat="server" />
This should work just tested it.

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.

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

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

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.

Strange - Clicking Update button doesn’t cause a postback due to <!-- tag

If I define the following template inside DetailsView, then upon clicking an Update or Insert button, the page is posted back to the server:
<EditItemTemplate>
<asp:TextBox ID="txtDate" runat="server" Text='<%# Bind("Date") %>'></asp:TextBox>
<asp:CompareValidator ID="valDateType" runat="server" ControlToValidate="txtDate" Type="Date" Operator="DataTypeCheck" Display="Dynamic" >*</asp:CompareValidator>
</EditItemTemplate>
If I remove CompareValidator control from the above code by simply deleting it, then page still gets posted back.But if instead I remove CompareValidator control by enclosing it within <!-- --> tags, then for some reason clicking an Update or Insert button doesn’t cause a postback...instead nothing happens:
<EditItemTemplate>
<asp:TextBox ID="txtDate" runat="server" Text='<%# Bind("Date") %>'></asp:TextBox>
<!-- <asp:CompareValidator ID="valDateType" runat="server" ControlToValidate="txtDate" Type="Date" Operator="DataTypeCheck" Display="Dynamic" >*</asp:CompareValidator> -->
</EditItemTemplate>
</EditItemTemplate>
Any idea why page doesn't get posted back?
thanx
Try using server-side comments:
<%--<asp:CompareValidator ID="valDateType" runat="server" ControlToValidate="txtDate" Type="Date" Operator="DataTypeCheck" Display="Dynamic" >*</asp:CompareValidator>--%>
You can also use the Enabled="False" attribute.
I got bitten by something similar in (someone else's!) JSP last week. The HTML comments don't stop the tag from being parsed; if it generates an HTML comment, you end up with nested HTML comments and all manner of strange things going on.
The only "safe" use of HTML-style comment delimiters in an ASP/JSP is where there is no generated content between them:
<!-- This is safe -->
We know exactly what that will be after the ASP engine has parsed it - it'll look the same. The problem may bite you when you have an ASP tag in there:
<!-- This might not be safe because I have no idea
what <asp:joesCustomTag/> expands to -->
If that tag generates an HTML comment, you'll have an HTML comment in your HTML comment! Let's see what happens when that custom tag is parsed and the HTML gets sent to the browser:
<!-- This might not be safe because I have no idea
what
<!-- Joe's custom tag -->
<p>Joe is 1337</p>
expands to -->
You can see the problem right there - SO's own parser is confused by the nested comments.
It's safest to assume that every tag generates an HTML comment, and that putting it in an HTML comment will bite you. Even if it's your tag, you might add an HTML comment to it later.
The answer, as Paperjam says, is to use the correct server-side comments to comment things out - though, of course, you really shouldn't be leaving commented-out dead things around waiting to bite you. Only use HTML-style comments if you actually want a comment to appear in the HTML source.

Resources