11 Answers, 1 is accepted
In the case of adding controls to the cells of GridBoundColumn,you cannot use ItemCreated only, but a combination of both ItemCreated and ItemDataBound.Here is the sample code.
C#:
protected
void
RadGrid1_ItemCreated(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
TableCell cell = (TableCell)item[
"UniqueName"
];
HyperLink hyperLink=
new
HyperLink ();
hyperLink.Text = cell.Text;
hyperLink.NavigateUrl =
"page.aspx"
;
hyperLink.ID =
"hyperLink1"
;
cell.Controls.Add(hyperLink);
}
}
protected
void
RadGrid1_ItemDataBound(
object
sender, Telerik.Web.UI.GridItemEventArgs e)
{
if
(e.Item
is
GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
TableCell cell = (TableCell)item[
"UniqueName"
];
HyperLink hyperLink=
new
HyperLink ();
hyperLink.Text = cell.Text;
hyperLink.NavigateUrl =
"page.aspx"
;
hyperLink.ID =
"hyperLink1"
;
cell.Controls.Add(hyperLink);
}
}
Thanks,
Shinu.
How can i set a custom text for the AutoGeneratedEdit column in my RadGrid. Please provide a solution as soon as possible.
Thanks,
Ryan
Please try the following code snippet.
C#:
protected
void
RadGrid1_ItemDataBound(
object
sender, Telerik.Web.UI.GridItemEventArgs e)
{
if
(e.Item
is
GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
((LinkButton)item[
"AutoGeneratedEditColumn"
].Controls[0]).Text =
"Your text"
;
}
}
Thanks,
Shinu.
hi shinu
your code works greats for me since the column type is 'GridBoundColumn' but when its comes to 'GridTemplateColumn' which i used rad combobox in it do not work. how can add hyperlink to control which i explained above.
thanks in advance
sarami
When using GridTemplateColumn, the controls in it have a known ID defined by the developer. Since the IDs are known, the developer can search for the control by its ID rather than trying to accesses the first control of the Controls Collection.
Example code for finding a control:
var SomeControl = dataItem[
"DocumentUploadLink"
].FindControl(
"SomeControlID"
);
Please take a look at the Accessing Cells and Rows article. There you can find more details on accessing components of the grid, get values of cells and also finding controls in them.
Kind regards,
Attila Antal
Progress Telerik
Hi Attila
And Thanks For Your Quick replay
would you mind to correcting me due to below code:
<
telerik:GridTemplateColumn
HeaderText
=
"QuotationNO"
HeaderStyle-Width
=
"250px"
UniqueName
=
"QuotationNo"
>
<
ItemTemplate
>
<%#DataBinder.Eval(Container.DataItem, "QuotationName")%>
</
ItemTemplate
>
<
EditItemTemplate
>
<
telerik:RadComboBox
RenderMode
=
"Lightweight"
runat
=
"server"
ID
=
"RadComboBox2"
EmptyMessage
=
"......"
UniqueName
=
"QuotationNo"
DataTextField
=
"QuotationName"
Skin
=
"Default"
OnTextChanged
=
"RadComboBox2_TextChanged"
Height
=
"140px"
Width
=
"140px"
DataValueField
=
"QuotationID"
DataSourceID
=
"RepeatQoutationDatasource"
SelectedValue='<%#Bind("QuotationID")%>'>
</
telerik:RadComboBox
>
</
EditItemTemplate
>
<
HeaderStyle
Width
=
"190px"
></
HeaderStyle
>
<
ItemStyle
Width
=
"190px"
></
ItemStyle
>
</
telerik:GridTemplateColumn
>
and my VB code
Dim
item
As
GridDataItem =
CType
(e.Item, GridDataItem)
Dim
cell
As
TableCell =
CType
(item(
"QuotationNo"
), TableCell)
Dim
hyperLink1
As
HyperLink =
New
HyperLink()
'
Dim
value = DataBinder.Eval(item.DataItem,
"Autoid"
)
hyperLink1.Text = DataBinder.Eval(item.DataItem,
"QuotationName"
)
hyperLink1.NavigateUrl =
"PDFLoading.aspx?AutoID=B"
& value &
"&TYPE=Quotation"
hyperLink1.Target =
"Blank"
hyperLink1.ID =
"hyperLink1"
cell.Controls.Add(hyperLink1)
after triger Above Code i am getting Double text in Quotation cell which one of them is hyperlinked .
thanks in advance
sarami
I am afraid I will need some more information to see and understand what is happening in the grid.
You can find attached a sample WebForms page with a Grid and template column. I would like you to modify this sample to produce the error share it with us. Once we can see the error we would be able to tell you how to fix it.
Kind regards,
Attila Antal
Progress Telerik
Hi Attila
I added a "XML" file to project and customized grid to make it looks same as i want .
i am getting Double text in Quotation cell which one of them is hyperlinked .
i am not able to attach zip file so please kindly get it from here...(Download)
thanks in advance
sarami
Thank you for the sample. Now I can see the reason for having the text displayed twice. Besides the text evaluated in the ItemTemplate, a Hyperlink is being added into the cell additionally.
The following Evaluation displays the text:
<
telerik:GridTemplateColumn
HeaderText
=
"QuotationNO"
HeaderStyle-Width
=
"250px"
UniqueName
=
"QuotationNo"
>
<
ItemTemplate
>
<%#DataBinder.Eval(Container.DataItem, "ShipName")%>
</
ItemTemplate
>
</
telerik:GridTemplateColumn
>
Then, an additional link is added to the cell in the code behind:
Protected
Sub
RadGrid1_ItemDataBound(sender
As
Object
, e
As
GridItemEventArgs)
If
TypeOf
e.Item
Is
GridDataItem
Then
Dim
item
As
GridDataItem =
CType
(e.Item, GridDataItem)
Dim
cell
As
TableCell =
CType
(item(
"QuotationNo"
), TableCell)
Dim
hyperLink1
As
HyperLink =
New
HyperLink()
'
Dim
value = DataBinder.Eval(item.DataItem,
"OrderID"
)
hyperLink1.Text = DataBinder.Eval(item.DataItem,
"ShipName"
)
hyperLink1.NavigateUrl =
"PDFLoading.aspx?AutoID=B"
& value &
"&TYPE=Quotation"
hyperLink1.Target =
"Blank"
hyperLink1.ID =
"hyperLink1"
cell.Controls.Add(hyperLink1)
End
If
End
Sub
Since you are using TemplateColumn, you can place a Hyperlink directly in the ItemTemplate of the column. This way you will not need to add it on server-side.
<
telerik:GridTemplateColumn
HeaderText
=
"QuotationNO"
HeaderStyle-Width
=
"250px"
UniqueName
=
"QuotationNo"
>
<
ItemTemplate
>
<
asp:HyperLink
ID
=
"HyperLink1"
runat
=
"server"
Target
=
"_blank"
NavigateUrl='<%# "PDFLoading.aspx?
AutoID
=
B
" & Eval("OrderID") & "&
TYPE
=
Quotation
" %>'><%# Eval("ShipName") %></
asp:HyperLink
>
</
ItemTemplate
>
</
telerik:GridTemplateColumn
>
Result:
The links will point to the following address: http://localhost:60039/PDFLoading.aspx?AutoID=B8&TYPE=Quotation
Kind regards,
Attila Antal
Progress Telerik
hi attila and thanks you bud.
what about if i want to do it with code behind server side code i mean , cuz in my case i need to check some database matter and then add hyperlink to column ,thats mean the hyperlink is conditional and some of row dosnt have hyperlink even
thanks in advance
sarami
You can achieve this requirement using the ItemDataBound event where you can access the generated control using the FindControl() approach:
https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/rows/accessing-cells-and-rows#accessing-controls-in-editinsert-mode
Then, you can set the Visible property of the Hyperlink control or modify it per your own preference.
In addition, I am also sending a sample RadGrid web site to demonstrated different ways of creating link columns.
Regards,
Eyup
Progress Telerik