1

我有一个telerik radgrid,如下图所示

| Username | Password   |
-------------------------
| A_user   | *****      |
| A_user2  | *****      |
| A_user3  | *****      |

当我单击其中一行时,它将显示单击行的密码,如下所示:

| Username | Password   |
-------------------------
| A_user   | *****      |
| A_user2  | A password |
| A_user3  | *****      |

这很好用。我之所以这样做是因为密码的解密是一个相当复杂且漫长的过程,因此一次解密一个密码的耗时较少,尤其是当用户不需要所有密码时。

当我显示密码时,我希望能够在网格中选择密码来复制它。不幸的是,radgrid 将再次触发“RowClick”项目命令,并且该行被取消选择。因此,我无法复制密码。

我的问题是:有没有办法在某些情况下取消 radgrid 的 itemcommand? 我希望能够在密码已经解密时禁用 itemcommand 事件。

提前致谢!

编辑: 我想我还应该提到我正在使用 radajaxloadingpanel 在加载时在网格上显示动画。即使 itemcommand 方法不执行任何操作,也会显示 ajaxloadingpanel 并且我尝试突出显示的文本被取消选择。

更多编辑:

这是我的 radgrid 代码

<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>

还有我的 ItemCommand 代码:

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
4

1 回答 1

2

切换到 radGrid 上的 OnSelectedIndexChanged 函数并将当前选定的行存储在客户端变量中,并处理 rowSelected 客户端事件并取消或允许回发发生,还添加您要使用的 ClientDataKeyName。

<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>

然后在服务器端:

  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 
        }
    }
于 2012-03-23T15:02:40.323 回答