Clickable link in RadGrid column - telerik

I have a RadGrid where a column in the grid holds a URL. When a put a value in the column I can see the URL but the URL is not clickable (to go to the URL). How can I make the URL clickable?
Here is a rough example of what I'm doing now:
DataTable table = new DataTable();
DataRow row = table.Rows[0];
row["URL"] = "http://www.google.com";
grid.DataSource = table;
In addition I'd really like to show specific text instead of the URL. Something similar to Link in HTML. Is there anyway to do this?

Have you tried the GridHyperLinkColumn? Below is a detailed example.
<telerik:GridHyperLinkColumn FooterText="HyperLinkColumn footer" DataTextFormatString="Search Google for '{0}'" DataNavigateUrlFields="CompanyName" UniqueName="CompanyName" DataNavigateUrlFormatString="http://www.google.com/search?hl=en&q={0}&btnG=Google+Search" HeaderText="HyperLink<br/>Column" DataTextField="CompanyName"></telerik:GridHyperLinkColumn>
You can also view the demosite to see how it works.
http://demos.telerik.com/aspnet-ajax/grid/examples/generalfeatures/columntypes/defaultcs.aspx

Add all the columns manually in the ascx page and make the column you want to contain the hyperlink a GridTemplateColumn:
<telerik:GridTemplateColumn
UniqueName="TemplateLinkColumn"
AllowFiltering="false"
HeaderText="URL">
<ItemTemplate>
<asp:HyperLink ID="Link" runat="server"></asp:HyperLink>
</ItemTemplate>
</telerik:GridTemplateColumn>
Make sure that your grid has an OnItemDataBound method:
<telerik:RadGrid
ID="RadGrid"
runat="server"
AutoGenerateColumns="False"
OnItemDataBound="RadGrid_ItemDataBound" >
In your OnItemDataBound method set the field to the URL:
protected void RadGrid_ItemDataBound(object aSender, GridItemEventArgs anEventArgs)
{
//Get the row from the grid.
GridDataItem item = anEventArgs.Item as GridDataItem;
GridTableCell linkCell = (GridTableCell)item["TemplateLinkColumn"];
HyperLink reportLink = (HyperLink)reportLinkCell.FindControl("Link");
// Set the text to the quote number
reportLink.Text = "Google";
//Set the URL
reportLink.NavigateUrl = "http://www.google.com";
//Tell it to open in a new window
reportLink.Target = "_new";
}

You will also need to check for the correct type, as follows;
if (anEventArgs.Item.GetType().Name != "GridDataItem")
{
return;
}

Related

Setting SelectedValue in a Telerik DropDownList in a RadGrid

I am trying to set the SelectedValue of a RadDropDownList in the EditTemplate of my RadGrid. The DataItemBound event appears to be throwing an error on compilation.
ASP.NET
<telerik:GridTemplateColumn DataField="givenAnswer" HeaderText="givenAnswer" UniqueName="givenAnswer">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "givenAnswer") %>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadDropDownList ID="ddlGivenAnswer" runat="server" OnItemDataBound="ddlGivenAnswer_DataBound">
<Items>
<telerik:DropDownListItem Text="Yes" Value="Yes" />
<telerik:DropDownListItem Text="No" Value="No" />
</Items>
</telerik:RadDropDownList>
</EditItemTemplate>
</telerik:GridTemplateColumn>
C#
protected void ddlGivenAnswer_DataBound(object sender, GridItemEventArgs e)
{
if ((e.Item.IsInEditMode))
{
GridEditFormItem item = (GridEditFormItem)e.Item;
RadDropDownList ddl = (RadDropDownList)item.FindControl("ddlgivenAnswer");
ddl.SelectedValue = (string)DataBinder.Eval(e.Item.DataItem, "givenAnswer").ToString();
}
}
Error
CS0123: No overload for 'ddlGivenAnswer_DataBound' matches delegate 'DropDownListItemEventHandler'
This error is being throw on the telerik:RadDropDownList open tag line in ASP.NET. What am I missing here?
Main Edit:
Error CS0123:
First typing CS0123 in Google show me that you were using wrong parameter for your event. Probably a copypast fail. Delete the even in the aspx and ask to intelisense to create a new one. Or copypast this one.
protected void ddlGivenAnswer_ItemDataBound(object sender, Telerik.Web.UI.DropDownListItemEventArgs e)
ItemDataBound:
ItemDataBound occure when a data is bound in a control.
I'am pretty sure that inline declaration are not going to fire this event.
Minor Misconception:
Why would someone change the Value of a selected element dynamically?
It's like changing the value of a vote without changing the vote him self or the name on the vote.
What you want is to check the rigth item.
To check Waldo in the drop down list :
ddlGivenAnswer.FindItemByValue("Waldo").Selected = true;
To check the right Item:
ddlGivenAnswer.FindItemByValue(
DataBinder.Eval(e.Item.DataItem, "givenAnswer").ToString()
).Selected = true;

display the selected value from DropDown in a page

Hi Folks i am retrieving Name from database to drop down list. Now i want to display the selected value from DropDown in a page. My code is like follows to retrieve data,am using Fluent data as a framework.
My Controller Action is
var query = Abc.GetProducts();
ViewBag.EnterpriseId = new SelectList(query.AsEnumerable(), "EnterpriseId", "Name");
return View();
My view is ,
#Html.DropDownList("EnterpriseID", (SelectList) ViewBag.EnterpriseID, "--Select Resource--")
You can use a simple js for this:
elem = $('.selectClass');
options = elem.find('option[value="'+DataFromDBHere+'"]');
if(options){
options.prop('selected', 'selected');
}

how to link one visualforce page from another visualforce page?

I want to add two numbers that are given in a vf page text boxes. I want to print the result in another vf page by clicking the button (add button) in first page.
You can do this by routing to the next page in Visualforce and passing the value you want to display as a parameter. Something like this in Apex should work:
PageReference gotoPage()
{
PageReference pr = Page.YourSecondVFPage;
pr.getParameters().put('secondnumber', '' + this.secondNumber);
pr.setRedirect(true);
return pr;
}
Then in Visualforce:
<apex:commandButton value="Go!" action="{!gotoPage}"/>
I think you might want to try using the same controller on both pages. That way you don't have to pass all that data, assuming you have more than just a couple of fields to maintain, as url parameters or have to store the data somewhere if you don't have to.
If both pages are using the same controller and when you redirect the user using a PageReference make sure to set the redirect value to false, otherwise Salesforce treats as a new request and starts the page with a new instance of the controller.
The sample code below lets the user type some text into a field and then when print view is clicked it renders a PDF using that same information. No data was saved to database.
Controller
public with sharing class MyController {
public string MyData {get;set;}
public MyController() {
MyData = '';
}
public PageReference printView() {
PageReference oPageRef = Page.MyPage2;
oPageRef.setRedirect(false);
return oPageRef;
}
}
Page 1:
<apex:page controller="MyController">
<apex:sectionheader title="Test" subtitle="My Page"/>
<apex:form id="formData">
<apex:pageBlock title="My Data">
<apex:pageBlockButtons >
<apex:commandButton value="Printable View" action="{!printView}" />
</apex:pageBlockButtons>
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
<apex:outputLabel value="My Data"/>
<apex:inputTextArea value="{!MyData}" />
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Page 2:
<apex:page controller="MyController"
renderAs="pdf">
{!MyData}
</apex:page>

Open a popup / new window from a gridview - VS2010, c#

Can anyone give me any hint on how I can open a new window by selecting a record from a gridview?
Im using response.redirect, and passing a value at present which opens the page fine but Id rather have a popup for this
thanks
You need to bind click event with the row of the grid view control
following code is just demo for you
following javascript open up the pop up window where you need to pass the url you want
<script>
function popWin(url){
window.open(url, '', '');");
}
</script>
follwing is samle code that attch the script with the gridview row to open up popup
code behind
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow )
{
string url = "www.google.com";
e.Row.Attributes.Add("onclick","popWin('" + url + "')");
}
}
I had the same issue with response.redirect so went with a asp:HyperLink button instead. I loaded the URL by Binding it through the SQL Database. Here is the code:
<asp:TemplateField HeaderText="Google Map">
<ItemTemplate>
<asp:HyperLink runat="server" ID="hlGMap" Target="_blank" Text="Map" ImageUrl="~/gfx/google.png" NavigateUrl='<%# Bind("GoogleMapsURL") %>' />
</ItemTemplate>
</asp:TemplateField>
Hope it helps.

Conditional Item Templates with RadComboBox

I have a RadComboBox that I am using to display department name and abbreviations. I am using an Item Template with a LinqDataSource to make each item appear as:
DeptAbbr - (DeptName)
Here is the code I am using to do this and it works fine:
<telerik:RadComboBox ID="rcbDepartments" runat="server" AppendDataBoundItems="True"
OnInit="rcbDepartments_Init" DataTextField="DepartmentAbbr" AutoPostBack="True"
DataSourceID="ldsDepartments" DataValueField="DepartmentID" HighlightTemplatedItems="true"
NoWrap="true" Width="250px">
<ItemTemplate>
<div>
<b>
<%# Eval("DepartmentAbbr")%></b><%# Eval("DepartmentName", " - ({0})") %>
</div>
</ItemTemplate>
</telerik:RadComboBox>
My question is this. I want to add an initial item in the list that is for "All Departments" and is the default item. I can do this easily, but the problem I'm having is that because I am not storing an "All Departments" entry in the database, the templating shows a blank space at the beginning of the items list when you pull down the combo box. I'm trying to find out if there is any way to template all but first item in the list?
Note: I have also tried do a conditional in the Eval like this:
<b><%# (Eval("DepartmentAbbr") != null) ? Eval("DepartmentAbbr") : "All Departments" %></b><%# Eval("DepartmentName", " - ({0})") %>
But it only evaluates on the items that are databound and not the initial item which I am sticking in manually. In other words, if I change the above statement to be:
<b><%# (Eval("DepartmentAbbr") == null) ? Eval("DepartmentAbbr") : "All Departments" %></b><%# Eval("DepartmentName", " - ({0})") %>
Then I just get a list with one blank item at the top and the rest reading "All Departments".
My work around for this problem has been to do some funky selection stuff with LINQ server side, but that has forced me to get rid of all templating and html formatting.
You can define the 'All Departments' RadComboBoxItem as a static item in the <Items> collection. Since you have enabled the AppendDataBoundItems property, you don't want to bind to your data source until after the control has already bound the static items; otherwise you'll get the blank space you are seeing when expanding the combo box. Also, use DataBinder.Eval(Container, "Text") to render the DepartmentAbbr field. Since you have set this field as the DataTextField for the control, that value will always render. If not, you'll get the empty space again when the control binds to the static item because it doesn't know what DepartmentAbbr is; it only has a Text field. Here's an example to get you going:
<telerik:RadComboBox ID="RadComboBox1" runat="server"
AppendDataBoundItems="True"
DataTextField="Abbr"
AutoPostBack="True"
DataValueField="DeptID"
HighlightTemplatedItems="true"
NoWrap="true"
Width="250px">
<Items>
<telerik:RadComboBoxItem runat="server" Text="All Departments" />
</Items>
<ItemTemplate>
<div>
<b><%# DataBinder.Eval(Container, "Text")%></b><%# Eval("Name", " - ({0})") %>
</div>
</ItemTemplate>
</telerik:RadComboBox>
public partial class _Default : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
RadComboBox1.Load += new EventHandler(RadComboBox1_Load);
}
protected void RadComboBox1_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Ensure the static items are already bound before assigning
// new data to the DataSource property
RadComboBox1.DataBind();
var departments = new[] {
new { DeptID = 1, Abbr = "ACME", Name = "ACME Corporation" },
new { DeptID = 2, Abbr = "MSFT", Name = "Microsoft Corporation" },
new { DeptID = 3, Abbr = "GOOG", Name = "Google, Inc" }
};
RadComboBox1.DataSource = departments;
RadComboBox1.DataBind();
}
}
}
Hope that helps!

Resources