
Richard Weeks
Top achievements
Rank 2
Richard Weeks
asked on 09 Sep 2010, 12:20 AM
Hi,
For this:
On firing, OnItemDataBound, this works:
But this does not (Label is null):
I can't see the wood for the trees, I think. Why can't I get a reference to the label?
Thanks,
Richard
For this:
<
telerik:GridTemplateColumn
UniqueName
=
"Value"
HeaderText
=
"Values"
>
<
HeaderTemplate
>
<
asp:Label
Text
=
"Value"
runat
=
"server"
>
</
asp:Label
>
</
HeaderTemplate
>
<
ItemTemplate
>
<
asp:Label
ID
=
"CurrentValues"
runat
=
"server"
Text='<%# Eval("Values") %>'>
</
asp:Label
>
</
ItemTemplate
>
<
EditItemTemplate
>
<
telerik:RadListBox
ID
=
"Values"
runat
=
"server"
DataTextField
=
"ValueTitle"
DataValueField
=
"ValueId"
>
</
telerik:RadListBox
>
</
EditItemTemplate
>
</
telerik:GridTemplateColumn
>
On firing, OnItemDataBound, this works:
var gridEditFormItem = (GridEditFormItem)e.Item;
var radListBox = (RadListBox)gridEditFormItem[
"Value"
].FindControl(
"Values"
);
But this does not (Label is null):
var label = (Label)gridEditFormItem[
"Value"
].FindControl(
"CurrentValues"
);
I can't see the wood for the trees, I think. Why can't I get a reference to the label?
Thanks,
Richard
8 Answers, 1 is accepted
0

Shinu
Top achievements
Rank 2
answered on 09 Sep 2010, 05:57 AM
Hello Richard,
Since your the label control is inside the item template ,you can try the following code to access the label control.
C#:
Regards,
Shinu.
Since your the label control is inside the item template ,you can try the following code to access the label control.
C#:
if
(e.Item
is
GridDataItem)
{
GridDataItem item=(GridDataItem)e.Item;
var label = (Label)item[
"Value"
].FindControl(
"CurrentValues"
);
label.Text =
"test"
;
}
Regards,
Shinu.
0

Richard Weeks
Top achievements
Rank 2
answered on 09 Sep 2010, 06:46 AM
Hi, thanks, I can get a reference using that approach.
However, I want to get the text from the label and use it to populate a multi-select listbox and I can't seem to get the two to co-exist.
Here's an example:
protected
void
MyGrid_OnItemDataBound(
object
sender, GridItemEventArgs e)
{
if
((e.Item
is
GridEditFormItem) && e.Item.IsInEditMode)
{
var gridEditFormItem = (GridEditFormItem)e.Item;
var radListBox = (RadListBox)gridEditFormItem[
"Value"
].FindControl(
"Values"
);
var valueData =
new
ValueData();
var values = valueData.Select();
TelerikHelper.BindRadListBox(radListBox, values,
false
);
}
if
(e.Item
is
GridDataItem)
{
GridDataItem gridDataItem = (GridDataItem)e.Item;
var label = (Label)gridDataItem[
"Value"
].FindControl(
"CurrentValues"
);
// label.Text = "Category 1,Category 2"
/* I want to do this...
*
var radListBox = (RadListBox)someItem["Value"].FindControl("Values");
string[] currentValues = label.Text.Split(',');
foreach (var value in currentValues)
{
radListBox.FindItemByText(value).Selected = true;
}
*/
}
}
Seems so simple, doesn't it? :) Any help in merging the two together would be great.
Thanks,
Richard
0

Princy
Top achievements
Rank 2
answered on 09 Sep 2010, 08:36 AM
Hello,
You can access the values using following code and select the corresponding items in RadListBox.
CS:
Thanks,
Princy.
You can access the values using following code and select the corresponding items in RadListBox.
CS:
protected
void
RadGrid1_ItemDataBound(
object
sender, GridItemEventArgs e)
{
if
((e.Item
is
GridEditFormItem) && e.Item.IsInEditMode)
{
var gridEditFormItem = (GridEditFormItem)e.Item;
RadListBox radListBox = (RadListBox)gridEditFormItem[
"Value"
].FindControl(
"Values"
);
// Get the value
string
values = gridEditFormItem.ParentItem[
"Values"
].Text;
// Your logic to select the ListItems
}
. . .
}
Thanks,
Princy.
0

Richard Weeks
Top achievements
Rank 2
answered on 10 Sep 2010, 12:06 AM
Hi Princy, I already tried that but ParentItem is null and throws an exception:
It's driving me crazy! I'm sure it must be something simple I overlooked but I can't think what it could be.
Richard
System.NullReferenceException was unhandled by user code
Message="Object reference not set to an instance of an object."
Source="My.NameSpace"
StackTrace:
at My.NameSpace..InvestigationsGrid_OnItemDataBound(Object sender, GridItemEventArgs e) in C:\FilePath\File.aspx.cs:line 457
at Telerik.Web.UI.RadGrid.OnItemDataBound(GridItemEventArgs e)
at Telerik.Web.UI.RadGrid.CallOnItemDataBound(GridItemEventArgs e)
at Telerik.Web.UI.GridEditFormItem.SetupItem(Boolean dataBind, Object dataItem, GridColumn[] columns, ControlCollection rows)
at Telerik.Web.UI.GridTableView.CreateInsertItem(Boolean useDataSource, GridEnumerableBase resolvedDataSource, ControlCollection rows)
at Telerik.Web.UI.GridTableView.CreateTopInsertItem(Boolean useDataSource, GridEnumerableBase resolvedDataSource, GridTHead tHead)
at Telerik.Web.UI.GridTableView.CreateControlHierarchy(Boolean useDataSource)
at Telerik.Web.UI.GridTableView.CreateChildControls(IEnumerable dataSource, Boolean useDataSource)
at System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data)
at System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data)
at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)
at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
at Telerik.Web.UI.GridTableView.PerformSelect()
at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
at Telerik.Web.UI.GridTableView.DataBind()
at Telerik.Web.UI.GridTableView.Rebind()
at Telerik.Web.UI.GridTableView.InsertItem(IDictionary newValues)
at My.NameSpace..InsertEmptyRecord(String radGridId, GridCommandEventArgs e) in C:\FilePath\File.aspx.cs:line 1671
at My.NameSpace..Grid_OnItemCommand(Object source, GridCommandEventArgs e) in C:\FilePath\File.aspx.cs:line 251
at Telerik.Web.UI.RadGrid.OnItemCommand(GridCommandEventArgs e)
at Telerik.Web.UI.RadGrid.OnBubbleEvent(Object source, EventArgs e)
at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
at Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e)
at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
at Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e)
at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
at System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
It's driving me crazy! I'm sure it must be something simple I overlooked but I can't think what it could be.
Richard
0
Hello Richard,
I tested the approach offered by Princy in a small sample project and it works fine with a column definition similar to yours. Could you please take a look at it and let me know what am I missing out from your scenario?
Also, if you still cannot find a solution, you could send us your code through a formal support ticket, so we can take a closer look at your logic and find out what causes the erroneous behavior faster.
Regards,
Tsvetina
the Telerik team
I tested the approach offered by Princy in a small sample project and it works fine with a column definition similar to yours. Could you please take a look at it and let me know what am I missing out from your scenario?
Also, if you still cannot find a solution, you could send us your code through a formal support ticket, so we can take a closer look at your logic and find out what causes the erroneous behavior faster.
Regards,
Tsvetina
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0

Richard Weeks
Top achievements
Rank 2
answered on 16 Sep 2010, 12:07 AM
In the end, the only way I could get it to work was as follows:
currentValues is just a comma delimited string that comes out from the database (e.g. "this,that,those").
It works really well for me and as the project is a big one with many other issues to address, I'm happy at that :)
Regards,
Richard
namespace
MyNamespace
{
public
partial
class
MyPage : System.Web.UI.Page
{
private
string
currentValues;
protected
void
ValuesGrid_OnItemDataBound(
object
sender, GridItemEventArgs e)
{
if
((e.Item
is
GridEditFormItem) && e.Item.IsInEditMode)
{
var gridEditFormItem = (GridEditFormItem)e.Item;
var radListBox = (RadListBox)gridEditFormItem[
"Value"
].FindControl(
"Values"
);
var valueData =
new
ValueData();
var values = valueData.Select();
TelerikHelper.BindRadListBox(radListBox, values,
false
);
if
(
this
.currentValues !=
null
)
{
radListBox = (RadListBox)gridEditFormItem[
"Value"
].FindControl(
"Values"
);
string
[] currentValues =
this
.currentValues.Split(
','
);
foreach
(var value
in
currentValues)
{
radListBox.FindItemByText(value).Selected =
true
;
}
}
}
if
(e.Item
is
GridDataItem)
{
if
(e.Item.OwnerTableView.IsItemInserted)
{
this
.currentValues =
null
;
}
else
{
var gridDataItem = (GridDataItem)e.Item;
var label = (Label)gridDataItem[
"Value"
].FindControl(
"CurrentValues"
);
if
(label !=
null
)
{
this
.currentValues = label.Text;
}
}
}
}
}
}
currentValues is just a comma delimited string that comes out from the database (e.g. "this,that,those").
It works really well for me and as the project is a big one with many other issues to address, I'm happy at that :)
Regards,
Richard
0

Sonny
Top achievements
Rank 1
answered on 17 Jun 2014, 09:34 PM
How would you do this when the grid's EditMode="Batch" and with a TextBox?
0

Shinu
Top achievements
Rank 2
answered on 18 Jun 2014, 06:24 AM
Hi Sonny,
Please take a look into this help documentation which discuss about the method of accessing ItemTemplate and EditItemTemplate control of RadGrid in Batch edit mode.
Thanks,
Shinu.
Please take a look into this help documentation which discuss about the method of accessing ItemTemplate and EditItemTemplate control of RadGrid in Batch edit mode.
Thanks,
Shinu.