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.
Related
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>
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
I have two Formviews on the same page. I can use FindControl on the first without issue, but it always fails on the second one.
I am using the ItemTemplate for both, both default to ReadOnly, both are bound to SQLDataSources (different ones) but I cannot for the life of me work out why FindControl works for one and not the other.
I have removed a lot of the plain text from the code below.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:SoftSaleDBConnectionString %>"
SelectCommand="SELECT * FROM [Apps] WHERE ([AppID] = #AppID)" >
<SelectParameters>
<asp:FormParameter FormField="AppID" Name="AppID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:FormView ID="FormView1" runat="server" DataKeyNames="AppID"
DataSourceID="SqlDataSource1">
<ItemTemplate>
<h1>
<asp:Label ID="AppNameLabel" runat="server" Text='<%# Bind("AppName") %>' />
<asp:Label ID="VersionLabel" runat="server" Text='<%# Bind("Version") %>' /> for
<asp:Label ID="OSLabel" runat="server" Text='<%# Bind("OS") %>' />
</h1>
<p>Text here</p>
<p><asp:TextBox ID="LicenseTextBox" runat="server"
Text='<%# Eval("License")%>'
TextMode="MultiLine" Width="800px" Rows="25" ReadOnly="True"></asp:TextBox></p>
<asp:HiddenField ID="AppLocation" runat="server" ViewStateMode="Inherit" Value='<%# Bind("AppLocation") %>'/>
</ItemTemplate>
</asp:FormView>
<p><asp:Literal ID="SizeLiteral" runat="server"></asp:Literal></p>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:SoftSaleDBConnectionString %>"
SelectCommand="SELECT * FROM [Installations] WHERE (([AppID] = #AppID) AND ([Username] = #Username))" >
<SelectParameters>
<asp:FormParameter FormField="AppID" Name="AppID" Type="Int32" />
<asp:FormParameter FormField="Username" Name="Username" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:FormView ID="DLFormView" runat="server" DataSourceID="SqlDataSource4"
DataKeyNames="AppID,Username">
<ItemTemplate>
<p> <asp:Label ID="DLAppNameLabel" runat="server" />
<br />
<br />
<asp:Label ID="NumberOfInstallations" runat="server" Text='<%# Bind("Installations") %>'></asp:Label>
<br />
<asp:HiddenField ID="TotalInstallations" runat="server" />
Number of New Installations:
<asp:DropDownList ID="NumberOfNewInstallations" runat="server">
</asp:DropDownList>
<br />
<asp:Label ID="TotalNumberOfInstallations" runat="server" Text="Label"></asp:Label>
</p>
</ItemTemplate>
</asp:FormView>
And the FindControl coding is as follows...
TextBox LicTextBox = (TextBox)FormView1.FindControl("LicenseTextBox");
HiddenField AppLocCode = (HiddenField)FormView1.FindControl("AppLocation");
Label AppNameCode = (Label)FormView1.FindControl("AppNameLabel");
These always work...
Label DLAppNameCode = (Label)DLFormView.FindControl("DLAppNameLabel");
This always returns null. I've read a million bosts about the controls not being rendered until databinding has completed, but surely if the two FOrmViews are set up the same way, the result should be the same.
Any help would be much apreciated :)
Matt :)
I'd like to plead stupidity and but also provide some background to how I've used what I've learnt from my stupidity.
My SqlDataSource for DLFormView was returning records, of course I then used slightly different values by accident and so no records were returned. I only figured this out by making the ItemTemplate section of the form view only contain unbound (basically plain text) data. As nothing displayed, this pointed towards the page using a different tempalte. When I put a test line in the EmptyDataTemplate, it displayed, confirming there was nothing returned by the SqlDataSource. Perhaps a School boy error not putting something in this template during development.
As it happens I need to use the EmptyDataTemplate anyway, I just hadn't got to this point. As only one template is rendered at a time, I have been able to use the same ID names in both templates. This means there is no need to test for which template is being used. (although empty template is used when DataItemsCount = 0, otherwise you can test for CurrentMode for the remaining templates.)
So a big fail on my part, but I hope people can learn from my mistake.
Matt :)
Apologies for the newbie question. My client wishes me to make a small change to the gridview on his http://www.flogitdonegal.com/SearchPage.aspx page.
Note the way the Title column is a hyperlink to view more information. This comes from a 'BriefDescription' field in the database.
How can I add 250 chars from the 'FullDescription' underneath the Title in the same cell, but I dont want it be a hyperlink.
Essentially it will be 2 fields coming into the same column.
Thanks in advance for all help.
John
If this is using a GridView you are most likely using a TemplateField as it is to display the HyperLink.
Within the ItemTemplate of the TemplateField you can specify an additional Label underneath using something as follows:
<asp:Label runat="server" id="FullDescLabel" Text='<%# DataBinder.Eval(Container.DataItem, "FullDescription") %>' />
You need to use the TemplateField and here is a tutorial that explains some of the other fields that GridView offers as well.
<asp:GridView ID="gvwCompounds" runat="server" DataSourceID="objItemsFromYourDB">
<Columns>
....
<asp:TemplateField>
<ItemTemplate HeaderText="Title">
<asp:HyperLink runat="server" ID="Hperlink1" NavigateUrl='<%# Eval("BriefDescriptionUrl") %>' Text='<%# Eval("BriefDescription") %>' />
<br />
<asp:Label runat="server" ID="Label1" Text='<%# Eval("FullDescription") %>' />
</ItemTemplate>
</asp:TemplateField>
....
</Columns>
</asp:GridView>
I'm trying to separate my presentation and logic as much as I can, but there's one problem.
How can i set the text-property to a dynamic value from my design file? I want to do like this:
<asp:Textbox id="txtUrl" runat="server" Text="<%= user.URL %>" />
But this will fail. Am i forced to do this from code behind?
<asp:Textbox id="txtUrl" runat="server" Text="<%# user.URL %>" />
It's all about the #. but it won't get set till txtUrl.DataBind() or something higher in the object heirarchy (like the Page) calls DataBind().
How about this :
<input type="text"
id="txtUrl" name="txtUrl" runat="server"
value='<%= user.URL %>' />
You can use binding instead of evaluation.
This code binds a text box's Text property to a user's Url property returned by MyData.GetLoggedInUser(). This allows for 2-way binding.
<asp:FormView ID="UserView" runat="server" DataSourceID="LoggedInUser">
<ItemTemplate>
<asp:TextBox ID="tb"
runat="server"
Text='<%# Bind("Url") %>'></asp:TextBox>
</ItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="LoggedInUser"
runat="server"
SelectMethod="GetLoggedInUser"
TypeName="MyData">
</asp:ObjectDataSource>