How to Conditionally Control The Visibility of a Control in Asp.Net

How do I conditionally control the visibility of a control in ASP.NET?

You can bind the Visible property of your control to the expression and call DataBind() while the page is loading:

<asp:Image runat="server" id="image" Visible='<%#Eval("Image") != null %>' />

If you are not using server controls and want to show/hide simple markup, you can simply enclose it in an if statement:

<% if ( condition ) { %>
<img src='<%= linkToImageSource %>' />
<% } %>

place controls conditionally on the web form

You need to add an event handler for the OnItemDataBound event for your ListView.

<asp:ListView ID="lstVw" runat="server" OnItemDataBound="lstVw_ItemDataBound">
<ItemTemplate></ItemTemplate>
</asp:ListView>

From that point you can dynamically add controls to the Controls collection of the row's data item.

protected void lstVw_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
int val = int.Parse(e.Item.DataItem.ToString());

switch (val)
{
case 1:
RadioButtonList list = new RadioButtonList();
list.Items.Add("Option 1");
list.Items.Add("Option 2");

e.Item.Controls.Add(list);

break;
case 2:
// Add a dropdown list
break;
case 3:
// Add a checkbox list
break;
default:
//
break;
}
}
}

One thing to note - I bound my ListView directly to a List<int>. So the code you'll need will be slightly different - have a look at this. The general idea is to cast e.Item.DataItem to whatever type the ListView is bound to and then get the value you need from that.

Now getting the values of the dynamically added controls on postback (assuming you need to) is going to be tricky. You'll have to loop through the controls on the page using Page.FindControl() or possibly even inspect the ListView and call FindControl on it's DataRows. You may need to use reflection to find each control's type. Like I said, it won't be straightforward, but you should be able to accomplish it.

How to set the visibility of a control in ASP.NET inside the DataList control?

You can use Item ItemDataBound event of DataList

protected void DatalistID_ItemDataBound(object sender, DataListItemEventArgs e) 
{
HiddenField hfStatusID= e.Item.FindControl("hfStatusID") as HiddenField;
ImageButton ReceiveButton= e.Item.FindControl("ReceiveButton") as ImageButton;
if (hfStatusID!= null && ReceiveButton!=null)
{
if (hfStatusID.Value == "123") // As per your Requirement
{
ReceiveButton.Visible= false;
}
}
}

And Take a HiddenField on .aspx page as:

<asp:HiddenField ID="hfStatusID" runat="server" Value='<%#Eval("StatusID")%>'/>

How to make asp.net control conditionally display based on user role?

You are using a server call on a server property. This won't work , normally you use <%# %> tags to set html or javascript data, not server data.

You can either set the Style attribute (which is a html property) or you could set the visiblity on page_load like this :

userOptionsPan.Visible = user.IsInRole("User");

Proper way to handle a conditional PlaceHolder in ASP.net

Set the visibility of the placeholder server side (i.e in your code behind)

example:

this.placeholdername.Visible = true;

if ( myObject == null )
{
this.placeholdername.Visible = false;
}

how to conditionally display controls in razor view?

Can you please try this

Your view

@model Models.EmployerModel
@{ ViewBag.Title = "Index"; }
<div>
<p class="validateTips">
Enter the new employment details.</p>
<fieldset>
<table>
<tr>
<td>
<label>
<b>Employment Type:</b></label>
</td>
<td>
@Html.DropDownList("EmploymentTypeDrp", new List<SelectListItem>

{
new SelectListItem{ Text="Admin", Value = "1" },
new SelectListItem{ Text="HR", Value = "0" }
}) // I just hard coded the departments here
</td>
</tr>
<tr>
<td>
<label>
<b>Job Title:</b></label>
</td>
<td>
@Html.TextBoxFor(m => m.JobTitle, new { Value = @Model.JobTitle })
</td>
</tr>
</table>
</fieldset>
</div>

In the script section of your view you can use .change() event as suggested by @Stephen Muecke

<script>
$(document).ready(function () {
$('#EmploymentTypeDrp').on('change', function (e) {

var selectValue = this.value;
if (selectValue == "Admin") {
$("#JobTitle").show();
}
else {
$("#JobTitle").hide();
}

});
});
</script>

ASP.NET Show Div Conditionally

<% if ( showDiv ) { %>
<div></div>
<% } %>

where showDiv would be a protected property in your code behind.



Related Topics



Leave a reply



Submit