adding logic to datagrid item template - c#

how would you go about adding logic to a datagrid item template? In my datagrid, i want to add a logic to it. that is, if the result for the data equals to "Yes", an "asp:label" control will be displayed; otherwise a "asp:imagebutton" control will be shown
<ItemTemplate1>
<% if DataBinder.Eval(Container.DataItem, "boflag").equals("Yes") then%>
<asp:Label id="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"boflag")%>'></asp:Label>
<% Else %>
<asp:imagebutton id="imgBtnUpdate" runat="server" NAME="Imagebutton3"
ImageUrl="no.gif"></asp:imagebutton>
<% end if %>
</ItemTemplate>
However, "<% if DataBinder.Eval(Container.DataItem,
"boflag").equals("Yes") then%> " this is not valid.
So, how can i get the data to compare the value.
Thank you

You should implement the items Data Bound Event in the code behind. Then show/hide/populate the controls there.

One other option you can do is to use a ternary operator to evaluate the boflag field and output accordingly. For example:
<%# DataBinder.Eval(Container.DataItem, "boflag").equals("Yes") ? DataBinder.Eval(Container.DataItem,"boflag") : "<input type=\"image\" src=\"\" />" %>
I'm not sure that you could add server controls through this method, but you could certainly add conditional HTML.

Related

Complex binding expression with IF on asp.net

I have a repeater with a column that I needs to have the following expression, but not sure of the syntax.
If value="DispForm.aspx" then
Show "No Document"
Else
Show the real value.
I tried to use all expresion in one line, but not sure what am I missing here
<a href=" <%# DataBinder.Eval(Container.DataItem, "Path") %>">
<%#
if (DataBinder.Eval(Container.DataItem, "FileName") == "DispForm.aspx")
{
"No document";
}
else
{
DataBinder.Eval(Container.DataItem, "FileName");}%>
</a>
Error :
http://screencast.com/t/ZERZjzZxST
It would be much simpler to use the ItemDataBound event, add an id and runat="server" to the anchor element, then you can use e.Item.FindControl("anchorname") to get the htmlanchor element. You can then set it's text, href, visibility etc. in codebehind far easier than trying to get some of the more difficult databinding logic to work effectively.
I solved it my self like this: item databound events will be fired on the server side for each row, and this report returns 2000 rows. Not a good thing.
<a href="<%# DataBinder.Eval(Container.DataItem, "fileNameUrl") %>">
<%# DataBinder.Eval(Container.DataItem, "FileName").ToString() == "DispForm.aspx" ? "No document" : DataBinder.Eval(Container.DataItem, "FileName").ToString() %>
</a>

ASP.NET How to know if control in page if-else is visible?

Here is an example. The problem is that the Label2 has always Visible=true, regardless of the if-else result. How can i know if its visible or not?
<asp:GridView runat="server" ID="gdv">
<asp:TemplateField>
<ItemTemplate>
<%if (!IsItem)
{%>
<asp:TextBox runat="server" Text='<%# Eval("Qtde") %>'></asp:TextBox>
<%}
else
{ %>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Qtde") %>'></asp:Label>
<%} %>
</EditItemTemplate>
</asp:TemplateField>
I cannot access IsItem, since the class where i want to check the label for visibility is not the page code-behind. Also these controls are in a gridview.
The class method that checks for visibility is not called directly by the page, its a private method of a server control that extends a grid view and is supposed to work with a varierty of cases.
Thanks.
EDIT------------
I have found an alternative, decent solution. Still, i'm curious to know if it's possible to do the aforementioned. Thanks you for your time,
You are displaying that Label based on a variable called "IsItem." Why can't you use that to determine the Label's visibility?

ASP.net putting a dataitem in a repeater

I've got a:
Data repeater
Control in the repeater
And I'd like to put some values into the controls. At the moment I have a control in it as:
<asp:Repeater runat="server" ID="ArchiveEntryRepeater">
snip
<asp:HyperLink ID="HyperLink1" ToolTip="Comment on Some Entry Title Here" runat="server" NavigateUrl="~/Blog/7/How-to-know-when-this/Comments">
<strong><%# DataBinder.Eval(Container.DataItem, "Comments")%></strong> Comments
</asp:HyperLink>
snip
</asp:Repeater>
Which works fine, but I want to put
<%# DataBinder.Eval(Container.DataItem, "Title")%>
Into the NavigateURL property of the Hyperlink. Just putting it in doesn't seem to work!
Try enclosing your NavigateURL in apostrophes instead of double quotes to not confuse the ASP.NET parser:
<asp:HyperLink runat="server" NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "Title")%>' [...]>[...]</asp:HyperLink>
This might be your issue.
You can always bind on the back end using the event itemdatabound. It has some advantages especially where the data is not a neat view or needs some type of major manipulation.
Another option would be to loose the control and replace it with an html anchor control like this:
<asp:Repeater>
<ItemTemplate>
<a id="HyperLink1" title="Comment on Some Entry Title Here" href='<%# DataBinder.Eval(Container.DataItem, "Title")%>'
style="font-weight: bold">'<%# DataBinder.Eval(Container.DataItem, "Comments")%>'</a>
</ItemTemplate>
</asp:Repeater>
Once the server renders a hyperlink to the client there is generally no reason to have it "runat='server'" . The purpose is to navigate to another page so the overhead of using an asp:HyperLink vs. an nchor is wasted.

How to have an ASP checkbox for each entry in a list?

I want to do something like this in my asp code:
<%
foreach Record record in listOfRecords
{
%>
<asp:checkbox runat="server" id="employeeIdNumber" />
<p>Employee's Name: <%= record.name %> </p>
<p>Employee's Phone Number: <%= record.phoneNumber %></p>
<%
}
%>
The problem is that the checkbox id is a string literal. How can I give a unique id to each employee's checkbox?
Wrap this in a repeater. Then bind your listOfRecords to the repeater.
<asp:Repeater runat="server">
<ItemTemplate>
<asp:checkbox runat="server" id="employeeIdNumber" />
<p>Employee's Name: <%# Eval("name") %> </p>
<p>Employee's Phone Number: <%# Eval("phoneNumber") %></p>
</ItemTemplate>
</asp:Repeater>
Then to get it out, you run through the RepeaterItems collection and look for the checkboxes by RepeaterItem.FindControl("employeeIdNumber") to determine if they are checked.
I think you should use Repeater instead.
<asp:Repeater ID="rptEmployees" runat="server">
<ItemTemplate>
<asp:CheckBox ID="employeeIDNumber" runat="server" />
</ItemTemplate>
</asp:Repeater>
This Control will render unique ID for each checkbox for you.
CheckboxList is an option too.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkboxlist.aspx
This also has an example on how to check the checked state - just iterate through and check the Selected property.

Why can't I set the asp:Label Text property by calling a method in the aspx file?

Can somebody please explain this to me:
I have a label and I want to be able to set the Text property by calling a method in the aspx file. It works fine if I set the property in code behind, but I really want to set this property in the aspx file.
I have tried a couple of things, but what I expected to work was this:
<asp:Label ID="Label1" runat="server" Text=<%# GetMyText("LabelText") %> />
I get no errors when doing this, but my method is never called and the Text property is left empty.
Is it not possible to set property values to server side controls directly in the aspx without using resources or use hard coded values?
Update: My first try was:
<asp:Label ID="Label1" runat="server" Text=<%= GetMyText("LabelText") %> />
But that results in the following error:
Server tags cannot contain <% ... %> constructs.
The syntax =<%# ... %> is Data binding syntax used to bind values to control properties when the DataBind method is called.
You need to call DataBind - either Page.DataBind to bind all the controls on your page, or Label1.DataBind() to bind just the label. E.g. add the following to your Page_Load event handler:
if (!IsPostBack)
{
this.DataBind();
// ... or Label1.DataBind() if you only want to databind the label
}
Using Text='<%= GetMyText("LabelText") %>' as others have proposed won't work as you'll find out. This syntax is inherited from classic ASP. It can be used in some circumstances in ASP.NET for inserting dynamic values in static HTML, but can not be used for setting propeties of server controls.
The sysntax you are looking for is <%= %> the # is for data binding. So your code should read:
<asp:Label ID="Label1" runat="server" Text='<%= GetMyText("LabelText") %>' />
EDIT: This answere is incrrect
I am leaving this answer here because lots of people agreed with me that this is infact the correct answer, but it will not work. This line of code will produce the following HTML output:
<span id="Label1"><%= GetMyText("LabelText") %></span>
Try this:
<asp:Label ID="Label1" runat="server" Text='<%= GetMyText("LabelText") %>' />
Edit
Yep. I was wrong. #Joe was right.
However, THIS works (and I'm not sure what the difference is):
<asp:Label ID="Label1" runat="server"><%= GetMyText("LabelText") %></asp:Label>
CodeBehind:
protected string GetMyText(string input)
{
return "Hello " + HttpUtility.HtmlEncode(input);
}

Categories

Resources