• 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);
        }
  • 相关阅读:
    linux中按行读取指定行
    linux常用配置文件
    linux虚拟机设置网络
    jenkins新建一个robot脚本的job
    jenkins中配置邮件发送
    jenkins中robot framework插件安装
    Jenkins subversion svn插件安装失败
    jenkins节点启动
    {"non_field_errors":["Unable to log in with provided credentials."]}% 无法使用提供的凭据登录
    路径模板
  • 原文地址:https://www.cnblogs.com/moss_tan_jun/p/2096291.html
Copyright © 2020-2023  润新知