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

    实现这个功能,你需要为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);
        }
  • 相关阅读:
    nginx Server names
    ES6--变量的声明及解构赋值
    Android ListView and Tips.
    Eclipse自己定义keystore
    POJ 1129 Channel Allocation(DFS)
    机器学习笔记十三:Ensemble思想(上)
    设计模式——享元模式具体解释
    老猪带你玩转自定义控件三——sai大神带我实现ios 8 时间滚轮控件
    老猪带你玩转android自定义控件二——自定义索引栏listview
    android动手写控件系列——老猪叫你写相机
  • 原文地址:https://www.cnblogs.com/moss_tan_jun/p/2096291.html
Copyright © 2011-2022 走看看