telerik radgrid行的onclick事件

时间:2017-11-03 17:47:11

标签: c# asp.net telerik radgrid

我想在我的rad网格中单击一行时发生一个事件,我基本上想要替换' editform'点击行事件。

我已经在网上做了一些搜索,但对他人有用的是对我没有影响。

第一段代码在ASP.NET中,第二段在C#中。

<div id="rgUser" runat="server" style="width: 300px; float: left; margin-left: 15px;">
<telerik:RadGrid ID="rgEffectivePermissions" runat="server" AutoGenerateColumns="false"
    AllowSorting="True" Visible="true" AllowPaging="True"
    OnPageIndexChanged="rgEffectivePermissions_PageIndexChanged"
    OnItemDataBound="rgEffectivePermissions_ItemDataBound"
    OnItemCommand="rgEffectivePermissions_ItemCommand">
    <ClientSettings>
        <Resizing AllowColumnResize="true"></Resizing>
    </ClientSettings>
    <MasterTableView AllowSorting="true" DataKeyNames="SystemUserID">
        <CommandItemSettings ShowAddNewRecordButton="false" ShowRefreshButton="false" ShowExportToExcelButton="false" />
        <SortExpressions>
            <telerik:GridSortExpression FieldName="ClientCode" SortOrder="Ascending" />
        </SortExpressions>
        <Columns>
            <telerik:GridTemplateColumn UniqueName="UserName" DataField="UserName" HeaderText="User Name" SortExpression="UserName">
                <HeaderStyle HorizontalAlign="Center" />
                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="100" />
                <ItemTemplate>
                    <asp:Label ID="lblUsername" runat="server"><%# DataBinder.Eval(Container.DataItem, "UserName") %></asp:Label>
                    <asp:HoverMenuExtender ID="hmeSystemUserInfo" runat="server" TargetControlID="lblUsername" PopupControlID="pnlSystemUserInfo" PopupPosition="Bottom" />
                    <asp:Panel ID="pnlSystemUserInfo" runat="server" Style="visibility: hidden;">
                        <asp:Label runat="server" BackColor="Black" ForeColor="White" BorderColor="Black" Font-Bold="true" BorderStyle="Solid">
                        Last Updated By: <%# DataBinder.Eval(Container.DataItem, "LastUpdatedBy") %><br />
                        Last Update Date: <%# DataBinder.Eval(Container.DataItem, "LastUpdateDate") %><br />
                        Is Service Account: <%# DataBinder.Eval(Container.DataItem, "IsServiceAccount") %><br />
                        Is Account Manager: <%# DataBinder.Eval(Container.DataItem, "IsAccountManager") %>
                        </asp:Label>
                    </asp:Panel>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridBoundColumn UniqueName="SystemUserID" DataField="SystemUserID" HeaderText="SystemUserID" SortExpression="SystemUserID" ReadOnly="true" Visible="true" Display="false">
                <HeaderStyle HorizontalAlign="Center" />
                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            </telerik:GridBoundColumn>
            <telerik:GridTemplateColumn UniqueName="EmailAddress" DataField="EmailAddress" HeaderText="Email Address" SortExpression="EmailAddress">
                <HeaderStyle HorizontalAlign="Center" />
                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="100" />
                <ItemTemplate>
                    <%# DataBinder.Eval(Container.DataItem, "EmailAddress") %>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridButtonColumn ButtonType="ImageButton" UniqueName="EditButton" HeaderText="Edit"
                HeaderStyle-HorizontalAlign="Center" CommandName="EditForm" ImageUrl="~/Images/editPencil.png">
                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            </telerik:GridButtonColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>
</div>





protected void rgEffectivePermissions_ItemCommand(object source, GridCommandEventArgs e)
{
    switch (e.CommandName)
    {
        case ("EditForm"):
            {
                GridDataItem item = (GridDataItem)e.Item;
                String SystemUserID = item["SystemUserID"].Text;
                rgSecurityGroup.DataSource = AdminManager.GetClientGroupBySUSystemUserID(SystemUserID);
                rgSecurityGroup.DataBind();

                //int clientID = Convert.ToInt32(ddlClient1.SelectedValue);
                //rgProductGroup.DataSource = AdminManager.GetActivityGroupsByClientGroupID(41);
                //rgProductGroup.DataBind();
                break;
            }
    }
}

2 个答案:

答案 0 :(得分:0)

试试这个:

我使用SelectionChanged事件来更改行,并使用SelectedRows来访问选择行。

radGridView1.SelectionChanged += new System.EventHandler(radGridView1_SelectionChanged);

private void radGridView1_SelectionChanged(object sender, EventArgs e)
{
    try
    {
        if (this.radGridView1.SelectedRows.Count > 0)
        {
            int selectedIndex = radGridView1.SelectedRows[0].Index;
        }
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex.Message);
    }
}

答案 1 :(得分:0)

一个选项将使用RadGrid的OnRowClick客户端事件来运行一个脚本,该脚本将触发您选择的命令,然后在后面的代码中处理它。

<telerik:RadGrid ID="rgEffectivePermissions" runat="server" 
    <ClientSettings>
        <ClientEvents OnRowClick="onRowClik" />
    </ClientSettings>
</telerik:RadGrid>

<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
    <script>
        function onRowClik(sender, args) {
            $find("<%= rgEffectivePermissions.ClientID %>").get_masterTableView().fireCommand("MyCommand", args.get_item().getDataKeyValue("SystemUserID"));
        }
    </script>
</telerik:RadCodeBlock>

在后面的代码中:

protected void rgEffectivePermissions_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
    if(e.CommandName == "MyCommand")
    {
        rgSecurityGroup.DataSource = GetGridSource(int.Parse(e.CommandArgument.ToString()));
        rgSecurityGroup.Rebind();
    }
}

另一个选项将使用aghilpro提到的方法。利用OnSelectedIndexChanged服务器事件或RadGrid。

<telerik:RadGrid ID="rgEffectivePermissions" runat="server" 
    OnSelectedIndexChanged="rgEffectivePermissions_SelectedIndexChanged">
    <ClientSettings EnablePostBackOnRowClick="true">
        <Selecting AllowRowSelect="true" />
    </ClientSettings>
</telerik:RadGrid>

代码背后:

protected void rgEffectivePermissions_SelectedIndexChanged(object sender, EventArgs e)
{

    if(((RadGrid)sender).SelectedItems.Count > 0)
    {
        rgSecurityGroup.DataSource = GetGridSource(int.Parse(((GridDataItem)((RadGrid)sender).SelectedItems[0])["SystemUserID"].Text));
        rgSecurityGroup.Rebind();
    }
}