Reputation: 1456
I have a telerik radgrid, like shown underneath
| Username | Password |
-------------------------
| A_user | ***** |
| A_user2 | ***** |
| A_user3 | ***** |
When I click one of the rows, it will display the password of the clicked row, like so:
| Username | Password |
-------------------------
| A_user | ***** |
| A_user2 | A password |
| A_user3 | ***** |
That works fine. I am proceeding this way because the decryption of the password is a rather complicated and long process, so decrypting one password at a time is less time consuming, especially when the user doesn't need all the passwords.
When I have the password displayed, I'd like to be able to select the password in the grid to copy it. Unfortunately, the radgrid will fire the "RowClick" itemcommand once more, and the row gets de-selected. Therefore, I cannot copy the passwords.
My question is: Is there a way to cancel the itemcommand of a radgrid under certain circumstances? I'd like to be able to disable the itemcommand event when the password is already decrypted.
Thanks in advance!
Edit: I guess I should also be mentioning that I'm using a radajaxloadingpanel to display an animation over the grid when it's loading. Even when the itemcommand method does nothing, the ajaxloadingpanel is displayed and the text I try to highlight is de-selected.
More edits:
Here is my radgrid code
<telerik:RadGrid id="radGridAccounts" runat="server" Width="99%" PageSize="20" AllowPaging="true" AllowSorting="true"
AllowFilteringByColumn="True" ShowStatusBar="true" EnableLinqExpressions="False" GridLines="None"
AllowMultiRowSelection="false">
<ClientSettings EnableRowHoverStyle="true" EnablePostBackOnRowClick="true" AllowKeyboardNavigation="false">
<Selecting AllowRowSelect="True" />
<KeyboardNavigationSettings EnableKeyboardShortcuts="false" />
</ClientSettings>
<PagerStyle Mode="NextPrevAndNumeric" PagerTextFormat=""/>
<MasterTableView AutoGenerateColumns="False" EditMode="InPlace" CommandItemDisplay="Top"
InsertItemDisplay="Top" AllowFilteringByColumn="True" NoMasterRecordsText="Aucun compte"
InsertItemPageIndexAction="ShowItemOnCurrentPage" DataKeyNames="USERN" >
<Columns>
<%--Username--%>
<telerik:GridBoundColumn UniqueName="USERN" DataField="USERN" HeaderText="Username"
AllowFiltering="true" ColumnEditorID="radUsernameEditor"/>
<%--Password--%>
<telerik:GridBoundColumn UniqueName="PASWR" DataField="PASWR" HeaderText="Password"
AllowFiltering="false" ColumnEditorID="radPasswordEditor" />
<%--Edit--%>
<telerik:GridEditCommandColumn ButtonType="ImageButton"
InsertImageUrl=".\Images\ok.gif" UpdateImageUrl=".\Images\ok.gif" CancelImageUrl=".\Images\cancel.gif" />
<%--Delete--%>
<telerik:GridButtonColumn ButtonType="ImageButton" CommandName="Delete" Text="Delete"
ImageUrl=".\Images\delete.gif" />
</Columns>
<CommandItemSettings AddNewRecordText="" RefreshText="" />
</MasterTableView>
</telerik:RadGrid>
And my ItemCommand code:
Protected Sub radGridAccounts_ItemCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles radGridAccounts.ItemCommand
Try
If (e.CommandName = "RowClick" AndAlso TypeOf e.Item Is GridDataItem) Then
e.Item.Selected = True
For Each item As GridDataItem In radGridAccounts.Items
If Not item.IsInEditMode Then
If item.Selected Then
'Decrypt the password method
Else
item.Cells(4).Text = "*****"
End If
End If
Next
End If
Catch ex As Exception
DisplayMessage("Error : " & ex.Message, MessageType.Err)
End Try
End Sub
Upvotes: 1
Views: 1349
Reputation: 1160
Switch to OnSelectedIndexChanged function on the radGrid and store the current selected row in a clientside variable, and handle the rowSelected client event and either cancel or allow the postback to happen, also add in the ClientDataKeyName that you want to use.
<telerik:RadGrid id="radGridAccounts" runat="server" Width="99%" PageSize="20" AllowPaging="true" AllowSorting="true" OnSelectedIndexChanged="RadGridAccounts_SelectedIndexChanged"
AllowFilteringByColumn="True" ShowStatusBar="true" EnableLinqExpressions="False" GridLines="None"
AllowMultiRowSelection="false">
<ClientSettings EnableRowHoverStyle="true" EnablePostBackOnRowClick="true" EnableAllowKeyboardNavigation="false">
<Selecting AllowRowSelect="True" />
<KeyboardNavigationSettings EnableKeyboardShortcuts="false" />
<ClientEvents OnRowSelected="rowSelected" />
</ClientSettings>
<PagerStyle Mode="NextPrevAndNumeric" PagerTextFormat=""/>
<MasterTableView AutoGenerateColumns="False" EditMode="InPlace" CommandItemDisplay="Top"
InsertItemDisplay="Top" AllowFilteringByColumn="True" NoMasterRecordsText="Aucun compte"
InsertItemPageIndexAction="ShowItemOnCurrentPage" DataKeyNames="USERN" ClientDataKeyNames="USERN" >
<Columns>
...
</Columns>
<CommandItemSettings AddNewRecordText="" RefreshText="" />
</MasterTableView>
</telerik:RadGrid>
<script type="text/javascript">
var selectedUserName;//global js var
function rowSelected(senders, args)
{
//get the ClientDataKeyName
var rowUserName= args.getDataKeyValue("USERN");
if(selectedUserName == rowUserName){
args.set_cancel(true);//cancel the postback
}
}
</script>
Then on the server-side:
protected void RadGridAccounts_SelectedIndexChanged(object sender, EventArgs e)
{
if (RadGridAccounts.SelectedItems == null || RadGridAccounts.SelectedItems.Count == 0)
return;
var dataItem = RadGridAccounts.SelectedItems[0] as GridDataItem;
if (dataItem != null)
{
//do the password look up
}
}
Upvotes: 2