Conditionally render a control in a webpage - asp.net

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

Related

Uneditable box after ticking it

How can I make it after ticking the box and it turns uneditable right away?
This question is somewhat loaded because the answer is, it depends.
Your screenshot makes it look like that control is in a grid row, which would imply the control is connected to a datasource. If that's the case, do you want only the checkbox to be disabled or the entire row?
You would probably put code in the datasource field's modified method or the datasource's active method.
If the checkbox is a standalone control, you would override the clicked method with something like:
public void clicked()
{
super();
if (this.checked())
this.enabled(false);
}
I believe it's due to Field properties in table.
Seems like it restricts edit after creation.
Otherwise - check code on form (or class maintaining the form). It might be on Control/Field/Datasource/Table modify method.

How to make invisible ASP.NET controls within a certain div tag?

I have a dropdown "ddl_group" list followed by two tags containing controls related to item chosen in the first. One item in ddl_group is teacher and another item is student. By default, I have set all controls within the two div tags to invisible. They only show up when a group related to them is chosen in ddl_group.
Imagine I choose student in ddl_group. Next, controls within div.stdInfo start to appear one after the other in response to user who makes choices on controls within div.stdInfo. Now the user might want to choose another group i.e. teacher from ddl_group. Therefore, I'll need to make invisible again all controls within div.stdInfo and make visible controls within div.teacherInfo.
Is it possible to make invisible or visible all asp.net controls within a certain tag (e.g. div tag) in one place rather than setting them individually?
You can place your div in
ASP.NET Panel Control
then you can make it
pnlName.Visible=true; or pnlName.Visible=false;
when ever you want or
you can use Div with
runat="server"
attribute so that it is accessible in code page
their you can do it like
divname.Visible=true; divname.Visible=false;
You can use ASP.NET Place holder or ASP.NET Panel controls for these purposes.
Wrap your content with Place holder or Panel.
On SelectedIndexChanged event of your drop down, you can change visibility of this PlaceHolder or Panel.
If you add a runat tag and an ID tag to your divs, that will make them accessible via server side code. I prefer this way because I don't have to mess-up my css at all.
e.g.
markup:
<div class='teacherinfo' ID='teacherDiv' runat='server'>
.
.
.
.
.
</div>
code behind:
teacherDiv.Visible = false;
One word of warning, making a server-side control invisible means that it will not be present on the resulting html document. So if you try to find it with javascript document.getElementById("<%= teacherDiv.ClientId %>"), you'll get an error.

Only show label control if there is message to display

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;
}

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;
}

ASP.NET Ajax ReOrderList - Any way to disable specific items from being dragged and dropped?

I have an ajax controltoolkit reorderlist within an asp.net application.
I need to disable certain specific steps from being reordered. This has to be done dynamically. All steps are consecutive and start from the beginning, but it's not known until runtime how many need to be disabled from being reordered any further.
I tried the e.item.enabled = false for reorderlist_itemdatabound but this just disabled links. I need to disable the drag handler.
Any help is greatly appreciated. Thanks!
To be honest, I'm not too familiar with this control, but...
You need to hide the drag handler div (or whatever is in the 'DragHandleTemplate' I believe) and/or change its class. Two suggestions:
1) Add a javascript startup script to disable the divs in question.
2) Subclass this control... Override the Render() method. Replace it with original code from ReorderList, but check the Item to see if you should render the drag handle.
as a workaround to disable drag'n'drop for some item - you can set width=0 to the control inside <DragHandleTemplate> </DragHandleTemplate>. Thus user won't be able to pick the item for dragging.
Suppose you have an image with id dragme in DragHandleTemplate; you can do this in the ItemDataBound handler:
Image dragMe = (Image)((TableRow)e.Item.Controls[0].Controls[0]).Cells[0].Controls[0].FindControl("dragMe");
dragMe.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
This way you preserve the alignment.

Resources