单击链接按钮时获取aspgridview的选定行以获取隐藏的ID值 [英] Getting selected row of aspgridview when click on link button to get hidden ID value

查看:335
本文介绍了单击链接按钮时获取aspgridview的选定行以获取隐藏的ID值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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中的LinkBut​​ton连接一个onclick事件



标记

 < asp:TemplateField> 
< ItemTemplate>
< asp:LinkBut​​ton ID =LinkBut​​ton1runat =server
Text =Click1
OnClick =LinkBut​​ton1_Click/>
< / ItemTemplate>
< / asp:TemplateField>

隐藏代码

  protected void LinkBut​​ton1_Click(object sender,EventArgs e)
{
GridViewRow clickedRow =((LinkBut​​ton)sender).NamingContainer as GridViewRow;
Label lblID =(Label)clickedRow.FindControl(lblID);
}

使用RowCommand捕获LinkBut​​ton点击。



标记

 < asp:TemplateField> 
< ItemTemplate>
< asp:LinkBut​​ton ID =LinkBut​​ton2runat =server
Text =Click2
CommandName =MyCustomCommand/>
< / ItemTemplate>
< / asp:TemplateField>

隐藏代码

  protected void GridView1_RowCommand(object sender,GridViewCommandEventArgs e)
{
if(e.CommandName.Equals(MyCustomCommand))
{
GridViewRow clickedRow = ((LinkBut​​ton)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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆