Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to find a solution to a problem I'm having in passing a value to one form, "SongsByBand" to another form in my project "ViewDetails". "SongsByBands" is essentially a list of bands with each song played by that band presented in a bullet list. I want to, in some way, navigate from a single song in the bulleted list to the "ViewDetails" Form; ViewDetails takes the song id to access additional information about that specific song. The songs and the bands are contained in separate tables of a SQL Server database and Entity Framework is used to access the fields from each table.

The pertinent code in the "SongsByBand" Form follows:

<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
<asp:EntityDataSource ID="EntityDataSource1" runat="server">
</asp:EntityDataSource>
<asp:Repeater ID="Repeater1" runat="server" >
<ItemTemplate>
<h3> <asp:Literal ID="Literal1" runat="server"
Text='<%# Eval("Band1")%>'></asp:Literal></h3>
<h4> <asp:Literal ID="Literal2" runat="server"
Text='<%# Eval("Notes")%>'></asp:Literal></h4>
<asp:BulletedList ID="SongList" runat="server" DisplayMode="LinkButton"
DataSource = '<%# Eval("Songs") %>' DataTextField="Title"
DataValueField="Id">
</asp:BulletedList>
</ItemTemplate>
</asp:Repeater>
</asp:Content>


With the accompanying code behind:

C#
public partial class Songs_SongsByBand : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
      using (TRTDevelopmentSongsAndBandsEntities myEntities = 
             new TRTDevelopmentSongsAndBandsEntities())
      {
        var AllSongs = from Band in myEntities.Bands.Include("Songs")
                       orderby Band.Band1
                       select new { Band.Band1, Band.Notes, Band.Songs };
        Repeater1.DataSource = AllSongs.ToList();
        Repeater1.DataBind();
      }
    }
}

[NB: I left out the "using" statements.]

The pertinent code in the "ViewDetails" Form again follows:
ASP.NET
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
  <h1><asp:Label ID="TitleLabel1" runat="server" ></asp:Label></h1><br />
  <asp:Label ID="BandLabel1" runat="server" ></asp:Label><br />
  <asp:Label ID="SongWriterLabel1" runat="server" ></asp:Label><br />
  <asp:Label CssClass="Comments" ID="CommentsLabel1" runat="server"></asp:Label>
  <br />
    <asp:HyperLink ID="HyperLink2" runat="server" >For Song Clip, Click Here
    </asp:HyperLink>
    <br/>
  </asp:Content>


With the following code behind:

public partial
C#

class Songs_ViewDetails : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
int songId = Convert.ToInt32(Request.QueryString.Get("SongId"));
using(TRTDevelopmentSongsAndBandsEntities
myEntities = new TRTDevelopmentSongsAndBandsEntities())

mySong = (from r in myEntities.Songs.Include("Band")
where r.Id == songId
select r).SingleOrDefault();
if(mySong != null)
{
TitleLabel1.Text = "Title: - " + mySong.Title;
BandLabel1.Text = "Band/Singer: - " + mySong.Band.Band1;
SongWriterLabel1.Text = "Song Writer(s): - " + mySong.SongWriter;
CommentsLabel1.Text = "Comments: - " + mySong.Comments;
HyperLink2.NavigateUrl = mySong.ClipLink;
HyperLink2.DataBind();
Title = mySong.Title;
MetaDescription = mySong.Comments;
}
}
}

What I have tried:

I have been working on this for several days and attempted a number of approaches to capture the "SongId" and pass it to the "ViewDetails" Form:

I tried using a hyperlink in the "BulletedList" using NavigateUrl with variations of the following syntax:

NavigateUrl='<%# "ViewDetails?SongId=" + Item.Id.ToString() %>'

But the code doesn't recognize Item or ID or something like Song.Id or any other form I can come up with.

As you may note I have inserted DataValueField="Id" and confirmed that the ID retrieved is the appropriate SongId. It's very frustrating to be able to see the value but can find no method to pass it into the "ViewDetails" Form.

Any guidance or pointers that can be offered would be truly appreciated. T
Posted
Updated 11-Oct-16 6:29am
v7
Comments
Sinisa Hajnal 3-Oct-16 7:18am    
How do you bind other properties? I.e. if you use Item.BandName, same should work for Item.Id. <== note that this means that your Id field holds songId and not bandId or some other Id value.

I'm not sure that I understand the problem. Is it that Id doesn't get written to SongId parameter or that your DetailsView cannot see/parse the parameter?
Tipton Tyler 3-Oct-16 8:37am    
Thank you for your quick response. I don't believe the problem is in the binding. I can create a bulleted list that lists song titles using:

DataSource = '<%# Eval("Songs") %>' DataTextField="Title"

and a bulleted list that lists the appropriate song id using:

DataSource = '<%# Eval("Songs") %>' DataValueField="Id"

NB[somehow part of the code for the "SongsAndBands" forms got deleted from the question and looks like this:

<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
<asp:EntityDataSource ID="EntityDataSource1" runat="server">

<asp:Repeater ID="Repeater1" runat="server" >





I believe my problem is structure; I don't know how to capture the the song id and insert it in a hyperlink within the bulleted list.

I don't see anywhere that you're setting Item to a value. Maybe you need to use mySong.Id instead?
 
Share this answer
 
Comments
Tipton Tyler 3-Oct-16 17:51pm    
I'm not quite sure where I need to add "Item". However, here are some things I tried after adding a NavigateUrl tag to the "BulletedList"
1) NavigateUrl='%# "ViewDetails?SongId=" + Songs.Id.ToString()%'
// Songs does not exist in current context.
mySong.Id.ToString() //mySong does not exist in current context.
Song.Id // An object reeference is required for the non-statid field, method or property 'Song.Id.Get'
Song.Id // An object reeference is required for the non-statid field, method or property 'Song.Id.Get'
Value% // The name Value does not exist in current context. (I thought this might get the value from the "BulletedList" "DataValueField" but obviously it didn't)
I had little luck using a second
NavigateUrl='%# "ViewDetails?SongId=" + Eval("Id")%' // System.Web.HttpException: DataBinding: does not contain a property with the name 'Id'.
Eval("mySong.Id")%' // System.Web.HttpException: DataBinding: does not contain a property with the name 'mySong'.
Eval("Song.Id")%> // System.Web.HttpException: DataBinding: does not contain a property with the name 'Song'.
I'm beginning to believe I need a whole new approach but I don't really know where to start. It can't really be that hard to programatically stick a value into a "NavigateUrl".

[NB: I had to remove all the tags because the text editor seems to interpret them as end markers.]
#realJSOP 3-Oct-16 17:55pm    
escape them by use &lt; and &gt;
Issue solved. In the unlikely case anyone has a similar issue here is how I solved it. I assume there are other approaches that are probably even better but this worked.

Yes, I needed a new approach. I got rid of the "BulletedList" and just went with the Repeater. I then placed the HyperLink inside a Template within the Repeater tag. Finally, I added a Css Class to the hyperlink (<pre>CssClass="HyperLinkBullet" </pre>) and essentially formatted the bulleted list in the Css Style Sheet In essence here is the pertinent code:

lang="text"><pre lang="HTML"><asp:Repeater ID="BandRepeater" runat="server">
&lt;HeaderTemplate>
<p>Below you will find a list of songs for each of your favorite bands.</p>
&lt;/HeaderTemplate>
<ItemTemplate>
<h3>
<asp:Literal ID="Literal1" runat="server" Text='<%# Eval("Band1") %>'></asp:Literal>
</h3>
<h4>
<asp:Literal ID="Literal2" runat="server" Text='<%# Eval("Notes") %>'></asp:Literal>
</h4>
<asp:Repeater ID="SongRepeater" runat="server" ItemType="Song" DataSource='<%# Eval("Songs") %>'>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" CssClass="HyperLinkBullet" runat="server" Text='<%# Item.Title %>'
NavigateUrl='<%# "ViewDetails?SongId=" + Item.Id.ToString() %>'>
</asp:HyperLink><br />
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>

The css code I used in the css Style sheet is quite simple but the approach has the advantage of flexability in that additional styling of the "bulleting" could be incorporated:

<pre lang="text">.HyperLinkBullet

{
display: list-item;
list-style-type: disc;
margin-top: 10px
}</pre>
 
Share this answer
 
Comments
Vincent Maverick Durano 11-Oct-16 15:22pm    
Could you format your code to make it more readable and friendly to future readers. Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900