如何在pageindexchanging事件中从gridview获取linkbutton id [英] How to get linkbutton id from gridview in pageindexchanging event

查看:42
本文介绍了如何在pageindexchanging事件中从gridview获取linkbutton id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在pageindexchanging事件中从gridview获取linkbutton id 





我尝试过:



如何在pageindexchanging事件中从gridview获取linkbutton id 

解决方案

在网格视图标记中定义按钮,并为按钮分配一个CommandName值,如下所示





 <   pre  >  <   asp:GridView     ID   =  GridView1    AutoGenerateColumns   =  False    runat   =  server < span class =code-attribute>     

OnRowCommand = GridView1_RowCommand >
< >
< asp:TemplateField HeaderText = 添加记录 >
< ItemTemplate >
< asp:按钮 ID = btnAddRecord

CommandArgument = <%#((GridViewRow)Container).RowIndex %>

CommandName = AddRecord runat = server Text = 添加 / >
< < span class =code-leadattribute> / ItemTemplate >
< / asp:TemplateField &g t;
< / Columns >
< / asp:GridView >





现在在你的代码隐藏中,你可以处理OnRowCommand事件和AddRecord命令,如下所示:





  protected   void  GridView1_RowCommand( object  sender,GridViewCommandEventArgs e)
{
if (e.CommandName == AddRecord
{
// 获取作为命令参数传递的行的索引
int index = Convert.ToInt 32(e.CommandArgument.ToString());

// 这里的逻辑
}
}


how to get linkbutton id from gridview in pageindexchanging event



What I have tried:

how to get linkbutton id from gridview in pageindexchanging event

解决方案

Define the button in your grid view markup and assign a CommandName value to the button, like this



<pre><asp:GridView ID="GridView1" AutoGenerateColumns="False" runat="server"  

        OnRowCommand="GridView1_RowCommand">
    <Columns>
        <asp:TemplateField HeaderText="Add Record">
            <ItemTemplate>
                <asp:Button ID="btnAddRecord"  

                            CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" 

                            CommandName="AddRecord" runat="server" Text="Add" />
            </ItemTemplate>
        </asp:TemplateField> 
    </Columns>
</asp:GridView>



Now in your code-behind, you can handle the OnRowCommand event and AddRecord command, like this:



protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "AddRecord")
    {
        // Get index of row passed as command argument
        int index = Convert.ToInt32(e.CommandArgument.ToString());

        // Your logic here
    }
}


这篇关于如何在pageindexchanging事件中从gridview获取linkbutton id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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