Adding Link Column to ASP.NET GridView - asp.net

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>

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}"...

Use hyperlinkfield instead :
<asp:hyperlinkfield datatextfield="NewsHeadline"
datanavigateurlfields="NewsID"
datanavigateurlformatstring="~\newsdetails.aspx?Id={0}" />

You need to use a hyperlink field instead of a BoundField, like so:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1"
EmptyDataText="There are no data records to display.">
<Columns>
<asp:HyperLinkField HeaderText="NewsHeadline" SortExpression="NewsHeadline" DataTextField="NewsHeadline" NavigateUrl="..." />
</Columns>

Something like this will work fantastic as a solution in Visual Studio 2010.
Create a GridView in the Designer tab of your webpage in VS.
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.
Write the query SELECT News_Id, NewsHeadline FROM NewsHeadline
Finish the setup. Now some code should be generated in the Source tab. This will also create an SqlDataSource which is now the DataSource of your GridView.
Go to where the code is for your GridView in the Source tab and replace with the following code.
Code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="Id" DataSourceID="SqlDataSource1">
<Columns>
<asp:HyperLinkField
DataNavigateUrlFields="News_Id"
DataNavigateUrlFormatString="~\newsdetails.aspx?News_Id={0}"
DataTextField="NewsHeadline"
HeaderText="News HeadLines"
SortExpression="NewsHeadline" />
</Columns>
</asp:GridView>
And you're all set. This will create a list of all the Headlines as hyperlinks with a dynamically generated unique link to the newsdetails.aspx page compliments of the query string we built using the PRIMARY KEY News_Id corresponding to each NewsHeadline entry in the NewsHeadline table.
Then when you load the newsdetails.aspx page you use: Request.QueryString["News_Id"] to get the News_Id value from the URL and use it to query the database for the details about the specific NewsHeadline that was clicked. You can then display the result of that query on the webpage.

The HyperLinkField will work great as others have pointed out. But, in case you want the entire row clickable, you can use a custom server control that implements a GridView suggested in the SO post "Making an entire row clickable in a gridview".
Check out the question I posted on how to implement a C# custom server control on implementing it.
Just another option.

Related

ASP.Net GridView styling - invisible cell lining

I`m using a Gridview in an ASP.Net web app.
Since it has custom styling and templates, I couldn't figure how to style it the way I want. I want all the lines of the final HtmlTable to be invisible - just purely the cellcontent and margins between them, without a visible table. How can I do such a thing?
The gridView itself:
<asp:GridView
ID="Items" runat="server">
</asp:GridView>
<asp:GridView
ID="Items" runat="server" GridLines="None"
CellSpacing="Set a Value" CellPadding="Set a Value">
</asp:GridView>

ASP Net Gridview HyperLinkField to navigate to URL form database

In my database I have a table with a list of websites and names. I need to populate my gridview HyperLinkField with the names, and each name linking to its correspondnig URL in the database.
In the past I've done this for a simple query linking to another page in my own site, but I can't find anything for what I'm trying to do now.
Assuming items in your datasource has two properties WebsiteName and Link.
Now try this:
<asp:GridView runat="Server" id="gv1" AutoGenerateColumns="False" GridLines="None">
<Columns>
<asp:HyperLinkColumn HeaderText="Links" DataNavigateUrlField="Link" DataTextField="WebsiteName" />
</Columns>
</asp:GridView>

Data Displayed Twice In Gridview (ASP.NET)

I'm trying to make a page where information from the database are displayed on a page. For this, I'm using a Gridview control. The data displays fine, but it displays the same information twice. So basically, two tables are drawn by ASP and placed side by side.
Heres the code I'm using:
<asp:GridView ID="PackagesGV" runat="server" Width="520px">
<Columns>
<asp:BoundField DataField="ID" HeaderText="Package ID"/>
<asp:BoundField DataField="PackageName" HeaderText="Package Name"/>
<asp:BoundField DataField="PackageText" HeaderText="Package Text"/>
<asp:BoundField DataField="PackageImageID" HeaderText="Package Image"/>
<asp:BoundField DataField="PageID" HeaderText="Page ID"/>
</Columns>
</asp:GridView>
Also, the SQL Stored Procedure is pulling all of the fields required by the Gridview. The SQL is basically
"SELECT [ID], [PackageName], [PackageText], [PackageImageID], [PageID] FROM [Packages]"
So I'm not requesting the information twice using the Stored Procedure.
I've started ASP.NET in July, so I apologise now if this is something really simple.
Thanks!
Michael
You need to either set the GridView.AutoGenerateColumns Property to false or not set up the columns.
If you choose the former method your grid definition will become:
<asp:GridView ID="PackagesGV" runat="server" Width="520px" AutoGenerateColumns="False">

ASP.NET: How to assign ID to a field in DetailsView?

I have a master-detail page, in which I use GridView to display multiple rows of data, and DetailsView + jQuery dialog to display the details of a single records. only one DetailsView is open at a time.
I need to be able to pull out a single field of the open DetailsView, for manipulation using JavaScript. Is there a way to give a unique ID to a given field in DetailsView, so I can use getElementByID? Or is there another way to accomplish what I'm trying to do?
Thank you in advance.
If you are using a bound textbox in a template field in your detailsview you can then select it by:
$("[id$='MyTextBox']");
Which will find the textbox bound to MyFieldName as below.
<asp:DetailsView ID="DetailsView1" runat="server">
<Fields>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="MyTextBox" Text='<%# Bind("MyFieldName")%>' runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
Whatever guff asp.net adds onto the begining of the id won't matter because jQuery $= mean "ends with".
there may be a better way, but when I've needed an id available for js work, I just convert the bound field to a templated field. you can then give the textbox the id of your choice. keep in mind when rendered the id will be expanded with the id of the parent control.

Using GridView HyperLinkField to set a CategoryID

I am using asp.net and I'm working with a gridview. I have one of the fields in my GridView set up as a HyperLinkField that represents the name of the category. I pull the name and Id from a database and I can never be sure what they are because they are added separately. I want to be able to pass a changing CategoryId to another page through the query string. How can I do this with a GridView?
In your Hyperlink field, set the DataNavigateUrlFields to your ID column and set the DataNavigateUrlFormatString to the Url to navigate to (where {0} will be replaced by ID)
<asp:HyperLinkField
DataNavigateUrlFields="ID"
DataNavigateUrlFormatString="Target.aspx?ID={0}"
DataTextField="Name"
Target="_blank">
</asp:HyperLinkField>
You can do it by using a <asp:TemplateField /> as one of your columns instead of a <asp:BoundField />
Code example:
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<%#Container.DataItem("name")
</ItemTemplate>
</asp:TemplateField>
There might be a cleaner way of doing this rather than using the anchor element, but you should get the idea.

Resources