5 Answers, 1 is accepted
0
Shinu
Top achievements
Rank 2
answered on 14 Jan 2010, 06:25 AM
Hi AkAlan,
I guess you want to hide the button placed in CommandItemTemplate if any of the value in column is null. If so you can try the following code snippet in order to achieve this.
CS:
-Shinu.
I guess you want to hide the button placed in CommandItemTemplate if any of the value in column is null. If so you can try the following code snippet in order to achieve this.
CS:
protected void RadGrid1_PreRender(object sender, EventArgs e) |
{ |
foreach (GridDataItem item in RadGrid1.MasterTableView.Items) |
{ |
if (item["Region"].Text == " ") // Check for null |
{ |
GridCommandItem commandItem = (GridCommandItem)RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)[0]; |
Button btn = (Button)commandItem.FindControl("Button4"); |
btn.Visible = false; |
break; |
} |
} |
} |
-Shinu.
0
AkAlan
Top achievements
Rank 2
answered on 14 Jan 2010, 07:14 PM
I tried your code (after converting it to vb) and I got an exception saying the index was out of range on this line:
Dim commandItem As GridCommandItem = DirectCast(RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)(0), GridCommandItem)
I Do in fact want a button hid but I misspoke in my first post, I want to check the field for not null. I'm going to post more code to make sure I am clear as to what is going on. I'm sure this is simple but I am new to Telerik so thanks for the help.
Here is the column I want to check during load and if there is a date in the field (indicating the Job is closed), I want to hide the button in the CommandItemTemplate button "btnCloseRwp" that is used to close Jobs.
Here is the CommandItemTemplate:
Dim commandItem As GridCommandItem = DirectCast(RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)(0), GridCommandItem)
I Do in fact want a button hid but I misspoke in my first post, I want to check the field for not null. I'm going to post more code to make sure I am clear as to what is going on. I'm sure this is simple but I am new to Telerik so thanks for the help.
Here is the column I want to check during load and if there is a date in the field (indicating the Job is closed), I want to hide the button in the CommandItemTemplate button "btnCloseRwp" that is used to close Jobs.
<telerik:GridTemplateColumn DataField="DateCompleted" |
DataType="System.DateTime" HeaderText="Date C/W" |
SortExpression="DateCompleted" UniqueName="DateCompleted" |
GroupByExpression="DateCompleted"> |
<ItemTemplate> |
<asp:TextBox ID="txtDateCompleted" Width="55px" SkinID="XSmallTextBoxLeft" runat="server" |
Text='<%# Eval("DateCompleted", "{0:dd MMM, yy}") %>'></asp:TextBox> |
</ItemTemplate> |
</telerik:GridTemplateColumn> |
Here is the CommandItemTemplate:
<CommandItemTemplate> |
<asp:Button ID="btnInsertWork" |
Text="Add Work" |
Runat="server" |
CommandName="InitInsert"> |
</asp:Button> |
<asp:Button ID="btnCloseJob" |
Text="Close Job" |
Runat="server" |
CommandName="CloseJob"> |
</asp:Button> |
</CommandItemTemplate> |
0
Hello Alan,
You can also wire the ItemDataBound event and set the visibility of the button in the following way:
As to the exception you get - could you verify whether you have set the MasterTableView.CommandItemDisplay property to value different than "none"?
Regards,
Martin
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
You can also wire the ItemDataBound event and set the visibility of the button in the following way:
Protected
Sub
RadGrid1_ItemDataBound(
ByVal
sender
As
Object
,
ByVal
e
As
GridItemEventArgs)
If
TypeOf
e.Item
Is
GridDataItem
Then
Dim
item
As
GridDataItem = TryCast(e.Item, GridDataItem)
Dim
textBox
As
TextBox = TryCast(item.FindControl(
"txtDateCompleted"
), TextBox)
If
textBox.Text.Length <> 0
Then
Dim
commandItem
As
GridCommandItem = TryCast(TryCast(sender, RadGrid).MasterTableView.GetItems(GridItemType.CommandItem)(0), GridCommandItem)
TryCast(commandItem.FindControl(
"btnCloseJob"
), Button).Visible =
False
End
If
End
If
End
Sub
As to the exception you get - could you verify whether you have set the MasterTableView.CommandItemDisplay property to value different than "none"?
Regards,
Martin
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Alexander
Top achievements
Rank 2
answered on 15 Nov 2013, 08:39 AM
Hi
I have a similar need and have not been able to create a solution that is to my satisfaction.
I want to change the commanditemtemplate content based on status and number of rows in the grid.
ie. :
No rows : Only insert command button should be visible, all others invisible.
Rows exist: Update not visible
On insert : Only cancel and save visible
On edit: Delete, Insert, Refresh invisible
I think you get my drift.
I have a working solution but it involves setting visibility for commanditems in itemdatabound event when binding GriddataItem.
I actually would like to set visibility in declaration as in the following snip:
<
td
id
=
"EditCommands"
runat
=
"server"
class
=
"CommandItems"
visible
=
"true"
>
<
asp:LinkButton
ID
=
"btnEditSelectedMD"
ToolTip
=
"Edit"
runat
=
"server"
Visible
=
"false"
CommandName="<%#RadGrid.EditSelectedCommandName%>"><
img
style
=
"border:0px;vertical-align:middle;"
alt
=
""
src
=
"img/EditRecord.gif"
/></
asp:LinkButton
>
<
asp:LinkButton
ID
=
"btnUpdateEditedMD"
runat
=
"server"
ToolTip
=
"Save"
CommandName="<%#RadGrid.UpdateEditedCommandName%>" Visible='<%# MDGrid.EditIndexes.Count > 0%>'><
img
style
=
"border:0px;vertical-align:middle;"
alt
=
""
src
=
"img/UpdateRecord.gif"
/></
asp:LinkButton
>
<
asp:LinkButton
ID
=
"btnCancelMD"
runat
=
"server"
ToolTip
=
"Cancel"
CommandName="<%#RadGrid.CancelCommandName%>" Visible='<%# MDGrid.EditIndexes.Count > 0 Or MDGrid.MasterTableView.IsItemInserted%>'><
img
style
=
"border:0px;vertical-align:middle;"
alt
=
""
src
=
"img/CancelRecord.gif"
/></
asp:LinkButton
>
<
asp:LinkButton
ID
=
"btnInsertMD"
runat
=
"server"
ToolTip
=
"Insert"
CommandName="<%#RadGrid.InitInsertCommandName%>" Visible='<%# Not MDGrid.MasterTableView.IsItemInserted And MDGrid.EditIndexes.Count = 0%>'><
img
style
=
"border:0px;vertical-align:middle;"
alt
=
""
src
=
"img/Add1818.png"
/></
asp:LinkButton
>
<
asp:LinkButton
ID
=
"btnSaveMD"
runat
=
"server"
ToolTip
=
"Save"
CommandName="<%#RadGrid.PerformInsertCommandName%>" Visible='<%# MDGrid.MasterTableView.IsItemInserted%>'><
img
style
=
"border:0px;vertical-align:middle;"
alt
=
""
src
=
"img/SaveData.gif"
/></
asp:LinkButton
>
<
asp:Image
ID
=
"imgSeperatorMD"
Style
=
"border: 0px; vertical-align: middle;"
alt
=
""
src
=
"img/Separator24.png"
runat
=
"server"
Visible
=
"false"
/>
<
asp:LinkButton
ID
=
"btnDeleteMD"
OnClientClick
=
"javascript:return confirm('Remove document?')"
runat
=
"server"
ToolTip
=
"Remove document"
CommandName="<%#RadGrid.DeleteSelectedCommandName%>" Visible='false'><
img
style
=
"border:0px;vertical-align:middle;"
alt
=
""
src
=
"img/DeleteRecord.gif"
/></
asp:LinkButton
>
<
asp:LinkButton
ID
=
"btnRefreshMD"
runat
=
"server"
ToolTip
=
"Refresh"
CommandName="<%#RadGrid.RebindGridCommandName%>" Visible='false'><
img
style
=
"border:0px;vertical-align:middle;"
alt
=
""
src
=
"img/Refresh1616.png"
/></
asp:LinkButton
>
</
td
>
Here are snips from databound :
If TypeOf e.Item Is GridDataItem And Not e.Item.IsInEditMode Then
' Enable commanditems
Dim commandItem As GridCommandItem = TryCast(TryCast(sender, RadGrid).MasterTableView.GetItems(GridItemType.CommandItem)(0), GridCommandItem)
' Enable editcommands
TryCast(commandItem.FindControl("btnEditSelectedMD"), LinkButton).Visible = True And (MDGrid.EditIndexes.Count = 0)
TryCast(commandItem.FindControl("btnDeleteMD"), LinkButton).Visible = True And (MDGrid.EditIndexes.Count = 0)
TryCast(commandItem.FindControl("btnRefreshMD"), LinkButton).Visible = True And (MDGrid.EditIndexes.Count = 0)
TryCast(commandItem.FindControl("imgSeperatorMD"), Image).Visible = True And (MDGrid.EditIndexes.Count = 0)
If TypeOf rpi.DataItem Is GridInsertionObject And e.Item.IsInEditMode Then
Dim commandItem As GridCommandItem = TryCast(TryCast(sender, RadGrid).MasterTableView.GetItems(GridItemType.CommandItem)(0), GridCommandItem)
TryCast(commandItem.FindControl("btnEditSelectedMD"), LinkButton).Visible = False
TryCast(commandItem.FindControl("btnDeleteMD"), LinkButton).Visible = False
TryCast(commandItem.FindControl("btnRefreshMD"), LinkButton).Visible = False
TryCast(commandItem.FindControl("imgSeperatorMD"), Image).Visible = False
Any input on how to improve on this?
For instance I would need to have number of rows in aspx to get this to work.
regards
Alexander
0
Hello Alexander,
Please try the following approach:
I hope this helps.
Regards,
Daniel
Telerik
Please try the following approach:
<
CommandItemTemplate
>
<
asp:LinkButton
ID
=
"Button1"
runat
=
"server"
Text
=
"Refresh"
CommandName
=
"Rebind"
Visible='<%# DetermineCommandItemButtonVisibility("Rebind") %>' />
<
asp:LinkButton
ID
=
"Button2"
runat
=
"server"
Text
=
"Insert"
CommandName
=
"InitInsert"
Visible='<%# DetermineCommandItemButtonVisibility("InitInsert") %>' />
<
asp:LinkButton
ID
=
"Button3"
runat
=
"server"
Text
=
"Update"
CommandName
=
"Update"
Visible='<%# DetermineCommandItemButtonVisibility("Update") %>' />
<
asp:LinkButton
ID
=
"Button4"
runat
=
"server"
Text
=
"Cancel"
CommandName
=
"Cancel"
Visible='<%# DetermineCommandItemButtonVisibility("Cancel") %>' />
<
asp:LinkButton
ID
=
"Button5"
runat
=
"server"
Text
=
"Delete"
CommandName
=
"Delete"
Visible='<%# DetermineCommandItemButtonVisibility("Delete") %>' />
<
asp:LinkButton
ID
=
"Button6"
runat
=
"server"
Text
=
"Save"
CommandName
=
"PerformInsert"
Visible='<%# DetermineCommandItemButtonVisibility("PerformInsert") %>' />
</
CommandItemTemplate
>
protected
bool
DetermineCommandItemButtonVisibility(
string
commandName)
{
switch
(commandName)
{
case
"Update"
:
return
!RadGrid1.MasterTableView.IsItemInserted;
case
"Cancel"
:
return
RadGrid1.MasterTableView.IsItemInserted || RadGrid1.EditIndexes.Count > 0;
case
"Refresh"
:
return
RadGrid1.MasterTableView.Items.Count > 0;
case
"Delete"
:
return
!RadGrid1.MasterTableView.IsItemInserted && RadGrid1.EditIndexes.Count == 0;
case
"InitInsert"
:
return
!RadGrid1.MasterTableView.IsItemInserted;
case
"PerformInsert"
:
return
RadGrid1.MasterTableView.IsItemInserted;
}
return
true
;
}
I hope this helps.
Regards,
Daniel
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.