开发者

ASP.NET Repeater Control linking to detailed view via QueryString parameters

I have a Repeater Control using an XMLDataSource to produce a list of movies (Movies.aspx). I need to link to a detailed page via query parameters like MovieDetails.aspx?movie=Matrix. What control do I use on the MovieDetails.aspx page to render a single movie, preferably using ItemTemplate and my own HTML.

My data source:

<asp:XmlDataSource ID="MoviesXmlDataSource" runat="server" 
    DataFile="~/Movies.xml" XPath="movies/movie"></asp:XmlDataSource>

I read the StackOverflow post Send string with QueryString in Repeater Control in ASP.net and list my items via a repeater like this:

    <asp:Repeater ID="Repeater1" runat="server" DataSourceID="MoviesXmlDataSource">
    <HeaderTemplate>
        <ul class="productlist">
    </HeaderTemplate>

    <ItemTemplate>
        <li>
            <a href="MovieDetails.aspx?movie=<%#Eval("title")%>"></a>
            <img src="Images/<%#Eval("image") %>" /><br/> 
            <b><%#Eval("title") %></b>
        </li>
    </ItemTemplate>

    <FooterTemplate>
        </ul>
    </FooterTemplate>
</asp:Repeater>

On my MovieDetail.aspx I get the query string paramete开发者_开发技巧r as expected. However, I don't know how to fetch this item from my XMLDataSource and render it nicely. I figured out how to do this using GridView and then render a DetailsView depending on what you click in the grid, but it's so ugly. Repeater lets me specify my own HTML, but only for a list and not a single item.


The whole idea of the Repeater control is to display a set of data in a consistent manner. And the HTML you apply is actually for a single item in a list of items. And if i were you, i would be using a table to display the items. Title in one column, Image in another, details in another etc. Or you could use a

Title
--------
Image  |   Description Line 1
       |   Description Line 2
       |   Description Line 3
       |   Description Line 4
--------

Title
--------
Image  |   Description Line 1
       |   Description Line 2
       |   Description Line 3
       |   Description Line 4
--------

approach.

The only thing that is stopping you from getting a nice list of movies is the knowledge of HTML (which you already know) and creativity! Let your imagination run wild ;)

  • Lists
  • would
  • definitely
  • suck
  • in
  • this
  • scenario
  • :(


I just figured this out myself. I didn't realize that I could simply access my XMLDataSource from my code behind. I don't have to bind it to any control. Here is my solution:

  1. Upon PageLoad, get the url parameter movie
  2. Convert MoviesXMLDataSource to an XmlDocument
  3. Fetch the single node where title matches my movie parameter
  4. Fetch title, description and image link and store as variables
  5. In the aspx, render normal HTML and insert <%=title %> and so on...

    protected void Page_Load(object sender, EventArgs e)
    {
       this.movieTitle = Request["movie"];
    
       string xpath = String.Format("/movies/movie[@title=\"{0}\"]", movieTitle);
       XmlDocument doc = MoviesXmlDataSource.GetXmlDocument();
       XmlNode node = doc.SelectSingleNode(xpath);
    
       this.title = node.Attributes["title"].Value;
       this.description = node.Attributes["description"].Value;
       this.image = node.Attributes["image"].Value;
    }
    

And in the aspx I render stuff as normal:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

<asp:XmlDataSource ID="MoviesXmlDataSource" runat="server" 
    DataFile="~/Movies.xml" XPath="movies/movie">
</asp:XmlDataSource>    

<h1><%=title %></h1>
<p>
    <img src="<%=image %>"/><br />
    <%=description %>
</p>

</asp:Content>

In the end this was really simple. I just got confused by forcing myself to use a control.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜