asp.net HyperLinkField Has no ToolTip property (Alt text) - asp.net

I wish there was a ToolTip field in HyperLinkField as there is one in HyperLink.
I'm creating a HyperLinkField by code before binding to my data source:
HyperLinkField hl = new HyperLinkField();
hl.DataNavigateUrlFields = new string[] { "col" };
hl.DataNavigateUrlFormatString = "{0}";
hl.DataTextField = "Foo";
Is there any way to also set a value to something which will render as a tooltip (or alt text)?
Any help will be appreciated.

That's correct, there's no tooltip/alt text property in a HyperlinkField. To get around this shortcoming, you need to use a template field and add a regular Hyperlink control.
<asp:TemplateField HeaderText="Href">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#
Eval("Href") %>' Text='<%# Eval("Href") %>' ToolTip='<%# Eval("Text") %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
However, doing this in a programmatic requires a lot of work. You need to create your own class that implements the ITemplate interface. Here's a tutorial on that.

Your requirement can be accompished in <asp:HyperlinkField> itself by adding tooltip for that specific cell in RowDataBound event of a GridView. After binding the GridView to your DataSource you can do this in a RowDataBound event as follows:
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].ToolTip = "Your tooltip text";
}
Though you have accepted another answer, my answer may be helpful for some other users!

Related

RadDatePicker in ASP.NET Gridview TemplateField

I've got a date picker field declared within a template field in a GridView as follows:
<asp:TemplateField HeaderText="Shipping Date">
<ItemTemplate>
<asp:Label ID="lblShippingDate" runat="server" CssClass="dnnFormLabel"
AssociatedControlID="ShippingDatePicker" />
<dnn:DnnDatePicker runat="server" ID="ShippingDatePicker" />
</ItemTemplate>
</asp:TemplateField>
I can select the date just fine and it appears in my label control - I then click on a standard asp:Button which finds the selected row and despite it finding all the other controls on the row including the label control displaying the selected date, the label's Text attribute is blank:
var txtShippingDate = row.Cells[7].FindControl("lblShippingDate") as Label;
Please note that I'm using the DotNetNuke dnn:DnnDatePicker control but this is actually a RadDatePicker under the covers.
Can anyone suggest how I can successfully read the value?
Thanks for looking :)
Try following after you find label:
if(txtShippingDate != null) {
string date = Request.Form[txtShippingDate.UniqueID].ToString();
}

Using hyperlink with querystring for gridview row

Is there someway to turn the row of a gridview into a hyperlink so that when a user opens it in a new tab for example, it goes to that link? Right now I am using a LinkButton and when the user opens it in a new tab, it doesn't know where to go.
I figured the .aspx code would look something like:
<asp:TemplateField>
<ItemTemplate>
<Hyperlink ID="hyperlink" runat="server" ForeColor="red" HtmlEncode="false" navigationURL="testUrl.aspx"
</ItemTemplate>
</asp:TemplateField>
The only thing is, our URLs are set up in the C# code behind as a query string, so I'm not sure how to pass that into the navigationURL section.
I'm guessing there's something I can do on the page_load with the query string to redirect to the page I need, but this is my first time working with query strings so I'm a little confused.
Thanks!
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#String.Format("~/controller.aspx?routeID1={0}&routeID2={1}", Eval("routeid1"), Eval("routeid2"))%>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
routeid1 and routeid2 are passed as query strings to the controller of that page.
What I did recently is modified my class to have a readonly property that constructs the A tag for me. This way I have control over what gets displayed; just text or a link.
<ItemTemplate>
<asp:Label ID="ColumnItem_Title" runat="server" Text='<%# Bind("DownloadATag") %>'> </asp:Label>
</ItemTemplate>
The code behind just binds an instance of the class to the gridview. You can bind the gridview whenever, on load on postback event, etc.
Dim docs As DocViewList = GetViewList()
GridViewDocuments.DataSource = docs
GridViewDocuments.DataBind()
In the above code, the DocViewList, instantiated as docs, is a list of a class that has all the properties that are needed to fill my GridView, which is named GridViewDocuments here. Once you set the DataSource of your GridView, you can bind any of the source's properties to an item.
Something like:
<asp:LinkButton ID="LinkButton_Title" runat="server" target="_blank"
PostBackUrl='<%# Eval(Request.QueryString["title"]) %>'
or binding them from the RowCreated event:
protected void GridView_OnRowCreated(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
(e.Row.FindControl("LinkButton_Title") as LinkButton).PostBackUrl = Request.QueryString["title"]))
}
}

How to add a hyperlink to my gridview

I want to set hyperlink (on image) in my gridview. When user clicks on that hyperlink, a query string should be generated based on selected value of dropdown list. How to set the hyperlink in gridview and how to form query string for that hyperlink?
Thanks in advance..
You can simply Cancatinate the value of your dropdown to NavigateUrl property of hyperlink
<ItemTemplate>
<asp:HyperLink ID="hlEdit" runat="server"
NavigateUrl='<%# Eval("ID", "PageName.aspx?ID={0}" + "&TID=" + ddl.SelectedValue) %>'
ImageUrl="~/Images/edit.png"></asp:HyperLink>
</ItemTemplate>
Edit:
<ItemTemplate>
<asp:ImageButton ID="hlEdit" runat="server"
PostBackUrl='<%# Eval("ID", "PageName.aspx?ID={0}" + "&TID=" + ddl.SelectedValue) %>'
ImageUrl="~/Images/edit.png"></asp:ImageButton>
</ItemTemplate>
You will probably need javascript for this.
Add an 'onclick' attribute to your images
In the onclick handler, you retrieve the dropdownlist value and compose your query
Set the composed url to the href of your link
Some more detailed information would be useful to be able to provide you with some code..
Are you using an asp HyperLink, ImageButton, ...?
You could for example use the OnClientClick property in case you would be using an ImageButton.

NavigateUrl with NavigateUrlFormat populated from AppSettings (dynamically)

I have a gridview with a hyperlink inside an TemplateField. The NavigateUrlFormat will be populated from the Web.Config file, it has the following format:
mysite.com/{0}
Where {0} will be populated from the gridview datasource, however, my current code doesn't work:
<asp:TemplateField HeaderText="WorkOrder #">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("WorkOrderKey", "<%$ AppSettings:DispatchLink %>") %>'
Text='<%# Eval("WorkOrderKey") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
The problem is right after "AppSettings:DispatchLink", it closes the tag when it see "%>". Is there a way around this problem?
Thank you,
Kenny.
You cannot nest <%# and other tags. Use ConfigurationManager.AppSettings["DispatchLink"] (pls. lookup correkt Class/Property in MSDN) instead.
Or: If your are using that HyperLink Control more than once I would suggest that you create an own MYHyperLink derived from System.Web.UI.WebControls.HyperLink and set the Property NavigateUrlFormatString in your Constructor.
Something like this:
public class DispatchLink : System.Web.UI.WebControls.HyperLink
{
public DispatchLink()
{
this. NavigateUrlFormatString = ConfigurationManager.AppSettings["DispatchLink"]
}
}

ASP.NET GridView ItemTemplate

OK I have a GridView and there is a column that I want to be a link if a file exists, otherwise I just want it to be a label. Right now I am changing the controls on RowDataBound event handler using the Row passed in the args. I am not a big fan of this as I am hard coding the column ID, and if it ever changes I will need to remember to change this code. I was hoping I could do a conditional in the asp code to add a link if a property value is not null otherwise add a label. Is this possible? Any different solutions?
I would like something like this:
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<%# if (Eval("LogFileName") == null)
<%#{
<asp:LinkButton ID="LogFileLink" runat="server" CommandArgument='<% #Eval("LogFileName") %>' CommandName="DownloadLogFile" Text='<%# Blah.NDQA.Core.Utilities.GetEnumerationDescription(typeof(Blah.NDQA.Core.BatchStatus), Eval("Status")) %>'>
<%# }
<%# else
<%#{
<asp:Label ID="LogFileLabel" runat="server"Text='<%# Blah.NDQA.Core.Utilities.GetEnumerationDescription(typeof(Blah.NDQA.Core.BatchStatus), Eval("Status")) %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
You can continue to use RowDataBound event but in your aspx you add:
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
In your C# code something like that:
if (LogFileName) {
LinkButton ctrl = new LinkButton();
ctrl.CommandArgument= ...;
ctrl.CommandName= ...;
} else {
Label ctrl = new Label();
ctrl.Text= ...;
}
// You have to find the PlaceHolder1
PlaceHolder1.Controls.Add(ctrl);
In this way you don't have to hard coding the column ID
I know this is a little old now but just in case someone else stumbles across this as I did when looking for an answer to a similar question, I found you can do something like this:
<ItemTemplate>
<asp:ImageButton ID="btnDownload" runat="server"
CommandName="Download"
CommandArgument='<%# Eval("Document_ID") & "," & Eval("Document_Name") %>'
ImageUrl="download.png" ToolTip='<%#"Download " & Eval("Document_Name") %>'
Visible='<%# Not(Eval("Document_ID") = -1) %>' />
</ItemTemplate>
i.e. set the Visible property to evaluate a boolean expression based on your field. If you wanted to display something instead of the download link or button, such as a "Not available" label, then you would just set its Visible property to the opposite boolean expression to your download link. (This is VB.NET not C#, but you get the idea.)
If you're going to be doing this a lot, I suggest writing your own field. The simplest approach is probably to make a NullableHyperlinkField inheriting from HyperlinkField, and render out a plain string if the anchor's URL would otherwise be null.
Use properties on the page to determine if you want to show the label or the link
<asp:GridView ID="gv" runat="server">
<Columns>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:LinkButton runat="server" Visible='<%# ShowLink %>' PostBackUrl="~/Aliases.aspx" >This is the link</asp:LinkButton>
<asp:Label runat="server" Visible='<%# ShowLabel %>'>Aliases label</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The add the properties ShowLink and ShowLable toyour code behind
public bool ShowLabel
{
get
{
//determine if the label should be shown
return false;
}
private set
{
//do nothing
}
}
public bool ShowLink
{
get
{
//determine if the link should be shown
return true;
}
private set
{
//do nothing
}
}

Resources