Ben Williams
Ben Williams

Reputation: 131

Replace in ASP.net

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

Upvotes: 1

Views: 1895

Answers (3)

Wilfredo P
Wilfredo P

Reputation: 4076

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.

Upvotes: 1

pete
pete

Reputation: 25091

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

Upvotes: 0

nunespascal
nunespascal

Reputation: 17724

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

Upvotes: 0

Related Questions