1

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 :)

6
  • What is PContent? A variable? A property? How and where is it defined? Commented Jun 7, 2013 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... Commented Jun 7, 2013 at 10:47
  • Is Label4 in a ListView ItemTemplate (or similar template)? Commented Jun 7, 2013 at 10:54
  • Yes it is, in a listview template. <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1"> Commented Jun 7, 2013 at 10:57
  • Can you post your ListView markup and the select query from SqlDataSource1? Commented Jun 7, 2013 at 11:02

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi! I've just tried this but i'm getting a different error - is that coded in C# as i'm working in VB? :-)
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?
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!
0

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

3 Comments

Thanks for the reply... but isn't <asp:Label runat="server" ID="Label4" text='<%# Eval("PContent")%>' /> the same thing?
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
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?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.