Binding a control property to an expression - asp.net

With ASP.NET WebForms, is it possible to bind a control's property to an expression?
For example
Visible="<% myProp.Value == otherProp.Value %>"
I realize I can do this in the code-behind, but I'm trying to keep UI display details confined to the markup as much as possible.

Yes, and this is the most efficient way to do ASP.Net WebForms development.
You want to call DataBind() to make the <%# myProp.Value == otherProp.Value %> expression to be evaluated. I prefer calling DataBind() in Page_PreRender
Example:
protected void Page_PreRender(object sender, EventArgs e)
{
DataBind();
}
<asp:PlaceHolder runat="server" Visible='<%# myProp.Value == otherProp.Value %>'>
Hello
</asp:PlaceHolder>
In fact, you can put your databinding logic either in your base class for your page, or in a <script runat="server"> block.
Complete page example:
<%# Page Language="C#" %>
<script runat="server">
void Page_PreRender(object sender, EventArgs e)
{
DataBind();
}
</script>
<html>
<body>
<asp:Placeholder runat="server" Visible='<%# DateTime.Now.Seconds % 2 == 1 %>'>
It is now an odd second
</asp:Placeholder>
</body>
</html>

Related

can't access a property using <%# %>

i was trying to hide and show a button using following code
where AllowUpdate is a property of page.
now problem is This statement never get Executed.
I have used similar code on other pages but it is unreliable many times it just fails and hides buttons even if they must not be
<asp:Button runat="server" ValidationGroup="param" Text='<%$ Resources:Resources, Save%>' ID="btnsave" CssClass="btn btn-primary btn_round" OnClick="btnsave_Click" Visible="<%# AllowUpdate %>" />
If you're going to use the <%# %> syntax, you must call data bind.
<asp:Panel runat="server" ID="Panel1">
<%# SomeProperty %>
</asp:Panel>
Code behind:
Panel1.DataBind();
Alternatively, use the <%= %> syntax.
<%= SomeProperty %>
or as Cal279 points out in the comments, you can set it in your code behind on some event, such as Page_Load.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
Butbtnsaveon1.Visible = AllowUpdate;
}
}

Set visible of a label from code behind

In asp.net I have this label:
<asp:Label ID="Label3" runat="server" Text="0" visible='<%# visibleCredits() %>'></asp:Label>
In code behind I have:
protected bool visibleCredits()
{
return false;
}
But the label is always shown, it should be invisible I think. Please don't ask why I did not set:
Label3.Visible = visibleCredits();
from the code behind.
Add this to your page:
protected void Page_Load(object sender, EventArgs e)
{
DataBind();
}
It will bind your page to the server control and allow you to use data bindings like this.
As Vache suggeseted, you need to call DataBind() since you're using the data binding syntax <%# visibleCredits() %>. Alternatively, you can also use <%= visibleCredits() %> and not need to call DataBind().

Call method in ListView EmptyDataTemplate

I have a simple ListView with an EmptyDataTemplate. In the EmptyDataTemplate, there is a LinkButton whose Visble property value is an expression that calls a method in my code behind. The problem is that the LinkButton is always visible regardless of whether the method returns true or false (my method isn't being called as I even set a breakpoint on it). Anyone come across this? What's happening here?
e.g.
<asp:ListView ID="peopleListView" runat="server" ...>
...
<EmptyDataTemplate>
Sorry, no people to view.<br />
<asp:LinkButton ID="newButton" runat="server" Visible='<%# EditPermitted() %>'>New Record</asp:LinkButton>
</EmptyDataTemplate>
</asp:ListView>
In the code behind, I have the method:
protected bool EditPermitted()
{
return false;
}
I don't think you can put scriplets <% %> inside of server controls.
You need to grab the RowDataBound event, and set the link button's visibility there
void gridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.EmptyDataRow) {
LinkButton lb = e.Row.FindControl("newButton");
lb.Visible = EditPErmitted();
}
}
You can't use code blocks like <% ... %> or <%= ... %> inside the attributes of server controls. Only data binding blocks like <%# ... %>. But you can use code blocks inside your EmptyDataTemplate, in this case a simple if statement should work:
<EmptyDataTemplate>
<% if(EditPermitted()) { %>
<asp:LinkButton ID="newButton" runat="server" ... />
<% } %>
</EmptyDataTemplate>

Displaying collection in Repeater control

I'm beginner in Asp.Net and Im trying to display "gc" on reapet control.
Here's the code-behind:
public partial class _Default : System.Web.UI.Page
{
List<GlassesCollection> gc= BL.Example.GetCategory() ;
protected void Page_Load(object sender, EventArgs e)
{
rpt1.DataSource = gc;
rpt1.DataBind();
}
protected void rpt1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
}
Im using the following ASP code:
<asp:Repeater ID="rpt1" runat="server" onitemdatabound="rpt1_ItemDataBound">
<ItemTemplate>
<%# Eval("gc") %>
</ItemTemplate>
</asp:Repeater>
But in run time i get this Exception:
Exception Details: System.Web.HttpException: DataBinding: 'ISeeOptic.DataType.GlassesCollection' does not contain a property with the name 'gc'.
Why i get this Excption and idea how to solve this?
Thank you in advance!
Try eval the specific property rather than the object
<asp:Repeater ID="rpt1" runat="server" onitemdatabound="rpt1_ItemDataBound">
<ItemTemplate>
<%# Eval("gcProperty") %>
</ItemTemplate>
</asp:Repeater>
The SO Question gives more details about binding a repeater to a generic list.
You can generally take advantage of DataBinder class which has the most power and flexibility the same as code listing here...
<asp:Repeater ID="rpt1" runat="server" onitemdatabound="rpt1_ItemDataBound">
<ItemTemplate>
<%# DataBinder.Eval("gcProperty") %>
</ItemTemplate>
</asp:Repeater>

How to use ASP.NET <%= tags in server control attributes?

This works:
<span value="<%= this.Text %>" />
This doesn't work:
<asp:Label Text="<%= this.Text %>" runat="server" />
Why is that?
How can I make the second case work properly, i.e., set the label's text to the value of the "Text" variable?
Use Data binding expressions
<asp:Label ID="Label1" runat="server" Text="<%# DateTime.Now %>" ></asp:Label>
Code behind,
protected void Page_Load(object sender, EventArgs e){
DataBind();
}
you can do this
<asp:Label ID="Label1" runat="server" ><%= variable%></asp:Label>
You will need to set the value of the server control in code
First of all, assign an ID to the label control so you can access the control
<asp:Label ID="myLabel" runat="server" />
Then, in your Page_Load function, set the value of your labels 'Text' field
protected void Page_Load(object sender, EventArgs e)
{
myLabel.Text = 'Whatever you want the label to display';
}
This function will be in your code behind file, or, if you are not using the code behind model, inside your aspx page you will need
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
myLabel.Text = 'Whatever you want the label to display';
}
</script>
Good luck.
In my code i am using something like this easily but in the databound control like ListView Item template
<asp:HyperLink ID="EditAction" class="actionLinks" Visible='<%#Eval("IsTrue").ToString() != "True"%>' runat="server" NavigateUrl='<%# Eval("ContentId","/articles/edit.aspx?articleid={0}")%>' />
But when i tried to use outside the databound control using <%# .. %>, it simply doesn't work.
You can easily do with
My href
But for server controls, and outside of databound control. We need to call DataBind() in pageload event explicitly
<asp:Hyperlink ID="aa" NavigateUrl='<%#myHref%>' >
Not sure how to mark this as such, but this is a bit of a duplicate. See this thread.
I don't think embedding code in to your markup will really make your markup any clearer or more elegant.
<asp:Label> is compiling at runtime and converting to html tags. You can set text with codebehind or like this:
<asp:Label id="Text1" runat="server" />
<% Text1.Text = this.Text;%>
UPD: Seems like my variant doesnt work, this is better:
protected void Page_Load(object sender,EventArgs e)
{
Text1.Text = this.Text;
}
Just pitching this little nugget in for those who want a good technical breakdown of the issue -- https://blogs.msdn.microsoft.com/dancre/2007/02/13/the-difference-between-and-in-asp-net/
I think the crux is in pretty decent agreement with the other answers:
The <%= expressions are evaluated at render time
The <%# expressions are evaluated at DataBind() time and are not evaluated at all if DataBind() is not called.
<%# expressions can be used as properties in server-side controls. <%= expressions cannot.

Resources