问题描述
限时送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 内的 LinkButton 连接一个 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 捕捉 LinkButton 点击.
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:关键词]
发布评论