Only show label control if there is message to display - asp.net

I'm a noob to asp and I am trying to show a label control if there is a message populated dynamically, otherwise hide the label. This is basically and error message control.
I have applied a css style to the control with a colored background, so if there is no message to display, the user still sees the blank control.
I know how to do this with javascript, just not sure if it's possible within the control.

lbl.visible = (lbl.Text.Length > 0);
Where lbl is the ID of your Label control

On the server side all the controls have a Visible property that determines whether they appear on the page or not. You should be able to use it something like the following:
if (String.IsNullOrEmpty(MyLabel.Text)
{
MyLabel.Visible=false;
}

Related

ASP.NET typing text vs using label

What's the difference between typing some text on a page vs inserting a label and typing some text into that label ?
Any reason why somebody would want to use a label vs just type text on the page ?
The only advantage that I can think of is that a label can be updated easily ( e.g user clicks a button , in the event code for the click action one can write something like label1.Text = "some value" )
Thanks
Labels can be associated with controls using the AssociatedControlID property, allowing the user to click the label to focus the control.
If a label is associated with a checkbox, clicking the label will toggle the checkbox.
You've nailed it. Putting text inside a label control allows you easy programmatic control over that portion of the page, while putting it directly in the HTML requires you to then jump through extra hoops if you want to modify it later.
In addition, you can also programmatically show/hide a label, add css styles, and associate it with an input control (AssociatedControlId property).
You can't easily apply CSS styling to random text on the page.
Edit - Sorry I meant in server side code.
The difference is that typing into a Label causes it to render the HTML from the server side while typing text into the HTML does not.
This is very useful if you want to change the text dynamically or if you need to deal with changing the text for internationalization.
ASP.NET labels should be used to much like HTML labels: to indicate which control this text is related to. ASP.NET also has the LiteralControl, which is just text, and is better suited to your needs.
Typing text directly onto your page is often uncontrollable - It is difficult to control where it will appear, and in what manner. Labels have very predictable features that can be adjusted easily to work with formatting. Furthermore, as your page gets more complicated, having text in labels that is identifiable with IDs makes things significantly easier.

Part of custom control should be rendered only once

I am working on a custom control. Part of it is to render a div that is not displayed right away. Based on a particular client event, that div is shown. Everything works fine when there is only once instance of the custom control on a page. But in case of multiple instances, that many above mentioned divs are rendered, though not displayed. To make container page lighter, I basically want to render that div only once, irrespective of the number of occurrences of the custom control. Any help would be great.
Thanks,
Surya
Perhaps you can store a flag telling that that div has already been rendered. You can store that flag in HttpContext.Items. Here is some code
if ((bool)HttpContext.Current.Items["divRendered"] == false)
{
//Render the div
HttpContext.Current.Items["divRendered"] = true;
}

Moving the ScrollBar To the Selected Node In A TreeView Control

I have a treeview control where I am dynamically selecting a node depending on user interaction. when a node is selected I want to be able to have the scrollbar go to the location of that selected node in the tree. The scrollbar is simply made by overflow:auto in the div tag where the treeview is located. Can someone give me some detailed code to accomplish this? Thanks in advance.
If the scrollbar is a browser default triggered by overflow:auto, you'll probably need to use javascript. See if the answer below works for you:
Programmatically scroll to an Anchor Tag
In other words, you will need to figure out the ID of the selected node (or insert an element with an ID into the text of the node), then insert a snippet of javascript into the page (using, for example, a Literal control) that will scroll to that element when the page is loaded.
It's hard to give specific examples without seeing your code, but let's say your selected node is called ActiveNode and you've inserted a literal control called litScript. Then you could do something like this:
ActiveNode.Text = ActiveNode.Text & "<a id='TVSelectedNode'></a>"
litScript.Text = "<script type='text/javascript'>document.getElementById('TVSelectedNode').scrollIntoView(true);</script>"

Conditionally render a control in a webpage

For example what if I am trying to hide a custom navigation control what can I place in another webcontrol to conditionally hide the label something like this
<mycontrol:hidethis id=mycontrol1 runat="server">
<mycontrol:mynavi runat="server"/>
</mycontrol:hidethis>
Thanks
Your question is wide open, there could be a few ways to hide your control.
One very simple way to do it is in the PreRender of the navigation control's parent - just set the Visible property of the nav control to false (if the right conditions are met). If coded correctly (i have to say that, sometimes people code stupidly :) then the Visible property will be propagated to all the child controls of the nav control, and none of them will get rendered to the output stream.
Why don't you add a property to the control which says Visible and you internally set the control as visible true/ false.
Have a look at
protected override void AddParsedSubObject(object obj)
{
}
That should be the right place to say wether subcontrols should be rendered or not

How to populate long text in dropdownlist in asp.net

In asp.net, am trying to populate a dropdownlist box with very long text. I have fixed the width of list on the page and i don't want to change its size as it would affect my page layout. On clicking the dropdownlist, the text gets truncated to the size of the dropdown. I want to see the entire text without any truncation, without changing the size of the dropdownlist box..Also if there are any third party controls which would solve this problem?Would be very helpful if there's a solution for this.
Update:
Right now am trying a dropdown box in Jquery that will wrap the text if it exceeds the size..but again the problem is it works fine on an independent solution but when i integrate it with my application, it does not read the .css file and it does not do any formatting(style, font, alignment) that it is supposed to do according to the .css file.Any idea why this is happening?
The problem that you're describing is restricted to IE (it might be fixed in the latest version, I haven't tested).
In the past, I've had success with binding javascript methods to the onClick event on the drop down to increase the width, and the onBlur event to set the width back to its original value.
You might be able to use jQuery to create a tooltip like thing that appears when you hover over each option.
something like
// this executes on page load - just like asp.net Page_load()
function pageLoad(){
// attach a mouseover event to each option in your dropdown
$("#myDropdown option").mouseover(function(){
// get the text from that option
var text = $("#"+this.id).text();
// display that text in a small tooltip
showToolTip(text);
});
}
function showToolTip(text){
// display in some way
}
there's a javascript library called wz_tooltip available at walterzorn.com
hope that helps

Resources