Hyperlink on a gridview or repeater

Fuma

Executive Member
Joined
Jul 9, 2007
Messages
5,111
I have a repeater:
Code:
<asp:Repeater ID="repeater1" runat="server">

</asp:Repeater>
or a gridview

Code:
<asp:GridView ID="GridView1" runat="server">

</asp:GridView>

There is a database table that has two fields:

name page
Page 1 default.aspx
Page 2 whatever.aspx
Page 3 test.aspx
When I click on Page 1 on the grid, it should take me to default.aspx

How do i add hyperlink or linkbutton to either gridview or repeater (whichever one is better) at run time?
I don't want to touch the design

For now I can only bind the data to the repeater/gridview
Code:
dim reader As SqlDataReader
dim objCommand As SqlCommand
dim sql as string = "SELECT name, page from webpages"

objCommand = New SqlCommand(sql, sqlConnection1)
reader = objCommand.ExecuteReader()
repeater1.DataSource = reader [COLOR="blue"] ' OR  GridView1.DataSource = reader[/COLOR]
 

FarligOpptreden

Executive Member
Joined
Mar 5, 2007
Messages
5,396
Binding the data to the repeater is the right thing to do. All you have to do now, is to add a Hyperlink web control to the repeater or grid and set the Text and NavigateUrl properties to point to some field from the datasource:

Code:
<asp:Repeater ID="repeater1" runat="server">
    <asp:HyperLink 
        ID="myHyperLink" 
        runat="server" 
        Text="<%# DataBinder.Eval(Container, "DataItem.name") %>" 
        NavigateUrl="<%# DataBinder.Eval(Container, "DataItem.page") %>" />
</asp:Repeater>

That piece of markup tells the page to bind the Text property of the Hyperlink to the "name" field of the data item in the current container, while binding the NavigateUrl property to the "page" field. Just replace the "name" and "page" fields with the proper fields retrieved from your query and you'll be alright... :)
 

MielieSpoor

Expert Member
Joined
Dec 6, 2006
Messages
1,984
When using a datagrid, make use of a template field, then you can add your hyperlink control in there.
 

Fuma

Executive Member
Joined
Jul 9, 2007
Messages
5,111
Binding the data to the repeater is the right thing to do. All you have to do now, is to add a Hyperlink web control to the repeater or grid and set the Text and NavigateUrl properties to point to some field from the datasource:

Code:
<asp:Repeater ID="repeater1" runat="server">
    <asp:HyperLink 
        ID="myHyperLink" 
        runat="server" 
        Text="<%# DataBinder.Eval(Container, "[B]DataItem.[/B]name") %>" 
        NavigateUrl="<%# DataBinder.Eval(Container, "[B]DataItem.[/B]page") %>" />
</asp:Repeater>

That piece of markup tells the page to bind the Text property of the Hyperlink to the "name" field of the data item in the current container, while binding the NavigateUrl property to the "page" field. Just replace the "name" and "page" fields with the proper fields retrieved from your query and you'll be alright... :)

I was trying that the whole day yesterday and eventually gave up. But I eventually got it write using your code. The DataItem. part was the problem. I just used the field name. Eish
Tx,
 

Raithlin

Executive Member
Joined
Jan 4, 2005
Messages
5,049
Now, Farlig. If you'd have done this on StackOverflow you'd have gotten some recognition. ;)
 

Fuma

Executive Member
Joined
Jul 9, 2007
Messages
5,111
Then how do I calculate the total/sum of <%# DataBinder.Eval(Container, "DataItem.Qty") %> on the footer?
Code:
<FooterTemplate>
<asp:Label ID="lblTotalQty" runat="server">
</FooterTemplate>
I believe I have to run the calculations in the ItemDataBound event
 

FarligOpptreden

Executive Member
Joined
Mar 5, 2007
Messages
5,396
Now, Farlig. If you'd have done this on StackOverflow you'd have gotten some recognition. ;)

Ya ya ya... :p

Then how do I calculate the total/sum of <%# DataBinder.Eval(Container, "DataItem.Qty") %> on the footer?
Code:
<FooterTemplate>
<asp:Label ID="lblTotalQty" runat="server">
</FooterTemplate>
I believe I have to run the calculations in the ItemDataBound event
That would be the easiest, possibly, but what about including the sum in the query as a seperate field? That might save you some processing time...
 

Fuma

Executive Member
Joined
Jul 9, 2007
Messages
5,111
Ya ya ya... :p


That would be the easiest, possibly, but what about including the sum in the query as a seperate field? That might save you some processing time...
Code:
[U]Qty..............Price[/U]
10...................8
15...................2
5.....................10
_______________
[B]30[/B]....................[B]20[/B]
How?
 

Raithlin

Executive Member
Joined
Jan 4, 2005
Messages
5,049
Code:
dim sql as string = "SELECT Qty, Price, (Qty * Price) AS TotalQty from table"
 
Last edited:

Fuma

Executive Member
Joined
Jul 9, 2007
Messages
5,111
Code:
dim sql as string = "SELECT Qty, Price, (Qty * Price) AS TotalQty, page from table"
Not that. I want a total for Qty and price on the footer, meaning Qty = (10+15+5 = 30) and price = (8+2+10=20).
It was a hypothetical table I used there.
 

Raithlin

Executive Member
Joined
Jan 4, 2005
Messages
5,049
Ah, my mistake. :)

You could either add it up manually (not really recommended if it means stepping through each record) or if you are using a compatible database platform, then what about "WITH ROLLUP" in your statement?
 

Fuma

Executive Member
Joined
Jul 9, 2007
Messages
5,111
I decided to create 2 label controls (lblTotalQty and lblTotalPrice) in the footer and ran it in the ItemDataBound event.

Code:
        sql = "select sum(Qty) as Qty, sum(price) as price  FROM table" [COLOR="blue"]'same WHERE clause as the previous sql[/COLOR]
       
       If e.Item.ItemType = ListItemType.Footer Then
         Dim lblTotalQty As Label = e.Item.FindControl("lblTotalQty")
         Dim lblTotalPrice As Label = e.Item.FindControl("lblTotalPrice")

         lblTotalQty.Text = ds.Tables(0).Rows.Item(0).Item("Qty")
         lblTotalPrice.Text = ds.Tables(0).Rows.Item(0).Item("price")
      end if
 

Fulmine

Well-Known Member
Joined
May 13, 2009
Messages
193
Dude do you know what a stored proc is m8 :)

On possible candidates the first thing a person would get skipped over for is inline SQL.

No: 2 is connection strings in code and not in the config files.
 
Top