Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Link Column to ASP.NET GridView

Tags:

I want to output a list of news headlines that are clickable. So far I can get it to print out a list of headlines because I dragged and dropped the NewsHeadline table in designer view in VS 2010. How do you think I should the make the list elements clickable? I looked for a URL attribute but I did not see it. Do I need to wrap in a < a href ?

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"          DataSourceID="SqlDataSource1"          EmptyDataText="There are no data records to display.">         <Columns>             <asp:BoundField DataField="NewsHeadline" HeaderText="NewsHeadline"                  SortExpression="NewsHeadline" />         </Columns>     </asp:GridView>    <asp:SqlDataSource ID="SqlDataSource1" runat="server"          ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>"          ProviderName="<%$ ConnectionStrings:ConnectionString1.ProviderName %>"          SelectCommand="SELECT [NewsHeadline] FROM [NewsTable]"></asp:SqlDataSource>    </form> 
like image 364
patrick Avatar asked Dec 04 '09 23:12

patrick


People also ask

How can create GridView column link in asp net?

Hover your mouse over the GridView and click the arrow that appears in the top right. Go to "Choose Data Source" and select "new data source..." Create the connection string to your database and choose the NewsHeadline table.


2 Answers

You need to change the column type from a BoundColumn to a Hyperlink column.

   <asp:hyperlinkfield headertext="NewsHeadline"       datatextfield="NewsHeadline"       datanavigateurlfield="NewsURL"        datanavigateurlformatstring="http://{0}" /> 

In addition to making this change, you'll need to make sure that you are selecting the URL or something you can use to create the link to the news article. In the example above, I'm assuming the URL is something you can grab from your SQL source. If it is an ID, simply type out the rest of the url like this... "~/MyNewsPage.aspx?NewsID={0}"...

like image 95
RSolberg Avatar answered Sep 24 '22 13:09

RSolberg


Use hyperlinkfield instead :

<asp:hyperlinkfield datatextfield="NewsHeadline"         datanavigateurlfields="NewsID"         datanavigateurlformatstring="~\newsdetails.aspx?Id={0}"  /> 
like image 40
Canavar Avatar answered Sep 20 '22 13:09

Canavar