zoukankan      html  css  js  c++  java
  • GridView内按钮Click获取记录主键值

    在GridView控件中,每行记录内会放置一个铵钮,当用户点击这个铵钮时,获取当笔记录的主键值。可看演示(是一个gif动画,重新播放尝试刷新网页):


    实现这个功能,你需要为GridView控件设置DataKeyNames属性和OnRowCreated事件。

    View Code
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="MediaTypeId"
                        OnRowCreated
    ="GridView1_RowCreated">
                        
    <Columns>
                            
    <!--
                                其它 TemplateField
                            
    -->
                            
    <asp:TemplateField HeaderText="Select">
                                
    <ItemTemplate>
                                    
    <asp:Button ID="Button1" runat="server" Text="选择" />
                                
    </ItemTemplate>
                            
    </asp:TemplateField>
                        
    </Columns>
                    
    </asp:GridView>

    .aspx.cs代码:

    View Code
     protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
           
    if (e.Row.RowType != DataControlRowType.DataRow) return;

            
    if (e.Row.FindControl ("Button1"!= null)
            {
                Button CtlButton 
    = (Button)e.Row.FindControl ("Button1");
                CtlButton.Click 
    +=new EventHandler(CtlButton_Click);
            }
        }

        
    private void CtlButton_Click(object sender, EventArgs e)
        {
            Button button 
    = (Button)sender;
            GridViewRow gvr 
    = (GridViewRow)button.Parent.Parent;
            
    string pk = GridView1.DataKeys[gvr.RowIndex].Value.ToString();

            
    //do something

            
    //InsusJsUtility objJs = new InsusJsUtility();  //http://www.cnblogs.com/insus/articles/1341703.html
            
    //objJs.JsAlert(pk);
        }
  • 相关阅读:
    通过HTTP发包工具了解HTTP协议
    Oracle之数据库安全
    SQL注入深入剖析
    apache中如何调用CGI脚本
    fastcgi php-cgi与php-fpm区别和之间的关系
    使用PHPExcel实现Excel文件的导入和导出(模板导出)
    学会数据库读写分离、分表分库
    框架Thinkphp5 简单的实现行为 钩子 Hook
    php文件下载
    PHP为JSON数据的API返回空数组或者空对象
  • 原文地址:https://www.cnblogs.com/insus/p/2094151.html
Copyright © 2011-2022 走看看