I have an XML file and it contains data about products,and the most interesting data is a url that takes user to the page of thah product.I have successfully extracted urls from that XML file into XmlNodeList and then I put them into DataTable so these urls can be displayed in ASPxGridview.But these urls are shown as text and are not clickable.How to convert the text into HyperLinks?Thanks in advance!
You are looking for a HyperLinkField: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hyperlinkfield.aspx
You'd bind the url to the DataNagivateUrlFields property.
Edit with code example:
<asp:gridview id="gv1"
autogeneratecolumns="true"
runat="server">
<columns>
<asp:hyperlinkfield text="View Product"
DataNavigateUrlFields="url"
/>
</columns>
</asp:gridview>
Related
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>
I have implemented export data from GridView to excelsheet functionality in .net application.
and result coming in the following format which is wrong:
but result should be in the following format:
column in gridview :
FirstName,
LastName,
<asp:TemplateField HeaderText="List of Answers" >
<headerstyle cssclass="headingtext" />
<ItemTemplate>
<asp:Label ID="lblAnswer" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"List") %>'></asp:Label>
</ItemTemplate>
<itemstyle cssclass="cells" HorizontalAlign="Left" />
</asp:TemplateField>
and text coming from database for 3r column is:
"Q1:No<br/>Chair<br/>Desk<br/>Monitor<br/>Keyboard<br/><br/>"
row generated in excel sheet should be single according to result set.
Expected result should be as shown in second image.
How can we resolve this?
I got the solution.I have to put the following lines in style sheet.
br {mso-data-placement:same-cell;}
You data in your database has two HTML linebreaks (<br />) at the end, so the data in your Excel sheet has them as well. This is, in fact, correct behavior. If you don't want the line breaks in your Excel sheet, don't render them in your GridView, i.e., remove the <br /> at the end of your data before binding it.
Look at the following link
http://aspalliance.com/518
br {mso-data-placement:same-cell;}
is the key.
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">
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.
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.