简体   繁体   中英

onclick event for telerik radgrid row

I want to make an event occur when I click a row in my rad grid, I basically want to replace the 'editform' with an on click row event.

I've done a bit of searching online, but what works for others is having no impact for me.

The first piece of code is in ASP.NET, the second piece is in 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;
            }
    }
}

Try this:

I use SelectionChanged event to get row changing and if SelectedRows to access the selection rows.

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);
    }
}

One option would be to use the OnRowClick client event of RadGrid to run a script which will fire a command of your choice, and then process it in the code behind.

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

In the code behind:

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();
    }
}

Another option would be to use the approach mentioned by aghilpro. Utilizing the OnSelectedIndexChanged server event or RadGrid.

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

Code behind:

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM