首页 > 代码库 > GridView按钮事件

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;                }            }        }

 

GridView按钮事件