单击链接按钮时获取aspgridview的选定行以获取隐藏的ID值 [英] Getting selected row of aspgridview when click on link button to get hidden ID value
问题描述
我使用aspgridview中的标题数据绑定链接按钮,并绑定隐藏的标签,其中包含ID值。当用户点击这个链接按钮时,我想访问ID值。我需要这样做,因为如果用户登录,那么只有我弹出详细信息窗口其他警告消息登录的详细信息。
在lnkTitle_Click()事件我试图访问选定row找到标签控件。
GridViewRow grdSelRow = GridView1.SelectedRow;
标签lblID =(标签)grdSelRow.FindControl(lblID);
但是我将grdSelRow设置为null。
如何在gridview的linkbutton上点击选中的行?
问题是,当你点击一个GridView中的按钮时,该行将只是一个点击行而不是 SelectedRow
。如果你想使它成为SelectedRow,你必须在Button的标记中指定 CommandName =Select
。
以下是完成您的需求的两种方法。
为ItemTemplate中的LinkButton连接一个onclick事件
标记
< asp:TemplateField>
< ItemTemplate>
< asp:LinkButton ID =LinkButton1runat =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点击。
标记
< asp:TemplateField>
< ItemTemplate>
< asp:LinkButton ID =LinkButton2runat =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作为GridViewRow;
Label lblID =(Label)clickedRow.FindControl(lblID);
}
}
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.
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");
But I am getting grdSelRow as null.
How to get the selected row when click on linkbutton of gridview?
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.
Wiring up an onclick event for the LinkButton inside ItemTemplate
Markup
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server"
Text="Click1"
OnClick="LinkButton1_Click"/>
</ItemTemplate>
</asp:TemplateField>
Code-behind
protected void LinkButton1_Click(object sender, EventArgs e)
{
GridViewRow clickedRow = ((LinkButton) sender).NamingContainer as GridViewRow;
Label lblID = (Label)clickedRow.FindControl("lblID");
}
Using RowCommand to catch the LinkButton click.
Markup
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server"
Text="Click2"
CommandName="MyCustomCommand"/>
</ItemTemplate>
</asp:TemplateField>
Code-behind
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");
}
}
这篇关于单击链接按钮时获取aspgridview的选定行以获取隐藏的ID值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!