单击链接按钮获取隐藏的 ID 值时获取aspgridview 的选定行

编程入门 行业动态 更新时间:2024-10-26 02:24:46
本文介绍了单击链接按钮获取隐藏的 ID 值时获取aspgridview 的选定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我将链接按钮与 aspgridview 中的标题数据绑定,并绑定包含 ID 值的隐藏标签.当用户单击此链接按钮时,我想访问 ID 值.这是我需要的,因为如果用户登录,那么我只会弹出详细信息窗口,否则会弹出警告消息以登录以获取详细信息.

I am binding link button with title data in aspgridview and also binding hidden label which holds the ID value. when user click on this link button I would like to access the ID value. This I need because, if user logs in then only I popup detail window else alert message to login for details.

在 lnkTitle_Click() 事件中,我试图访问所选行以查找标签控件.

in lnkTitle_Click() event I am trying to access the selected row to find the label control.

GridViewRow grdSelRow = GridView1.SelectedRow;
Label lblID = (Label)grdSelRow.FindControl("lblID");

但我得到的 grdSelRow 为空.

But I am getting grdSelRow as null.

点击gridview的linkbutton时如何获取选中的行?

How to get the selected row when click on linkbutton of gridview?

推荐答案

问题是当你在 GridView 中单击一个按钮时,该行只会是一个 Clicked Row 而不是 SelectedRow.如果您想让它成为 SelectedRow,您必须在按钮的标记中指定 CommandName="Select".

The problem is that when you click a button in a GridView, the row will only be a Clicked Row and not a SelectedRow. If you wanna make it the SelectedRow you have to specify CommandName="Select" in the Button's markup.

这里有两种方法可以满足您的要求.

Here are two methods for accomplish your requirement.

为 ItemTemplate 内的 LinkBut​​ton 连接一个 onclick 事件

Wiring up an onclick event for the LinkButton inside ItemTemplate

标记

<asp:TemplateField>
    <ItemTemplate>
        <asp:LinkButton ID="LinkButton1" runat="server" 
                    Text="Click1" 
                    OnClick="LinkButton1_Click"/>
    </ItemTemplate>
</asp:TemplateField>

代码隐藏

protected void LinkButton1_Click(object sender, EventArgs e)
{
    GridViewRow clickedRow = ((LinkButton) sender).NamingContainer as GridViewRow;
    Label lblID = (Label)clickedRow.FindControl("lblID");
}

使用 RowCommand 捕捉 LinkBut​​ton 点击​​.

Using RowCommand to catch the LinkButton click.

标记

<asp:TemplateField>
    <ItemTemplate>
        <asp:LinkButton ID="LinkButton2" runat="server" 
                    Text="Click2" 
                    CommandName="MyCustomCommand"/>
    </ItemTemplate>
</asp:TemplateField>

代码隐藏

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if(e.CommandName.Equals("MyCustomCommand"))
    {
        GridViewRow clickedRow = ((LinkButton)e.CommandSource).NamingContainer as GridViewRow;
        Label lblID = (Label)clickedRow.FindControl("lblID");
    }
}

这篇关于单击链接按钮获取隐藏的 ID 值时获取aspgridview 的选定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-01 10:36:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/828296.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单击   按钮   链接   aspgridview   ID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!