Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i'm new to asp.net and i'm struggling with the replace function that i'm hoping someone can help with. When i use some test text it works fine (as in the example below) but as soon as i replace the test text with the value from the database (Eval("PContent")) i get a databinding error. The label separately works fine.

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

I've tried al-sorts but i cannot get around this.

Here's my code:

<asp:Label runat="server" ID="Label4" text='<%# Eval("PContent") %>' /> 
<%
Dim text1 As String = "Some text here [q]testing[/q]"
Dim output As String = text1.Replace("[q]", "<span class='quote'>")
Dim VS As String = output.Replace("[/q]", "</span>")
Response.Write(VS)
%>

Thanks for your time - sorry if this is a very n00b thing to ask! I did try search for an answer on here and google but i can't find anything...

**Update....

<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:Label runat="server" ID="Label5" text='<%# Eval("PMonthName")%>' />
<asp:Label runat="server" ID="Label6" text='<%# Eval("PDay")%>' /></small>
</div><!--middlebartext -->

<div class="middlebartexttitle"><a href="/Details.aspx?ID=<%# Eval("BID")%>">
<asp:Label runat="server" ID="Label3" text='<%# Eval("Header")%>' /></a><br />

 <asp:Label runat="server" ID="Label4" text='<%# Eval("PContent")%>' />
 <a href="/Details.aspx?ID=<%# Eval("BID")%>">Permalink</a>
 <div class="ruler"></div>
 </ItemTemplate>
 </asp:ListView> 



 <asp:SqlDataSource 
      ConnectionString="<%$ ConnectionStrings:Conn2 %>"
      ID="SqlDataSource1" runat="server" 
      SelectCommand="SELECT * from tablename where Deleted = 'False' Order By  DateAdded DESC"
      onselected="SqlDataSource1_Selected">
     </asp:SqlDataSource>

I've cut a chunk of code out so it's not as long :)

share|improve this question
    
What is PContent? A variable? A property? How and where is it defined? –  pete Jun 7 '13 at 10:44
    
PContent is a cell name from my database. I've used a listview with an attached sqldatasource so i've not actually defined it anywhere i don't think... –  Ben Williams Jun 7 '13 at 10:47
    
Is Label4 in a ListView ItemTemplate (or similar template)? –  pete Jun 7 '13 at 10:54
    
Yes it is, in a listview template. <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1"> –  Ben Williams Jun 7 '13 at 10:57
    
Can you post your ListView markup and the select query from SqlDataSource1? –  pete Jun 7 '13 at 11:02

3 Answers 3

up vote 0 down vote accepted

It's another way to do the replace more short:

C#

<%# ((string)Eval("PContent")).Replace("[/q]", "</span>") %>

VB.net

<%# (Eval("PContent").ToString().Replace("[/q]", "</span>") %>

I don't know a lot Vb.net but I think the code above works.

I hope that help you.

share|improve this answer
    
Hi! I've just tried this but i'm getting a different error - is that coded in C# as i'm working in VB? :-) –  Ben Williams Jun 7 '13 at 11:00
    
Hello! Thanks for this - it's nearly working.... one thing though, how do i add multiple replace statements? For instance this currently works for [/q] but how do i add a [q] as well? I think in classic asp it could be done with multiple "a","b","c","a2","b2","c2" or something like that? –  Ben Williams Jun 7 '13 at 11:39
1  
Oo! Answered my own question! <asp:Label runat="server" ID="Label4" text='<%# (Eval("PContent").ToString().Replace("[/q]", "</span>").Replace("[q]", "<span>"))%>' /> Question answered! Thanks all for the help! –  Ben Williams Jun 7 '13 at 11:52
    
You are welcome, I am not answered later becouse I was Busy. –  Wilfredo P Jun 7 '13 at 14:04

I don't see PContent defined in your question, but

it would be simpler to do something like,

Label4.Text = [value from db] 

You could set the text after you have fetched the records from database

share|improve this answer
    
Thanks for the reply... but isn't <asp:Label runat="server" ID="Label4" text='<%# Eval("PContent")%>' /> the same thing? –  Ben Williams Jun 7 '13 at 10:59
    
No, its not. <%# is a data binding expression. If you had a property in your class by the name PContent then you could use this. And even then, it would work only after you called Page.DataBind(). Its meant for use in databound controls. and the value comes after you call DataBind on the control –  nunespascal Jun 7 '13 at 11:10
    
Oooh, so this could be simply written as Label4.Text = PContent for example? (PContent being a DB cell). THen set the text in the code behind? –  Ben Williams Jun 7 '13 at 11:40

Try changing this:

<div class="middlebartexttitle"><a href="/Details.aspx?ID=<%# Eval("BID")%>">
<asp:Label runat="server" ID="Label3" text='<%# Eval("Header")%>' /></a><br />

<asp:Label runat="server" ID="Label4" text='<%# Eval("PContent")%>' />
<a href="/Details.aspx?ID=<%# Eval("BID")%>">Permalink</a>

To:

<div class="middlebartexttitle"><a href='/Details.aspx?ID=<%# Eval("BID")%>'>
<asp:Label runat="server" ID="Label3" text='<%# Eval("Header")%>' /></a><br />

<asp:Label runat="server" ID="Label4" text='<%# Eval("PContent")%>' />
<a href='/Details.aspx?ID=<%# Eval("BID")%>'>Permalink</a>

Since Eval requires quotes for the field it's evaluating, my guess is that the quotes you have defining the href attributes are throwing it off. Change those to single quotes (like you have everywhere else) and see if that works.

Also, you can learn more about inline expressions (and when to use them) at http://support.microsoft.com/kb/976112

share|improve this answer
    
cool - thanks, i'll give that a shot as well :) –  Ben Williams Jun 7 '13 at 12:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.