How to access the current row's data in telerik's grid? - asp.net

I have a grid, and say I am in given telerik:GridTemplateColumn (or GridboundColumn?) I want to display the result of 2 other columns, how would I do this?
In asp.net repeater I could do:
<%# Container.DataItem("Col3") %> <%# Container.DataItem("Col4") %>
How would I do this in a telerik grid?

You should be able to use very similar syntax. Inside the GridTemplateColumn, you need to make an ItemTemplate. In that, you can put whatever markup you want to be displayed in the column. For example:
<telerik:GridTemplateColumn HeaderText="Col3 and 4" UniqueName="TemplateColumn">
<ItemTemplate>
<%# Container.DataItem("Col3") %> <%# Container.DataItem("Col4") %>
</ItemTemplate>
</telerik:GridTemplateColumn>
For more information, you can reference the documentation for the various column types: http://www.telerik.com/help/aspnet-ajax/grid-column-types.html (you'll need to scroll down a ways to get to the section on GridTemplateColumn.
Also, I'm assuming you are using Visual Basic? Otherwise, I think the syntax would be different.

Related

asp.net datacontrol sum with Eval and other value

Friends
In my Repeater control I want to sum these two value on Item Template,
<ItemTemplate>
<h4>
<%# Convert.ToDouble(Eval("price")) %>
(sum +)
<%=hiddenMinprice.Value%>
</h4>
</ItemTemplate>
can you please give me Idea about this?, I want to sum of Eval("price") and hidden Field value.I think it may simple and also we can use javascript but for practise please provide me tips and tricks for <%# and <%= in asp.net Data controls.

Render DateTime.Now directly in the ASPX page

I am trying to do the following directly into the aspx page but it is not showing the date value. I dont want to do it from the code behind. Am i missing something small here? Pls suggest.
<asp:Literal ID="ltrDate" Text='<% DateTime.Now.ToLongTimeString() %>' runat="server"></asp:Literal>
Even the use of hash in the expression <%#DateTime.Now.ToLongTimeString() %> does not work.
If you do not need to access the value of the Literal control from the code-behind, then there is no need to use it. Instead, you can just use the following expression in your page directly where you want to print the date:
<%= DateTime.Now.ToLongTimeString() %>
With server controls, you can only put either static text, databinding expressions <%# xx %>, or expression builders <%$ %> inside the property values in the page markup.
See this related question for more details about each approach.
When using a databinding expression such as <%# DateTime.Now.ToLongTimeString() %>, then you have to call Page.DataBind() (or ltrDate.DataBind() if that's the only databound control) from your code-behind (e.g. in Page_Load).

<script> tags inside an <asp:repeater>

I'm outputting a few lines of Javascript within a Repeater control on an ASPX page. I want to use a value from my DataSource inside the script tag.
A very basic example might be:
<asp:Repeater ID="RepeaterBlah" runat="server">
<ItemTemplate>
Hello <%# DataBinder.Eval(Container.DataItem, "SomeName")%>
<script>myfunction(<%# DataBinder.Eval(Container.DataItem, "SomeNumber")%>)</script>
</ItemTemplate>
</asp:Repeater>
I'm aware that most people won't repeat script tags like this, but I am using a small snippet of code from a third-party that you can place anywhere on a page to create a Flash object. You pass it a number so it knows which image gallery to display. No problems using several on one page.
To begin with, this worked fine, although I noticed the colours in Visual Web Developer indicated that it didn't really like the <%# being used inside a <script> tag. Intellisense was going a bit nuts in the code-behind too!
So what is the correct way to pass Dataset items into a script tag?
This perhaps? (Can't quite remember if the + signs should in fact be & signs though)
<%# "<script>myfunction(" + DataBinder.Eval(Container.DataItem, "SomeNumber") + ")</script>" %>
Another alternative syntax would be the following:
<%# Eval("SomeNumber", "<script>myfunction({0});</script>") %>
This uses the optional parameter where you can supply a format string.

Why does Visible='<%#false%>' work on a GridView but not label?

I am scratching my head over this, but have no idea what the problem is.
My actual code is
<asp:Label ID="Label1" runat="server" Text="abc"
Visible='<%#Request.QueryString["ListName"] == null %>' />
<asp:GridView ID="gvLists" runat="server"
Visible='<%#Request.QueryString["ListName"] == null %>' />
As you can see, i am trying to only make the visibility of the object be driven by the querystring. It works fine for the GridView, but doesn't work for a label. I also tried Panel and HyperLink with the same results.
I am sure I could get this working by putting my code in the code-behind, but it won't be as clean.
<%# %> works only on databound items.
you need to change it to <%= %> (Notice the "=")
<%= is uses to print to the page directly and the <%# is used for data binding elements. Here is a great explanation of all the inline code directives.
Thanks to Alison for pointing me in the right direction.
I needed to add Page.DataBind() to my Page_Load event in order for the expression to be evaluated.

ASP.net Inline Expression Issue

I can't seem to figure out why this does not work below. I need to bind the text box to a value from an inline expression. Seems like a simple thing right? But neither of these work. Any ideas? Thanks in advance.
<asp:textbox id="tbName" runat="server" Text='<%# Eval("test") %>' />
<asp:textbox id="tbName" runat="server" Text='<%= "test" %>' />
Edit:
I should mention that this page has no code behind and only the following directives at the top.
<%# Import Namespace="System" %>
<%# Import Namespace="System.Web" %>
<%# Page Language="C#" %>
Edit:
The only workable solution that I could come up with short of adding a code behind is adding an inline server script, like this one. I wish I knew why the inline expressions won't work unless you're in a data binding context.
<script language="C#" runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
tbName.Text = "test";
}
</script>
In the Page_Load of you will have to make a call to Page.DataBind() for
<asp:textbox id="tbName" runat="server" Text='<%# Eval("test") %>' />
to work.
<%= %> is a shortened response.Write() and is never valid as an attribute, for any server tag.
<%# %> can be used, only if the conatainer is databound (the page in your case).
<%$ %> can be used to access data in resources files.
EDIT: You can also take a look at How to 'bind' Text property of a label in markup which is a smimilar question.
As stated, <%= %> is illegal anywhere within a server control declaration, except where inner markup is being parsed as content (eg <ItemTemplate> within a Repeater).
<%# %> is valid as an expression for control properties, as these expressions will be evaluated when DataBind() is called on the control.
Your use of Eval() looks a little suspect though. Per the example, Eval() will use the current Page object as the binding context, which means that the value of the public property named "test" will be bound to when DataBind() is called. Unless you actually have this property defined on the Page class, the expression will never evaluate to anything.
Eval() is mainly intended for use in expressions within controls such as Repeater, GridView, ListView, etc, where there is a list of data items being bound using templates, and you need a method to be able to access the properties of the current data item.
For all other controls, just use normal code expressions inside a data-binding expression - it's much faster, and more intuitive than Eval(), which relies on runtime reflection.
If you want a more clever alternative using <%$ %> syntax that avoids data-binding altogether, go here:
http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx
Use <asp:textbox id="tbName" runat="server" Text='<%# Eval("test") %>' />
and set tbName.DataBind(); in page load event.
For those who are searching for more info about inline expressions, refer to the following links.
ASP.net have the following inline expressions
Embedded code blocks <% ... %>
Displaying expression <%= ... %>
Directive expression <%# ... %>
Data-binding expression <%# ... %>
Expression builder <%$ ... %>
Server-side comments block <%-- ... --%>
You might need the namespace for the textbox control
<%# Import "System.Web.UI.WebControls" %>
Try adding runat="server" to the server elements.
Otherwise, this element will not be processed at server.
EDIT: Actually, "its correct" that this doesn't work; code <%=...%> cannot be evaluated in a server tag, only expressions like for instance <%$ Resources: h1 %>
<asp:textbox id="tbName" runat="server"><%="test"%></asp:textbox>

Resources