Telerik - 网格行内的图像按钮,获取第一个单元格的值

发布于 2024-12-09 22:03:24 字数 592 浏览 1 评论 0原文

我有一个 RadGrid,其中包含一个模板列,我在其中放置了两个用于编辑和删除操作的图像按钮。

<telerik:GridTemplateColumn HeaderText="Actions">    
   <ItemTemplate>
     <asp:ImageButton ID="btnEdit" runat="server"  ImageUrl="~/images/icon_edit.png" style="display: inline-block" ToolTip="Edit"  />&nbsp;&nbsp;&nbsp;<asp:ImageButton ID="btnDelete" runat="server" ImageUrl="~/images/icon_delete.png" style="display: inline-block" ToolTip="Delete" />
      </ItemTemplate>
</telerik:GridTemplateColumn>

当我单击“删除”按钮时,如何获取该行第一个单元格(datafield =“User_ID”)的值?

I have a RadGrid that contains a template column in which I've put two image buttons for edit and delete actions.

<telerik:GridTemplateColumn HeaderText="Actions">    
   <ItemTemplate>
     <asp:ImageButton ID="btnEdit" runat="server"  ImageUrl="~/images/icon_edit.png" style="display: inline-block" ToolTip="Edit"  />   <asp:ImageButton ID="btnDelete" runat="server" ImageUrl="~/images/icon_delete.png" style="display: inline-block" ToolTip="Delete" />
      </ItemTemplate>
</telerik:GridTemplateColumn>

How would get the value of the first cell of the row (datafield = "User_ID") when I click on the "delete" button?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥抱影子 2024-12-16 22:03:24

第 1 步。

转到 Radgrid 本身并编辑字段 DataKeyNames=""(在 MasterTableView 下)并添加您要提取的数据字段:

<MasterTableView ... DataKeyNames="User_ID">

第 2 步。
编辑位于网格中的图像按钮的 CommandName="" 属性:

 <asp:ImageButton ID="btnDelete" runat="server" style="display: inline-block" ToolTip="Delete" CommandName="dosomething"/>

为 Radgrid 创建以下方法并添加以下代码:

protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
   if (e.CommandName == "dosomething")
    {
        //Use a line of code here to save that User_ID that you want from the first column
        theUserId = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["User_ID"];
    }
}

确保 UserId = 相同的类型 (int,double,dec... )作为它从中提取的字段,否则您将不得不解析它:

theUserId = Int.Parse(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["User_ID"]);

如果您需要更多帮助,请告诉我。

Step 1.

Go to the Radgrid itself and edit the field DataKeyNames="" (under MasterTableView) and add the datafield you are pulling:

<MasterTableView ... DataKeyNames="User_ID">

Step 2.
Edit the CommandName="" property of the Imagebuttons located in the grid:

 <asp:ImageButton ID="btnDelete" runat="server" style="display: inline-block" ToolTip="Delete" CommandName="dosomething"/>

Create the following method for your Radgrid and add this code:

protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
   if (e.CommandName == "dosomething")
    {
        //Use a line of code here to save that User_ID that you want from the first column
        theUserId = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["User_ID"];
    }
}

Make sure theUserId = the same Type (int,double,dec...) as the field it's pulling from or you will have to parse it:

theUserId = Int.Parse(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["User_ID"]);

Let me know if you need more help.

南街女流氓 2024-12-16 22:03:24

请检查下面的代码片段。

   <MasterTableView  DataKeyNames="ID">

<Columns>
                    <telerik:GridBoundColumn DataField="Name" HeaderText="Name" UniqueName="Name">
                    </telerik:GridBoundColumn>
                    <telerik:GridTemplateColumn HeaderText="Actions">
                        <ItemTemplate>
                            <asp:Button ID="btnEdit" runat="server" ToolTip="Edit" CommandName="Edit" />   <asp:Button
                                ID="btnDelete" runat="server" ToolTip="Delete" CommandName="Delete" />
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>

……………………

protected void grdCompCliente_ItemCommand(object sender, GridCommandEventArgs e)
{
        if (e.CommandName == "Edit")
        {
            GridDataItem item = e.Item as GridDataItem;
            string ID = item.GetDataKeyValue("ID").ToString();
            string Name = item["Name"].Text;
        }
        else if (e.CommandName == "Delete")
        {
            GridDataItem item = e.Item as GridDataItem;
            string ID = item.GetDataKeyValue("ID").ToString();
            string Name = item["Name"].Text;
        }
}

Please check Below code snippet.

   <MasterTableView  DataKeyNames="ID">

<Columns>
                    <telerik:GridBoundColumn DataField="Name" HeaderText="Name" UniqueName="Name">
                    </telerik:GridBoundColumn>
                    <telerik:GridTemplateColumn HeaderText="Actions">
                        <ItemTemplate>
                            <asp:Button ID="btnEdit" runat="server" ToolTip="Edit" CommandName="Edit" />   <asp:Button
                                ID="btnDelete" runat="server" ToolTip="Delete" CommandName="Delete" />
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>

.........................

protected void grdCompCliente_ItemCommand(object sender, GridCommandEventArgs e)
{
        if (e.CommandName == "Edit")
        {
            GridDataItem item = e.Item as GridDataItem;
            string ID = item.GetDataKeyValue("ID").ToString();
            string Name = item["Name"].Text;
        }
        else if (e.CommandName == "Delete")
        {
            GridDataItem item = e.Item as GridDataItem;
            string ID = item.GetDataKeyValue("ID").ToString();
            string Name = item["Name"].Text;
        }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文