GridView按钮事件

1.html代码

                               <asp:TemplateField HeaderText="操作">
                                  <ItemTemplate>
                                        <div style="width: 129px">
                                            <asp:Button ID="btnPass" runat="server" Text="通过" OnCommand="pass" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" />
                                            &nbsp;
                                            <asp:Button ID="btnRefuse" runat="server" Text="不通过" OnCommand="refuse" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" />
                                        </div>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="删除">
                                    <ItemTemplate>
                                        <asp:Button ID="btnDelete" Width="60" runat="server" Text="删除" CommandName="Delete" />
                                    </ItemTemplate>
                                </asp:TemplateField>

2.后台代码

     protected void gvAuthManagement_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int index = e.RowIndex;
            string openId = ((Label)gvAuthManagement.Rows[index].Cells[0].FindControl("Label4")).Text;
            AuthorizationInfo authInfo = new AuthorizationInfo { OpenId = openId };
            AuthorizationBiz.Delete(authInfo);
            InfoList = AuthorizationBiz.Query(new AuthorizationInfo());
            Bind(InfoList);
        }

        //给微信用户授权
        protected void pass(object sender, CommandEventArgs e)
        {
            int index = Convert.ToInt32(e.CommandArgument);
            string lienceplate = ((Label)gvAuthManagement.Rows[index].Cells[0].FindControl("Label1")).Text;
            string status = ((Label)gvAuthManagement.Rows[index].Cells[6].FindControl("Label6")).Text;
            AuthorizationInfo authInfo = new AuthorizationInfo { LiencePlateNumber = lienceplate };
            IList<AuthorizationInfo> list = AuthorizationBiz.Query(authInfo);

            //微信客服消息回复通过
            string access_token = AccessTo.GetExistAccessToken();
            string content = "{ \"touser\":\"" + list[0].OpenId + "\",\"msgtype\":\"text\",\"text\":{ \"content\":\"亲\"}}";
            string url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + access_token;//json格式的数据
            RequestHelper.SendPost(content, url, "application/json");
            if (status != "已授权")
            {
                authInfo.Status = "已授权";
                AuthorizationBiz.Update(authInfo);
            }
            InfoList = AuthorizationBiz.Query(new AuthorizationInfo());
            Bind(InfoList);
        }

        //拒绝微信用户授权
        protected void refuse(object sender, CommandEventArgs e)
        {
            int index = Convert.ToInt32(e.CommandArgument);
            string lienceplate = ((Label)gvAuthManagement.Rows[index].Cells[0].FindControl("Label1")).Text;
            string status = ((Label)gvAuthManagement.Rows[index].Cells[6].FindControl("Label6")).Text;
            AuthorizationInfo authInfo = new AuthorizationInfo { LiencePlateNumber = lienceplate };
            IList<AuthorizationInfo> list = AuthorizationBiz.Query(authInfo);

            //微信客服消息回复通过
            string access_token = AccessTo.GetExistAccessToken();
            string content = "{ \"touser\":\"" + list[0].OpenId + "\",\"msgtype\":\"text\",\"text\":{ \"content\":\"您的微信号授权申请被拒绝\"}}";
            string url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + access_token;//json格式的数据
            RequestHelper.SendPost(content, url, "application/json");
            if (status != "未授权")
            {
                authInfo.Status = "未授权";
                AuthorizationBiz.Update(authInfo);
            }
            InfoList = AuthorizationBiz.Query(new AuthorizationInfo());
            Bind(InfoList);
        }

二、

1)html代码

                               <asp:TemplateField HeaderText="编辑" ItemStyle-Width="80">
                                    <ItemTemplate>
                                        <asp:Button ID="btn_edit" runat="server" Text="编辑" CommandName="Edit" ForeColor="#1C94C4"></asp:Button>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:Button ID="btn_update" runat="server" CommandName="Update" Text="更新" /><%--OnClientClick="update()"--%>
                                    </EditItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="删除" ItemStyle-Width="80">
                                    <ItemTemplate>
                                        <asp:Button ID="btn_delete" runat="server" Text="删除" CommandName="Delete" ForeColor="#1C94C4"></asp:Button>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:Button ID="btn_cancel" runat="server" CommandName="Cancel" Text="取消" />
                                    </EditItemTemplate>
                                </asp:TemplateField>

2).

//行删除
        protected void gvCenterMangerment_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //此处只实现了单行的删除
            int index = e.RowIndex;
            string centerName = ((Label)gvCenterMangerment.Rows[index].Cells[1].FindControl("Label2")).Text;
            ContactInfo contactInfo = new ContactInfo { CenterName = centerName };
            try
            {
                ContactBiz.Delete(contactInfo);

            }
            catch (Exception ex)
            {
                Logger.Write("中心名称为:" + centerName + "的记录删除失败!原因:" + ex.Message);
                throw ex;
            }
            InfoList = ContactBiz.Query(new ContactInfo());
            Bind(InfoList);
        }

        //行编辑
        protected void gvCenterMangerment_RowEditing(object sender, GridViewEditEventArgs e)
        {
            try
            {
                gvCenterMangerment.EditIndex = e.NewEditIndex;
                InfoList = ContactBiz.Query(new ContactInfo());
                Bind(InfoList);
            }
            catch (Exception ex)
            {
                Logger.Write("中心管理行编辑失败" + ex.Message);
                throw ex;
            }

        }

        //行编辑修改数据
        protected void btnModify_Click(object sender, GridViewUpdateEventArgs e)
        {
            int index = e.RowIndex;
            ContactInfo contactInfo = new ContactInfo();
            // System.Text.RegularExpressions.Regex.IsMatch("","");
            //Regex.IsMatch(contactInfo.PhoneNumber,"^1[3456789]\\d{9}$|^0\\d{2,3}-?\\d{7,8}$");
            contactInfo.CenterName = ((Label)gvCenterMangerment.Rows[index].Cells[1].FindControl("Label2")).Text;
            contactInfo.Name = ((TextBox)gvCenterMangerment.Rows[index].Cells[2].FindControl("TextBox3")).Text;
            if (contactInfo.Name == "")
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(), System.DateTime.Now.Ticks.ToString(), "alert(‘*联系人不能为空*‘);", true);
                return;
            }
            contactInfo.PhoneNumber = ((TextBox)gvCenterMangerment.Rows[index].Cells[3].FindControl("TextBox4")).Text;
            if (contactInfo.PhoneNumber == "")
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), System.DateTime.Now.Ticks.ToString(), "alert(‘*联系方式不能为空*‘);", true);
                return;
            }
            else
            {
                if (!Regex.IsMatch(contactInfo.PhoneNumber, "^1[3456789]\\d{9}$|^0\\d{2,3}-?\\d{7,8}$"))
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(), System.DateTime.Now.Ticks.ToString(), "alert(‘*联系方式格式错误*‘);", true);
                    return;
                }
            }
            contactInfo.Address = ((TextBox)gvCenterMangerment.Rows[index].Cells[4].FindControl("TextBox5")).Text;
            if (contactInfo.Address == "")
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(), System.DateTime.Now.Ticks.ToString(), "alert(‘*地址不能为空*‘);", true);
                return;
            }
            ContactBiz.Update(contactInfo);
            gvCenterMangerment.EditIndex = -1;
            InfoList = ContactBiz.Query(new ContactInfo());
            Bind(InfoList);
            errorWarning.Text = "";
        }

        //行编辑取消
        protected void gvCenterMangerment_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvCenterMangerment.EditIndex = -1;
            InfoList = ContactBiz.Query(new ContactInfo());
            Bind(InfoList);
        }

三、

1)html代码

<asp:ButtonField ButtonType="Button" CommandName="observe" HeaderText="操作" Text="查看" />

2)

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "observe")
            {
                try
                {
                    int index = Convert.ToInt32(e.CommandArgument);
                    string LiencePlateNumber = ((Label)GridView1.Rows[index].Cells[1].FindControl("label2")).Text.ToString().Trim();
                    string CreateTime = ((Label)GridView1.Rows[index].Cells[7].FindControl("label8")).Text.ToString().Trim();
                    string url = "../Management/ExDetails.html?LiencePlateNumber=" + LiencePlateNumber + "&CreateTime=" + CreateTime;
                    string js = "window.open(\"../Management/ExDetails.html?LiencePlateNumber=" + LiencePlateNumber + "&CreateTime=" + CreateTime + "\",\"_blank\")";
                    ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(), System.DateTime.Now.Ticks.ToString(), js, true);
                }
                catch (Exception ex)
                {
                    Logger.Write("后台查看异常详情失败,原因:" + ex.Message);
                    throw ex;
                }

            }
        }
时间: 2024-12-21 05:44:46

GridView按钮事件的相关文章

asp.net中父子页面通过gridview中的按钮事件进行回传值的问题

这两天写BS程序,遇到父子页面传值的问题,以前没写过web系统,用了几天时间才将问题解决,总结下记录下来: 问题描述: 父页面A中有一个gridview,每行6个列,有5列中均有一个按钮,单击按钮,会根据列的不同进入不同的子页面,在子页面中有获取数据,并返回父页面的gridview中对应的行的对应列中. 问题关键在于如果确定点击的是gridview的哪一行的哪个按钮,因为数据回传的时候,还要放入该行的该列中. 所以需要考虑一下几个方面: 1.动态添加行,以及各行的各列中的按钮 2.点击某行某列的

IDA学习笔记--VS2008按钮事件捕捉

http://blog.csdn.net/ccnyou/article/details/8521611 IDA笔记--VS2008按钮事件捕捉 用到工具: IDA Proc C32Asm Rescope VS2008 LordPE 实例程序:MFCDemo.exe(附下载链接),我们目标是找到Button1对应的函数的地址处 附件包含:博客文章原文文档,文章中用到的MFCDemo程序. 下载链接:http://download.csdn.net/detail/ccnyou/5012040 1,首

iOS代码实现:创建按钮,绑定按钮事件,读取控件值

// // main.m // Hello // // Created by lishujun on 14-8-28. // Copyright (c) 2014年 lishujun. All rights reserved. // #import <UIKit/UIKit.h> // 视图控制器对象 @interface HelloWorldViewController : UIViewController @property (nonatomic, retain) IBOutlet UIT

android开发步步为营之22:处理Activity中的back按钮事件

在手机应用中,用户点击回退按钮一般是返回上个页面,一般页面不用处理,如果在首页,点回退,没任何提示,就把应用给关了,这个用户体验就不太好了,所以一般都会给用户一个确认的提示:是否退出?免得用户误操作. 一. Activity 中处理 @Override public boolean onKeyDown( int keyCode, KeyEvent event) { // TODO Auto-generated method stub Toast.makeText( this , "onkeydo

安卓之页面跳转与传值和按钮事件

一:新建页面 即新建Activity,new-other-Android Activity,next, 新建Activity的时候, 1:eclipse会自动创建Layout,我们发现Layout目录下会多了对应的xml文件: 2:ec会自动在AndroidManifest.xml中创建对应的activity节点: 需要注意的是,这些都是ec帮我们自动创建的,我们完全可以手动创建 class,然后让它继承自activity,然后指定layout的那个xml,然后手动创建节点完成. 二:点击按钮进

Ugui按钮事件添加方法

按钮事件添加方法3个例子 Eg1: using UnityEngine; using System.Collections; using UnityEngine.Events;//引用事件命名空间 using UnityEngine.UI;//引用UI命名空间 public class Test : MonoBehaviour { // Use this for initialization void Start () { //定义Action,并赋予delegate方法 UnityAction

MVC中使用内建的HTML辅助方法产生表单元素提交表单与button按钮事件的陷阱

网站模板页有个登陆的退出按钮,当点击时跳转到登陆页面. <button onclick="logout()" >退出</button> $("#logOut").click(function () { location.href = "@Url.Action("Logout", "Account")"; }); 然后再某个页面楼主用了HTML辅助方法产生表单元素,代码如下所示: @H

导航栏切换按钮事件

同事写的一段代码,不是很难,但感觉不错保存起来 // 导航栏切换按钮事件 $('ul.main-tab>li').on('mousedown', function() { var $this = $(this), $box = $('.main-tab-content'), i = $this.index(); if ($this.hasClass('on')) { return false; } switch (i) { case 0: break; case 1: updateRadarTa

.Net 转战 Android 4.4 日常笔记(4)--按钮事件和国际化

原文:.Net 转战 Android 4.4 日常笔记(4)--按钮事件和国际化 我们知道资源被注册到R.java我们通过R.java就可以读取到界面中的组件.跟我们.net一样,通过ID来读取组件 知识点: 通过R.java读取组件 MainActivity.java通过findViewById方法查找组件 在Layout中用@string查找字符串,这个跟我们MVC中国际化基本相识,通过xml这种资源引用进行中文和英文切换 加入中文和英文本地化 双击fragment_main.xml设计视图