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

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

Related

change Date format in VB.Net

How to change the Date format in a asp grid displaying date format populated from Database.
The Database stores value in '2009-08-23 10:24:00.000' format but the value in grid is displayed as 'Sun 23-08-2009 10:24:00'
I want to display it as '23-AUG-2008'
VB .Net Code
"Dim dv As DataView1 = ds.Tables(0).DefaultView"
Markup is
<asp:BoundColumn DataField="DateFormat" HeaderText="Day&Date">
<ItemStyle Wrap="False" />
How can i display as '23-AUG-2008'?
You can use DataFormatString property of the BoundColumn. There just specify the desired format.
In your case:
<asp:BoundColumn DataField="DateFormat" DataFormatString="{0:dd-MMM-YYYY}" HeaderText="Day&Date">
Btw, DataGridView is a control from Winforms, not webforms...In webforms it's called GridView. And this has nothing to do with javascript or vb.net, so I've removed the tags as well.. The solution is specific to GridView control, which belongs to asp.net as whole no matter the code-behind language. Yes, you could use javascript to format those string, but...

DetailsView "Selected fields" ListBox of "Fields" window shows no fields

On a web form we have a DetailsView bound to a dataSource.
The property called "AutGenerateRows" is also set to true.
When the web form is displayed in a web browser, the data from the fields are shown.
We want to change the properties of some of the fields. When we display the "Fields" window, I noticed that the "Selected fields" ListBox only contained a CommandField and no other fields.
Can you tell me how to set up the DetailsView so we can change the properties of the fields?
The attached screen shots will show what is happening. There are many fields and we want to avoid typing in all the code needed to make the fields as template fields if possible.
These screen shots show what is happening:
Update: Thanks for your replies. With your help, we are now on the correct path. Here is what the finished web form looks like:
For deparatment ID add a template field column
From the fields window, add a templateField Column. Then go to aspx source code place a dropdownlist. Something like this:
<asp:TemplateField >
<ItemTemplate>
<asp:DropDownList ID="DDL" runat="server" DataTextField='<%# Eval("FieldName") %>' />
</ItemTemplate>
</asp:TemplateField>

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

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>

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

Calling a method on DataBound field 's DataField

I want to call a method on databound field on datafield. e.g.
<asp:BoundField DataField="TitlePLang" HeaderText="Batch Title" /> This is simple bound field but I want to do something like this.
<asp:BoundField DataField='<%# Eval(getCultureSpecificTitle("TitlePLang","TitleSLang",true))%>' HeaderText="Batch Title" />
Where getCultureSpecificTitle is a method in code behind, but its giving error that bound field doesn't allow data binding.
So please help how can I do this....
Well this is not possible using the bind files but you can make use of the TempalteField avaialbe in the grid view will allows you to do this easily.
If you want to achieve same from the code behind than you make use of OnRowDataBound event available for the datagrid view will do the work for you.

Resources