Replace in ASP.net - 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")%>' />
Permalink
<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 :)

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.

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

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")%>' />
Permalink
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

Related

Servertag format wrong

I'm using a gridview where I try to set the visibility for 2 buttons and a label and also the text of the label via commands in the aspx file itself.
Now I've run into the problem that regardless if I use <%# or <%= or <% I always get "servertag format wrong".
Code:
<asp:TemplateField HeaderText="Status" SortExpression="Status">
<ItemTemplate>
<asp:Button runat="server" text="Freigeben" Visible="<%#!IsEnvelopeCleared((String)Eval("Status")) %>"/>
<asp:Button runat="server" text="Ablehnen" Visible="<%#!IsEnvelopeCleared((String)Eval("Status")) %>"/>
<asp:Label runat="server" text="<%# Bind("Status") %>" Visible="<%# IsEnvelopeCleared((String)Eval("Status")) %>"/>
The error appears on the first <%# already (I also tried removing the ! to no avail).
Overall double quote is the problem.
You have to use single quote. Following is one of the line from your code.
<asp:Button runat="server" text="Freigeben" Visible='<%#!IsEnvelopeCleared((String)Eval("Status")) %>'/>

ASP.Net GridView TemplateField Inserting HTML

I'm relatively new to ASP.Net so this may be a simple question. I'm using Visual Studio Express 2012 for Web and I have a GridView setup (which works fine) that I am trying to get HTML wrapped round one of the columns. My code at the moment:
<asp:TemplateField HeaderText="" SortExpression="teamviewer">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("teamviewer") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<a href="javascript:teamviewerclientconnect('999999999')">
<img src="Images/teamviewer_icon.png" /><asp:Label ID="Label1" runat="server" Text='<%# Bind("teamviewer") %>'></asp:Label>
</a>
</ItemTemplate>
</asp:TemplateField>
At the moment this puts my image in as the hyperlink and then outputs the SQL resulting field as text next to it. What I need to do is replace where I have put 999999999 with the actual SQL result. I don't want it to print the text at all, it should just be in the hyperlink. Thanks in advance.
Use a code-behind method to build the href string, like this:
protected string BuildHref(string clientId)
{
return "javascript:teamviewerclientconnect('" + clientId + "')";
}
<ItemTemplate>
<a href='<%# BuildHref(Eval("DATABASE_COLUMN_NAME_HERE")) %>'>
<img src="Images/teamviewer_icon.png" />
<asp:Label ID="Label1" runat="server"
Text='<%# Bind("teamviewer") %>'>
</asp:Label>
</a>
</ItemTemplate>

eval and anchor tag href in a text label property ASP.Net

I have been trying to get these running looking at lots of different samples but with no luck.
To me it seems it's ok but what am I missing here?
<asp:Label ID="Label1" runat="server" Text="<%# String.Format("<a href=http://localhost/reportserver/Pages/ReportViewer.aspx?/temp&rs:Command=Render&id={0}>link</a>", Eval("ID")) %>" Width="100px" visible="true"></asp:Label>
Thanks
"With no luck" is not a descriptive error.
<%# is for databinding expression only. So have you called Page.DataBind() or at least Label1.DataBind() in codebehind?
You could also try
Text='<%= String.Format("<a href=http://localhost/reportserver/Pages/ReportViewer.aspx?/temp&rs:Command=Render&id={0}>link</a>", Eval("ID")) %>'
Apart from that, why not doing such things in codebehind only, so you don't have issues like this?
<asp:Label ID="Label1" runat="server" Text='<%# String.Format("<a href=http://localhost/reportserver/Pages/ReportViewer.aspx?/temp&rs:Command=Render&id={0}>link</a>", Eval("ID")) %>' Width="100px" visible="true"></asp:Label>
you shouldn't use Text="something". you should use Text='something'

ASP.net databinding a hyperlink from Access DB fields

I am helping my daughter build an asp.net website that has an Access database. We are using a DataList with a couple fields inside the ItemTemplate. The one that is giving me trouble is the Hyperlink. The code is below. When the page loads the hyperlink renders like this:
<a id="ContentPlaceHolder1_DataList1_lnk_6" href="#http://www.washingtonfaire.com/#" target="_blank">Washington Midsummer Renaissance Faire</a>
But when I click on the link, it tries to navigate to "http://localhost:1852/BOOMPiratesB/Raids.aspx#http://www.washingtonfaire.com/#"
What are we doing wrong?
Here is our code.
<asp:DataList ID="DataList1" runat="server" DataSourceID="AccessDataSource1">
<ItemTemplate>
<asp:HyperLink id="lnk" runat="server" NavigateUrl='<%# Eval("Link") %>' Target="_blank" Text='<%# DataBinder.Eval(Container.DataItem, "VenueName")%>'>
</asp:HyperLink>
<br />
<asp:Label ID="DateTextLabel" runat="server" Text='<%# Eval("DateText") %>' />
<br />
<asp:Label ID="CityStateLabel" runat="server" Text='<%# Eval("CityState") %>' />
<br />
<br />
</ItemTemplate>
</asp:DataList>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/my.mdb"
SelectCommand="SELECT [VenueName], [Link], [DateText], [Season], [DateStart], [CityState] FROM [qpgRaid]">
</asp:AccessDataSource>
I cannot see where the extra # signs are coming from. They don't appear to be in the field in the table.
It's very puzzling. And insight would be most appreciated.
Have a look at this question
I guess you are using an access hyperlink column to store the link. If you convert it to a text column it should start working fine, or you may have to strip the '#'s manually.

Why <%= %> works in one situation but not in another

This is stemming from a bad answer I gave last night. The curiosity as to why one method works and not the other is bugging me and I'm hoping someone smarter than me can give me the proper explanation (or point me to the documentation) of why the following behavior is as it is.
Given the following code-behind:
protected string GetMyText(string input)
{
return "Hello " + HttpUtility.HtmlEncode(input);
}
Why does this work
<asp:Label ID="Label1" runat="server"><%= GetMyText("LabelText") %></asp:Label>
but this does not
<asp:Label ID="Label1" runat="server" Text='<%= GetMyText("LabelText") %>' />
Edit - added
At the risk of having my original dumb answer downvoted more times, here's the link to the original question, since some of the answers I'm getting now were already covered in that question.
Why can't I set the asp:Label Text property by calling a method in the aspx file?
Using <%= %> is equal to putting Response.Write("") in your page. When doing this:
<asp:Label ID="Label1" runat="server"><%= GetMyText("LabelText") %></asp:Label>
The ASP.NET processor evaluates the control, then when rendering, it outputs the control's contents & calls Response.Write where it sees <%=.
In this example:
<asp:Label ID="Label1" runat="server" Text='<%= GetMyText("LabelText") %>' />
You can't use Response.Write("") on a Text attribute because it does not return a string. It writes its output to the response buffer and returns void.
If you want to use the server tag syntax in ASP.NET markup you need to use <%# %>. This combination of markup data binds the value in the tags. To make this work, you'll then need to call DataBind() in your page's Load() method for it to work.
Because they are both server side instructions - the second piece of code is equivalent to:
<asp:Label ID="Label1" runat="server" Text='Response.Write(GetMyText("LabelText"))' />
<%= GetMyText("LabelText") %> basically means
Response.Write(GetMyText("LabelText"));
Here it is OK.
<%= GetMyText("LabelText") %>
However when you use this:
<asp:Label ID="Label1" runat="server" Text='<%= GetMyText("LabelText") %>' />
It basically means:
Label1.Text = Response.Write(GetMyText("LabelText"));
which is a wrong statement.
Wrong format:
<asp:Label ID="Label1" runat="server" Text='<%= GetMyText("LabelText") %>' />
Right format with using resources:
<asp:Label ID="Label1" runat="server" Text='<%$ Resources:Resource, MyText %' />
For it to work in the second case, you'd want it as follows:
<asp:Label ID="Label1" runat="server" Text="<%# GetMyText("LabelText") %>" />
And then Label1 will need to be databound.
Do this on server controls, if you have your LabelText in a Global Resource file:
<asp:Label ID="Label1" runat="server" Text="<%$ Resources: resourceName, LabelText %>" />

Resources