views:

239

answers:

3

Hello,

I'm trying to create a table with Listview and one of the fields I'm using is supposed to show a hyperlink to a more detailed view of the data shown, how I want to do that is by using FindControl on the ID of that item and then changing the value into a hyperlink of the detailed view page with a querystring attached, the problem is that I have no idea how to re-insert that data back into the listview field, which looks something like this:

                    <ItemTemplate>
                    <td>
                    <asp:Label ID="ViewLinkLabel" runat="server" 
                        Text='[insert Link Here]' />
                    </td>
                    </ItemTemplate>

Please bear in mind that I'm still an amateur in ASP.net, and if any of this seems too convoluted when there's a much easier to do this that I don't know about.

Thank you

A: 

Change the label to a hyperlink and Use the OnItemDataBound event on the listview to modify the element: something like.

  protected void ContactsListView_ItemDataBound(object sender, ListViewItemEventArgs e)                  
  {
        try
        {
            HyperLink ViewLinkLabel = (HyperLink)e.Item.FindControl("ViewLinkLabel");
            lnkEvent.NavigateUrl += "http://required.url;
        }
        catch
        {

        }
    }

ListView.ItemDataBound Event for more details

Richard Harrison
A: 

Hey,

Yeah you have made a few mistakes here. First off if you were going to do this you would want to use an <asp:Hyperlink> instead of an <asp:Label>.

Also instead of trying to find the control in the code behind and set its value you would probably use a databinding statement in the markup.

This would look like this:

<ItemTemplate>
  <td>
    <asp:HyperLink ID="ViewLinkLabel" runat="server" Text="More details"
         NavigateUrl='<%# string.Format("~/DetailPage.aspx?ID={0}", Eval("RecordID")) %>' />
  </td> 
</ItemTemplate>

The pattern that you are referring to is called master / detail. This means you have a master list which lets you drill down into details. There are a bunch of tutorials which explain various ways to set this up on the official asp.net website:

They were written before asp.net 3.5 came out so they don't cover the listview control but they will explain to you how to set up master / detail and also how the databinding syntax works. If you went through the first 25 tutorials in that data access series then you would have a pretty solid understanding of how a lot of the asp.net features work. The data access technology it uses is a bit out of date but it is an easy one to get started with.

rtpHarry
+1  A: 

I dont have enough reputation to leave comments or edit posts but the code sample in Richard Harrison's post has some problems:

  protected void ContactsListView_ItemDataBound(object sender, ListViewItemEventArgs e)                  
  {
        try
        {
            HyperLink ViewLinkLabel = (HyperLink)e.Item.FindControl("ViewLinkLabel");
            ViewLinkLabel.NavigateUrl = "http://www.example.com/";
        }
        catch
        {

        }
    }

Ideally you should also check if ViewLinkLabel is null before you use it.

Also this is presuming that the ViewLinkLabel control is a HyperLink but in the question this is actually a Label control.

rtpHarry
this worked, thanks.
Viredae