ASP.NET FormView: Including a Member Variable in an XPath Expression - asp.net

Is something like this possible?
<asp:FormView ID="myFormView" runat="server">
<ItemTemplate>
<p><%# XPath("Root/Path/Item[#id=<%=this.m_myId%>]/HelloWorld")%></p>
</ItemTemplate>
</asp:FormView>
m_myId is a member variable on the page. I can't seem to get it to work without a parsing error.

You can't have nested asp tags. Try something like this:
<%# XPath("Root/Path/Item[#id= " + this.m_myId + "]/HelloWorld") %>

Related

WHy am I unable to add text along with <%# Container.DataItem %> in repeater in user control

I have a User Control which is dynamically placed by CodeBehind as follows:
Dim myControl As Control = CType(Page.LoadControl("~/Controls/mainMenu.ascx"), Control)
If InStr(Request.ServerVariables("url"), "/Login.aspx") <= 0 Then
mainMenu.Controls.Add(myControl)
End If
As per an example from my previous question on here.
Within this Control is a repeater which calls a database to generate values.
My Repeater mark-up is as follows
<asp:Repeater runat="server" ID="locationRepeater" OnItemDataBound="getQuestionCount">
<ItemTemplate>
<p id='locationQuestions' title='<%# Container.DataItem %>' runat='server'></p>
</ItemTemplate>
</asp:Repeater>
The example above works fine, but I want to be able to prepend text to <%# Container.DataItem %> in the title attribute of that <p> to print to the browser like this is some text DATA_ITEM_OUTPUT
When I try to do that though, it prints this is some text <%# Container.DataItem %> exactly like that, ie, turning <%# Container.DataItem %> into text, NOT the value from the repeater code.
It was working fine before I made it into a dynamically inserted control, so I am thinking I might have something being generated in the wrong order, but given that it works without any prepended text, I am stumped to fix it!
I'm new to .net and using vb.net, please could someone point me in the right direction?
It would help to see the broken version of your code, but I gather you're doing this:
<p id='locationQuestions' title='This is some text <%# Container.DataItem %>' runat='server'></p>
If that's the case, try it like this:
<p id='locationQuestions' title='<%# "This is some text " & Container.DataItem %>' runat='server'></p>
Following Statement include the server side code in your page code.
<%# Container.DataItem %>
When you Server side code on the page with some manipulation then put
it in single quote and code as you do code behind.
E.g.
Text = '<%# "Employee Name:" & Eval("EmpName") %>'
so you title markup code should be like this:
title='<%# "This is Title " & DataBinder.Eval(Container.DataItem, "Name") & "Text." %>'
Best of all, the Databinder.Eval syntax is the same for VB and C#.
check this link for more details.

using an if/then in a repeater in an asp.net page

I haven't used asp.net webforms in what seems like forever, and I am having the toughest time figuring out how to get a simple if/then statement to work. This is what I want to do:
<asp:Repeater ID="rpt" runat="server" DataSourceID="lds">
<ItemTemplate>
<% if(Eval("show")) { %> show something <% } %>
</ItemTemplate>
</asp:repeater>
But obviously that gives me an error - how do I do this? Thank you - I have completely gone to MVC now and I can't seem to remember this stuff.
You are missing an ItemTemplate
<asp:Repeater ID="rpt" runat="server" DataSourceID="lds">
<ItemTemplate>
<%# Eval("show") ? Eval("Whatever") : Eval("Whatever") %>
</ItemTemplate>
</asp:repeater>
Try the conditional operator
<%= Convert.ToBoolean(Eval("show")) ? "something" : "" %>
If you just want to show a simple string or something, you can do this:
<%# bool.Parse(Eval("show")) ? "show something" : null %>
What exactly are you trying to show/not show?

asp.net repeater with usercontrol - setting commandargument

I have the following repeater code:
<asp:Repeater ID="repMain" runat="server" OnItemCommand="repMain_ItemCommand" EnableViewState="false">
<ItemTemplate>
<dmg:testcontrol runat="server" MyData=<%#Container.DataItem %>>
</dmg:testcontrol>
</ItemTemplate>
</asp:Repeater>
The testcontrol usercontrol looks like:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="TestControl.ascx.cs" Inherits="TestRepeater.TestControl" %>
<asp:Literal runat="server" ID="litMain" Text="<%#MyData.MyValue %>"></asp:Literal>
<asp:DropDownList runat="server" ID="dropdownMain"></asp:DropDownList>
<asp:Button runat="server" ID="btnMain" Text="Click Me" CommandName="Update" CommandArgument="<%#dropdownMain.SelectedValue%>"/>
Is it possible for me to send through the dropdownMain.SelectedValue as the CommandArgument?
Just now it is an empty string.
Thanks
Duncan
PS This is related to ASP.NET Repeater not binding after ItemCommand but I thought the two sufficiently different to keep apart.
A bit old question but I just my self found the answer to this one.
CommandArgument="<%#dropdownMain.SelectedValue%>"
Needs to look like this instead with single quotes! All inline codes within a asp.net controls have to be done this way instead.
CommandArgument='<%#dropdownMain.SelectedValue%>'
Why not get the selected value, and use it inside the Command function ?
(why to try to send it as argument, from the moment you can get it inside the command called function and its the same)

Using Eval inside a IF statement and Repeater

I am trying to use Eval inside a IF Statement and Repeater.
I want to do something like this:
<asp:Repeater runat="server" ID="rpRepeater">
<ItemTemplate>
<% if ((bool)Eval("A_Boolean"))
{ %>
blah...
<% } %>
</ItemTemplate>
</asp:Repeater>
This code gives me the following error:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Eval can be only used inside "binding" tag.
<%# Eval("A_Boolean") %>
http://support.microsoft.com/kb/307860#1a
It is possible to simulate if statements like this (code goes within ItemTemplate).
<asp:Panel runat="server" Visible='<%# Eval("A_Boolean") %>'>
blah...
</asp:Panel>

Unable to bind in asp.net grid Template Column

I am having trouble accessing the data field. I receive the error: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
I can get the value but using <%# getOpenJobs((string)Eval("ParentPart")) %> but I need to use it in the if to display a certian picture if it passes the condition. Is there a better way to do this or am i just missing something simple?
<telerik:GridTemplateColumn UniqueName="hasOpenJobs" HeaderText="">
<ItemTemplate>
<% if (getOpenJobs((string)Eval("ParentPart")) > 1)
{ %>
<img src="../images/job-icon.gif" alt="Open Jobs" />
<%} %>
</ItemTemplate>
</telerik:GridTemplateColumn>
In these cases, I usually create a method in the code-behind to send back the final generated HTML. E.g.
<ItemTemplate>
<%# GetJobImageHtml((string)Eval("ParentPart")) %>
</ItemTemplate>
Then do whatever logic you need in the GetJobImageHtml() method and return a HTML string.

Resources