turn sql rows into links on a vb.net page? - asp.net

I am trying to create a specific aspx page where I display clickable links based on information in a sql database. For example one column could be the anchor tag another column could be the path to the link itself etc. In the past I would pull this information from sql and put it into a non-visible Label (say linkLabel1). Then in the page itself I would insert <%linkLabel1.text%> to insert the link path from the database to the appropriate area.
I hope I'm not confusing things too much here and that makes sense how I explained it.
What I would like to do is set up a way that I could simply enter a new row into a SQL table with link information and a web page automatically displays the new link for me.
I guess I am mostly looking for insight, opinions, or directions of what approach to consider. I can elaborate if I was unclear (wouldn't be awfully surprising if I was not).
Thanks in advance for anyone's time on this matter.

Since you are displaying this in a table, you could use a GridView for this. The columns that will display the link could be defined as hyperlink columns as so:
<Columns>
<asp:HyperLinkField
HeaderText="Header text"
DataNavigateUrlFields="PropertyContainingTheHRefForTheAnchor"
DataTextField="PropertyContainingTheTextForTheAnchor"
/>
</Columns>
So for example, if you return a record set containing these columns:
TextProperty PathProperty
See Details Assets/SomeOther/
Click me Products/AnotherPath/
Your grid will render these as:
See Details
Click me
If you define the column as:
<Columns>
<asp:HyperLinkField
HeaderText="Header text"
DataNavigateUrlFields="PathProperty"
DataTextField="TextProperty"
/>
</Columns>

Related

Publish two types of field information on same column in ASP.Net grid view

I want to publish two types of information on same column in ASP grid view.
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:hyperlinkfield text="Detail" datanavigateurlfields="name" datanavigateurlformatstring="LeadInformation.aspx?name={0}" />
The first column is a BoundField and second one is hyperlinkField. These two types of information will display separately on two columns.
or else I can do like this.
<asp:HyperLinkField DataNavigateUrlFields="name" DataNavigateUrlFormatString="LeadInformation.aspx?name={0}"
DataTextField="name" HeaderText="User Name" SortExpression="name" />
Then it will display all information on one column as a hyperlink.
I just want to display name as it is. (not as a hyperlink). And in same column,add a hyperlink like above.
Please help me.
Make it a TemplateField. Then add a Label and a Hyperlink. Then for the Text of the Label and the NavigateURL for the hyperlink use Bind("ColumnName"). (or on the design mode right click and select Databindings and enter Bind("ColumnName") for the properties.
You can refer the below MSDN link which explains how to show two values in a single column (FirstName and LastName in this case)
MSDN
The answer is absolutely correct. A template field allows you to display anything you like in a gridview, rather than the standard ASP:NET gridview column controls. The price you pay is that it then becomes a bit harder to get access to the values in the controls. You will find it's often useful to use the CommandName and CommandArgument properties to get at values of the controls.
You have a fair learning curve ahead of you, I'm afraid, and certainly the question is too broad to be answered here. Try this article, or searching for "ASP.NET gridview templatefield".

How to map DataNavigateUrlFormatString of Gridview Hyperlink Field dynamicatlly vb.net?

Hope you are having a nice day.
I am developing a gridview for a list of songs with a hyperlink field like below. I left unnecessary codes by the way. The purpose of hyperlink field is for user to download songs. I am using visual studio 2010 and I am still using development server such as http://Localhost:xxxx/mypage.aspx. So I have to update the hyperlink field everytime I run this page in order to keep the link correct. I understand once i put on the live server, i just need to put the domain and this problem is fixed but I am wondering if there is a way to dynamically map the path of DataNavigateUrlFormatString to the development server URL so that I don't need to keep changing while I am on development server.
Thanks a lot.
<asp:GridView>
<Columns>
<asp:BoundField DataField="Song_Name" HeaderText="Song_Name" SortExpression="Song_Name" />
<asp:HyperLinkField DataNavigateUrlFields="Song_Location"
DataNavigateUrlFormatString="http://localhost:6686/RioMusic/Uploads/{0}"
DataTextField="Song_Name" HeaderText="Download" />
</Columns>
</asp:GridView>
Can you just use a relative link? I.e.:
Uploads/{0}
(This will be relative to the page displaying the link.)
Or, if you wish to specify a url relative from the root of your site:
~/RioMusic/Uploads/{0}

ASP.NET 4 GridView - Pulling a Hyperlink out of a Database

I have a GridView bound to a SqlDataSource.
I'm pulling hyperlinks which point to Job Descriptions stored in a separate web space, out of a database and placing them in the GridView.
These are full Hyperlinks such as "Http://stackoverflow.com/"
Originally the GridView column was a simple BoundField like this:
<asp:BoundField DataField="JobDescription" HeaderText="JobDescription"
SortExpression="JobDescription" />
So I started trying to convert it into a hyperlink field.
<asp:HyperLinkField DataNavigateUrlFields="JobDescription"
DataTextField="JobDescription"
HeaderText="JobDescription"
SortExpression="JobDescription"
Target="_blank"
NavigateUrl="{0}" />
This produced the desired result, but I can no longer edit that column in the GridView. When it was a BoundField I could edit the item, but could find no way to make it into a hyperlink.
Either way will work...
I either need the HyperLinkField to be updatable, or I need the BoundField to be formatted as a Hyperlink with what it pulls directly from the database.
I appreciate the help.
Use a Template Field. So your can define your normal view and editing view.
Grrr found the answer:
<asp:BoundField DataField="JobDescription" HeaderText="Job Description"
SortExpression="JobDescription"
DataFormatString="<a target='_blank' href='{0}'>Text</a>"
HtmlEncode="False" />
You don't need a template field. That HtmlEncode property must be set to false in order for html in DataFormatString to be rendered as html, otherwise it changes all of your characters into the equivalent of stuff like...
The Entity Numbers here: http://www.w3schools.com/tags/ref_entities.asp

showing images through gridview?

suppose my images binary data is saved in database table and i want to show those images through gridview. one approach is just create one page where i will pass some id as query string and that page will retrieve the image data from db and stream down the image data to my page through BinaryWrite method. a example is http://www.aspdotnetcodes.com/Insert_Images_Database.aspx. this url describe how to show image data through BinaryWrite.
this approach is old and i know. is there any other way by which i can show the image in gridview but i don't want to store image url in table.please let me know if you know any other approach for showing the images through gridiview when image binary data is stored in db. thanks
I asked a similar question here:
Streaming Databased Images Using HttpHandler
Note that I had to use a DataReader in my final version :-)
It sounds like you've got your images stored in your database in binary format. You then want to show them in your GridView.
Consider this approach:
create a template field in your gridview to hold your image. It'll end up calling a handler URL to grab the binary data for your image.
create the image handler page -- make it an .ashx. Put a unique identifier on the querystring, for example, to grab the image you want from the database
<Columns>
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Height="80px" Width="80px"
ImageUrl='<%# "ImageHandler.ashx?ImID="+ Eval("ImageID")
%>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
See the answer at the bottom of this MSDN question for more details on how to implement your image handler. You then won't have to bring back the binary data when binding your GridView. On the other hand, it creates n calls to the handler for each row in the grid.

Best way to custom edit records in ASP.NET?

I'm coming from a Rails background and doing some work on a ASP.NET project (not ASP MVC). Newbie question: what's the easiest way to make a custom editor for a table of records?
For example: I have a bunch of data rows and want to change the "category" field on each -- maybe a dropdown, maybe a link, maybe the user types it in.
In Rails, I'd iterate over the rows to build a table, and would have a form for each row. The form would have an input box or dropdown, and submit the data to a controller like "/item/edit/15?category=foo" where 15 was the itemID and the new category was "foo".
I'm new to the ASP.NET model and am not sure of the "right" way to do this -- just the simplest way to get back the new data & save it off. Would I make a custom control and append it to each row? Any help appreciated.
You can REALLY cheat nowadays and take a peek at the new Dynamic Data that comes with .NET 3.5 SP1. Scott Guthrie has a blog entry demoing on how quick and easy it'll flow for you here:
http://weblogs.asp.net/scottgu/archive/2007/12/14/new-asp-net-dynamic-data-support.aspx
Without getting THAT cutting edge, I'd use the XSD generator to generate a strongly typed DataSet that coincides with the table in question. This will also generate the TableAdapter you can use to do all your CRUD statements.
From there, bind it to a DataGrid and leverage all the standard templates/events involved with that, such as EditIndex, SelectedIndex, RowEditing, RowUpdated, etc.
I've been doing this since the early 1.0 days of .NET and this kind of functionality has only gotten more and more streamlined with every update of the Framework.
EDIT: I want to give a quick nod to the Matt Berseth blog as well. I've been following a lot of his stuff for a while now and it is great!
There are a few controls that will do this for you, with varying levels of complexity depending on their relative flexibility.
The traditional way to do this would be the DataGrid control, which gives you a table layout. If you want something with more flexibility in appearance, the DataList and ListView controls also have built-in support for editing, inserting or deleting fields as well.
Check out Matt Berseth's blog for some excellent examples of asp.net controls in action.
Thanks for the answers guys. It looks like customizing the DataGrid is the way to go. For any ASP.NET newbies, here's what I'm doing
<asp:DataGrid ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="RuleID" Visible="False" HeaderText="RuleID"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Category">
<ItemTemplate>
<!-- in case we want to display an image -->
<asp:Literal ID="litImage" runat="server">
</asp:Literal>
<asp:DropDownList ID="categoryListDropdown" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
This creates a datagrid. We can then bind it to a data source (DataTable in my case) and use things like
foreach (DataGridItem item in this.GridView1.Items)
{
DropDownList categoryListDropdown = ((DropDownList)item.FindControl("categoryListDropdown"));
categoryListDropdown.Items.AddRange(listItems.ToArray());
}
to populate the intial dropdown in the data grid. You can also access item.Cells[0].text to get the RuleID in this case.
Notes for myself: The ASP.NET model does everything in the codebehind file. At a high level you can always iterate through GridView1.Items to get each row, and item.findControl("ControlID") to query the value stored at each item, such as after pressing an "Update" button.

Resources