asp.net datacontrol sum with Eval and other value - asp.net

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.

Related

How to access the current row's data in telerik's grid?

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.

dynamically populating div in asp.net

I am working on a weboage that will display questions and answers (maybe 5 at one time, maybe 7 at another time) returned from a database table. The questions will each be displayed in a div and the related answers displayed in another div. The question will have an icon "Show Answer / Hide Answer"
How can I go about creating a div and then populating it with values from a table?
Thanks
I would use repeater for that.
1.Create data source pulling data from your database
<asp:sqlDataSource Id="sqldsQuestionsAnswers" ... />
2.Create repeater linking to that data source:
<asp:repeater DataSourceId="sqldsQuestionsAnswers" runat="server">
<itemTemplate>
<div>
<%# Eval("question") %>
<hr/>
<%# Eval("answer") %>
</div>
</itemTemplate>
</asp:repeater>
The repeater will display anything whats in <itemTemplate> tag for every row returned by your query.
So if your query returns 2 questions like that:
Question-------------Answer
-----------------------------------
question1?----------answer1
question2?----------answer2
The output would be:
<div>
question1?
<hr/>
answer1
</div>
<div>
question2?
<hr/>
answer2
</div>
I hope it helps...
We need to know more about how you retrieve your question data and the context of the rest of your page, but you can do a few things here (roughly in order of preference):
Bind your data to an <asp:Repeater > control (or even one of the grid controls)
Build a custom or user control to render your questions and drop that on your page
Build your desired html as a string in code and set it to <asp:Panel > control (Panels render as div tags). If you want to be able to refresh your div without reloading the entire page (AJAX), you can use an <asp:UpdatePanel >.
Build your desired html in code and write directly to the response, either via <%= %> or <%: %> bee-stings or with the Response.Write() method.

<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.

Conversion from Classic ASP to ASP.NET

I have to convert some code from classic ASP to ASP.NET
1) How can I best handle syntax as below, where it seems to fail because the code is inside a tag, and also perhaps because the condition is split over several tags.
2) Any tools or guidelines that are good on this kind of code?
3) Classic ADO.
<li><a<% if "" = myFolder then %> class="selected"<% end if %> href="http://<%= SERVER_NAME %>/"><%= getLocale("Home") %></a></li>
<% SQL = "SP_TOPNAV '" & lang & "'"
Set maNav = conn.execute(SQL)
do while not maNav.EOF %>
<li><a<% if maNav(0) = myFolder then %> class="selected"<% end if %> href="http://<%= SERVER_NAME %>/<%= maNav(0) %>"><%= maNav(1) %></a></li>
<% maNav.MoveNext
loop
Set maNav = Nothing %>
</ul>
ASP.net handles code split between several <%%> tags just fine. The problem lies elsewhere. Please edit your question to include the error message.
If your using .net 2.0 look into a asp:repeater that can bind to a datasource, if your using .net 3.5 look into a asp:listview. Those controls give you the ability to iterate through data and generate markup which is essentially what your doing.
You can always use ASP.NET's data controls such as Repeater, GridView, DataList to display collections of item. And you can customize their looks by modifying the ItemTemplate. You can also include the rendering conditional inside ItemTemplate too.
For example:
<asp:Repeater id="Repeater1" runat="server">
<HeaderTemplate>
<table border="1">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td> <%# Container.DataItem %> </td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Taken from: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemtemplate.aspx
You can always insert conditional logic inside <%# ... %> text. Or if the logic are complicated, you can code them inside the code behind file and call them from here.
Search for "ASP.NET If inside ItemTemplate" or "ASP.NET Condition in ItemTemplate" for more info.

How to populate UpdatePanel in Repeater in ASPX (not code-behind)?

I have a Repeater which displays a list of items (in a grid-like/table view). When a user clicks an item, I display an UpdatePanel under this item with additional information relevant to the item (similar to Accordion control). I know how to populate the elements of the UpdatePanel in code-behind (I pass the ID of the selected element in the Repeater control as CommandArgument, get additional info for this ID, and set up the text fields of the active UpdatePanel controls). But I'm wondering if I could set up binding directly in the ASPX (instead of code-behind). When I used the <%= %> syntax to assign text fields of the UpdatePanel control the values of the page properties, e.g. <%= Comment %>, it sort of worked, but it changed the fields of all UpdatePanels in the repeater. Is there any way to bind the active UpdatePanel to the current values and leave already bound UpdatePanels unchanged?
Are you looking to display a container that displays additional information? Is there other activity in the "box" that requires it be an updatepanel?
<asp:repeater>
<itemtemplate>
<%# Eval("Name") %> <%# Eval("LastName") %><br />
<span onclick="$get('<%# Eval("Id") %>')">View Age</span>
<div id="<%# Eval("Id")%>" style="display:none;">
Age: <%# Eval("Age") %>
</div>
<itemtemplate>
</asp:repeater>
Ithink that's right, some syntax may be off a bit (typing without intellisense). Would that work?
I used ID as a unique identifier for the div id and the onclick command. You could also use jquery, asp:controls or whatever else you wanted.
Easiest way is to nest a FormView inside the update panel. Then the only thing you need to do in the code behind is get the additional info, assign it to the FormView.DataSource, and call FormView.DataBind(). Everything in the FormView will use the <%# Eval("SomeColumn") %> syntax. You'll probably need to use a FindControl() to get a reference to the FormView. I'd type up the code for you but I'll save you some headaches down the road and say DON'T DO THIS.
The update panel is about the most inefficient way to do any ajax stuff. The only way to get it all to wire up correctly with this repeater and server side code is to either have a gigantic viewstate or to rebind the repeater in your page load. You are turning a request that could be 300ms into something that will take over a second...or longer! Get familiar with a good ajax framework and don't be afraid to write real html. At the very least, use a webservice that loads a usercontrol with your markup.
I know the update panel is easy, and it's built in. It might even be adequate for what you are doing, but you must resist. You'll be glad you did.

Resources