How can I hide <div> in an HTML page in ASP.NET? - asp.net

I have one div as the following:
<div>
<% # IIF(DataBinder.Eval (Container.DataItem,"Specifiction2").ToString()<> "","")
<div>
I want to hide the above div when "spcefication2" is blank on a .aspx page.
How can I do it?

A couple of things.
You are using IIF, but IIF should never be used. Always use IF instead. The only reason to use IIF is you are stuck with a pre-2008 compiler, and even then, you should use something else.
The easiest way to do what you are describing is via an id and runat="server", you can then either set the visible property (which will mean that the div never gets emitted) or set the style to display:none
If you want to do it inline, per your example, the code is
<div style="display:
<%# IF(string.isnullorwhitespace(DataBinder.Eval(Container.DataItem,"Specifiction2").ToString), "none", "block") %>">
</div>
You are using Eval and container.dataitem, you should look into using ItemType For your grid/repeats, then referencing the value as item.Specification
The above would be much easier if you declared a function in your code behind, taking a string and returning the string none/block.

Related

Nested Literals

I have an ASP.NET website that is not passing the W3C XHTML validation.
It doesn't pass validation because I place <div> content into a <asp:Label>, and so the resulting markup looks like:
<span><div>stackoverflow</div></span> <!-- INVALID; DIV INSIDE SPAN -->
However, after replacing all my <asp:Label> with <asp:Literal>, I get errors that <asp:Literal> cannot be nested inside another <asp:Literal>.
I don't really understand how I'm suppose to be solving this, since <asp:Literal> sounds like it would have otherwise been exactly what I wanted.
Is the correct solution to use <asp:PlaceHolder>?
Try this solution
<div id="div" runat="server">
</div>
in code behind
div.InnerHtml = "<div>Example HTML</div>";
Duplicate, answered in: Using Panel or PlaceHolder
Short answer, an <asp:Panel> becomes a <div>, so you want something like
<asp:Panel>
<asp:Label .../>
</asp:Panel>

asp.net, how do you separate a repeater using page break

I'm trying to create a batch of letters using a repeater but so far when I convert the letters to PDF the contents of the letters are written one after the other on the same page. I want to split the repeater up so that each letter has its own page. How do I do this?
Please help me!! :)
usually when you convert to PDF, page breaks are controlled with standard CSS styles like page-break-before:always and page-break-after:always applied to any HTML object. These styles can be used either in a CSS class to be applied to an element or inline in the element style attribute.
so in your repeater you should do something like this (pardon my pseudo code):
<asp:repeater>
<ItemElement>
<div style="page-break-after:always;">
<asp:Label id="lblLetter">
</div>
</Item>
</asp:repeater>
where lblLetter is set to your letter in server-side event.

Trouble getting a background image to appear in HTML

I have the following HTML code:
<div style="background-image:url(~/Images/MyImage.jpg); width:100%; height:100%">
I'm not too familiar with HTML, but form all the articles I've seen, this should work fine. If I show the image in an asp.net image control then it shows fine. However, I want to put text on top of it. Can anyone tell me what I'm doing wrong, please?
By default, you cannot use the ~ in the paths of normal HTML elements. That would only be understood by ASP.NET, but your element is being treated as markup and simply sent to the browser without being processed by the server-side script.
You should either add an id attribute and the attribute runat="server" to your <div> so that it is recognized by ASP.NET, or else you would have to use a relative or absolute URL without the ~ in the path:
<div id="mydiv" style="background-image:url(~/Images/MyImage.jpg);" runat="server">
or
<div style="background-image:url(/Images/MyImage.jpg);">
Your div is not evaluated as a server control, thus the "~" char is not recognized as it should. Not 100% sure, but adding runat="server" should work...
<div style="background-image:url(~/Images/MyImage.jpg); width:100%; height:100%" runat="server">
In the case above whatever text you put between the <div ...> </div> would come over the background image.
Also as the runat = server tag is missing it won't even be touched by asp.net. So your url is also wrong by using ~.

Making Text box not editable

work on C# asp.net vs 05. I have a requirment, I have to fill text box with some data on gridview , which is coming from database and make it read-only
After that user can not enter any text on gridview template field. If I set textbox Enabled=false, then i lose text color, but i want to show text color. Just textbox to not be editable. I just want users to not be able to write anything in my textbox.
Isn't there a readonly property for the text box.
If then you can use
ReadOnly="true"
for the text box.
If you can use a label then I would prefer that one.
For wrapping contents inside a label you can use
word-wrap
word-wrap: break-word
Inside the properties choose your column field and in the properties.
Under Styles section
You can give a CssClass to the column.
If you specify CssClass as 'TextStyle'
the css looks like this
.TextStyle
{
color: #a9a9a9
}
In the color attribute give either the color name like 'red' or the hexcode like '#000000' for the text.
Enabled="false"
I'd use CSS to make a label look like a textbox.
Why does everyone insist on using an asp:label for this sort of thing? Just because it renders a <span> if you don't supply an AssociatedControlId?
You should look at using an asp:Panel or possibly an asp:Literal or asp:PlaceHolder for this as they will give you greater control over the output, and cleaner markup.
Panel would be better, as this will render the contents in a <div> rather than the PlaceHolder or Literal which won't add any extra markup.
The content you are putting in here is a semantic division, and so should really be marked up as such, and by default, <div>s are treated as a block display type, rather than as inline (<span>).
Here's a quick proof of concept I just knocked up.
<head runat="server">
<style>
.readTest
{
border: none;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox runat="server" Text="Do something" ReadOnly="true"
ID="txtOne" CssClass="readTest"></asp:TextBox>
</div>
</form>
</body>
</html>
Personally I would use a label, though you could write a custom control that renders a textbox or a label, depending on the state your app is in.
I'd prefer to write the text directly to the cell using a literal or String() output rather than using a control. It's a bit cleaner and will help minimize the amount of ViewState being written. Either that, or turn off ViewState for those controls being added.
Specify your CSS for the TextBox in the row created event of the GridView.

Why does my ASP.NET page add the prefix 'ctl00_ctl00' to html element IDs and break the design?

I don't understand it.
The ids of html elements in the master page are changed by the same id but with a prefix and it's breaking the css design.
In the master page I have:
<div id="container" runat="server">
<asp:ContentPlaceHolder ...
...
The above code is rendered
<div id="ctl00_ctloo_container">
...
And the CSS styles are gone obviously.
How do I stop it?
Thanks!
WebForms should only rewrite the ID's of server controls (like <asp:ContentPlaceHolder />, not ordinary HTML element like <div id="container"> without runat="server"
You cannot prevent the framework from rewriting ID's on server controls. You can use class names instead, though.
AFAIK you cannot do this. This is the default behaviour because of the control tree.
If you would like to use CSS then set the CSS class directly, don't depend on IDs, like
<asp:Whatever runat="server" id="whatever" CssClass="whateverClass">
Update: Here is a similair thread, but it won't help on your CSS problem.
Do you need the runat="server" tag? If you do not do anything with the div element in your code-behind, remove it.
I do not need the runat="server" tag and have removed it. Don't know why it was there ... Ids are not changed now. Thanks

Resources