Hyperlink control (inside TemplateField tags) is not acting like an anchor tag? - asp.net

I have this murkup somewhere in my gridview. The gridview is showing the data for that column, but it's not showing up like an anchor tag, i.e. no underlining and not possible to click it (Just like plain-tex).
<asp:TemplateField HeaderText="Status" Visible="True"> <ItemTemplate>
<asp:HyperLink ID="statusHpLink" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Status") %>' /> </ItemTemplate>
<ItemStyle CssClass="itemstyle" />
</asp:TemplateField>
Thanks for helping

YOu need to set the NavigateUrl attribute in your markup.
<asp:HyperLink runat="server" ID="link" Text="Click Me"
NavigateUrl="http://stackoverflow.com"/>

Related

How can I navigate to a different URL when user clicks the hyperlink in the GridView with a condition?

I am a beginner in learning asp.net. I have a column in a GridView with header name FORM ID. I want to be able to navigate to the different URL based on the part of the FORM ID.
For example,
Clicking on abc10001 will take us to "~/abc1.aspx?formid=abc10001"
Clicking on abc20001 will take us to "~/abc2.aspx?formid=abc20001"
I understand the use of the MID function like so v=MID(string,4,1) to capture the 4th value and redirect to page by determining the value v but I do not know how to apply this correctly. Please guide me. Your help is greatly appreciated.
The following is the aspx code I'm currently work on :
<asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="false">
<columns>
<asp:Hyperlinkfield DataTextField="formid" HeaderText="Form ID" ItemStyle- Width="150px"
DataNavigateUrlFields="formid" DataNavigateUrlFormatString="~/abc1.aspx" />
</Columns>
</asp:GridView>
You could switch your Hyperlinkfield to a TemplateField with a HyperLink control to give you more control over the NavigateUrl like so:
<asp:TemplateField HeaderText="Form ID">
<ItemTemplate>
<asp:HyperLink runat="server" Text='<%# Eval("formid") %>'
NavigateUrl='<%# "~/abc" + Mid(Eval("formid"), 4, 1) + ".aspx" %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
This is my final code which is working successfully :
<asp:GridView ID="child" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:TemplateField HeaderText="Form ID" >
<ItemTemplate>
<asp:Hyperlink runat="server" Text='<%# Eval("formid") %>'
NavigateUrl='<%# Eval("formid","~/abc" + Mid(Eval("formid"), 4, 1) + ".aspx?formid={0}") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Credits to #mason and #Tony L. for helping me out on this issue.

imagefield clickable in a gridview

i have this imagefield in a gridview
<asp:ImageField HeaderText="Image" DataImageUrlField="Image_Path" ControlStyle-Width="50" ControlStyle-Height="50"/>
the image_path is a column name from the database that retuns the image link
i need to make this field clickable so once i click on it i need to popup a form just like this field:
<asp:TemplateField HeaderText="View">
<ItemTemplate>
<asp:LinkButton ID="LinkButton3" CommandName="Select" onClick="popup_click" class="table-actions-button ic-table-edit" runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
i tried to put an image button instead of an image field but i'm always getting submit query instead of the image...
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" DataImageUrlFormatString="Image_Path" ControlStyle-Width="50" ControlStyle-Height="50"/>
</ItemTemplate>
does anyone know how can i fix this?

passing a querystring from one of the griditems bound columns

I have a gridview, one of the item in the gridview is
<asp:GridBoundColumn DataField="Id" UniqueName="Id" DataType="System.Int32" Visible="false"></asp:GridBoundColumn>
and another item is
<asp:ImageButton id="RadButton_RunQuery" ImageUrl="~/images/run_query_button.jpg" PostBackUrl="~/Viewer/ViewerSummary.aspx?QueryID=" runat="server" />
want to pass Id from the asp:GridBoundColumn as a querystring to the postbackurl of the asp:imageButton.
How can I achieve this?
You can try with this code
PostBackUrl='<%# "~/Viewer/ViewerSummary.aspx?QueryID=" + DataBinder.Eval(Container.DataItem,"ID") %>'
Since you are using an ImageButton then that means you are using an ItemTemplate. You can do something like this:
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton id="RadButton_RunQuery" ImageUrl="~/images/run_query_button.jpg"
PostBackUrl='<%#string.Format("~/Viewer/ViewerSummary.aspx?QueryID={0}",Eval("Id")) %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>

Displaying an image icon in a gridview column with a href value in asp.net

I have a gridview column in which I have one column that point to a pdf file on the file server. I need to have another column right beside this, displaying the pdf icon. The user should be able to click the icon and launch the file from the file server.
My code is:
<asp:GridView ID="gvInvoices" AutoGenerateColumns="false" runat="server" Font-Names="Arial" Font-Size="Small" Width="50%">
<Columns>
<asp:TemplateField HeaderText="File Type">
<ItemTemplate><img runat="server" src="Images/img_Pdf.gif" id="imgFileType" /></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate><%#Eval("InvoiceNumber")%></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
How can I add a "NavugateURL" or href="" to the img ?
Rather than using an HTML img control, try using an ASP.Net Server control.
A good choice would be the Hyperlink Control:
Instead of:
<img runat="server" src="Images/img_Pdf.gif" id="imgFileType" />
Use:
<asp:HyperLink ID="imgFileType" ImageUrl="Images/img_Pdf.gif" NavigateUrl="" runat="server">HyperLink</asp:HyperLink>
Just set your NavigateUrl property.
you need to wrap your icon around an anchor tag, and set the href of the anchor tag using DataBinding expression Eval. This assumes your Datasource field "PDFPath" is an absolute path.
<asp:GridView ID="gvInvoices" AutoGenerateColumns="false" runat="server" Font-Names="Arial" Font-Size="Small" Width="50%">
<Columns>
<asp:TemplateField HeaderText="File Type">
<ItemTemplate><a href='<%#Eval("PDFPath")%>'> <img runat="server" src="Images/img_Pdf.gif" id="imgFileType" /></a></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate><%#Eval("InvoiceNumber")%></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
inside the RowDataBind event look for your image control and set its NavigateUrl property
protected void gvInvoices_RowDataBound(object Sender , GridViewRowEventArgs e)
{
if(e.Row.RowType==DataRow)
{
HtmlControl icon = e.Row.FindControl("imgFileType") as HtmlControl;
icon.NavigateUrl = ((MyDataType)e.Row.DataRow).PDFPath;
}
}
Note it is free hand writing so you may find some syntax errors that you should fix

Gridview : Hyperlink and description in the same column cell

Apologies for the newbie question. My client wishes me to make a small change to the gridview on his http://www.flogitdonegal.com/SearchPage.aspx page.
Note the way the Title column is a hyperlink to view more information. This comes from a 'BriefDescription' field in the database.
How can I add 250 chars from the 'FullDescription' underneath the Title in the same cell, but I dont want it be a hyperlink.
Essentially it will be 2 fields coming into the same column.
Thanks in advance for all help.
John
If this is using a GridView you are most likely using a TemplateField as it is to display the HyperLink.
Within the ItemTemplate of the TemplateField you can specify an additional Label underneath using something as follows:
<asp:Label runat="server" id="FullDescLabel" Text='<%# DataBinder.Eval(Container.DataItem, "FullDescription") %>' />
You need to use the TemplateField and here is a tutorial that explains some of the other fields that GridView offers as well.
<asp:GridView ID="gvwCompounds" runat="server" DataSourceID="objItemsFromYourDB">
<Columns>
....
<asp:TemplateField>
<ItemTemplate HeaderText="Title">
<asp:HyperLink runat="server" ID="Hperlink1" NavigateUrl='<%# Eval("BriefDescriptionUrl") %>' Text='<%# Eval("BriefDescription") %>' />
<br />
<asp:Label runat="server" ID="Label1" Text='<%# Eval("FullDescription") %>' />
</ItemTemplate>
</asp:TemplateField>
....
</Columns>
</asp:GridView>

Resources