Gridview中几个Button的应用

 gridview中有三种方式添加button的应用,CommandField、ButtonField、TemplateField中加Button这三种方式。三种方式都可以实现同样的功能,但在实现某些功能时,实现方法是不一样的,下面我们来介绍一下:

一、获取选中行的某个字段值

1、模板中加Button,利用CommandArgument绑定数据库中某个字段。

A、首先,模板Button设置commandname为一个值例如“selectid”,绑定CommandArgument到数据库一个字段,CommandArgument='<%# Bind("shqxdj") %>'。

然后,在页面的源里,找到Gridview的代码,加入程序名OnRowCommand="GridView1_RowCommand"。让页面能找到执行程序的名字。

<asp:GridView ID="GridView1" runat="server" DataKeyNames="pid" DataSourceID="SqlDataSource1" OnRowCommand="GridView1_RowCommand">

然后在后台cs代码加入:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

{

if (e.CommandName == "selectid")

{

Label1.Text = e.CommandArgument.ToString();

}

}

B、前面一种方法是通过Button的Commandname,在GridView中的RowCommand中的加入点击后要执行的命令,这跟ButtonField是一样的形式,模板中Button还有另一种执行点击命令的方法,就是直接写Button_Click事件,就像一般的Button一样。

<asp:TemplateField HeaderText="用户类别选择"> <ItemTemplate> <asp:Label ID="LByhdj" runat="server" Text='<%# Bind("shqxmc") %>' Width="45px"></asp:Label> <asp:Button ID="BTNxzyh" runat="server" Font-Size="9pt" Text="选择具体用户" OnClick="BTNxzyh_Click" CommandArgument='<%# Bind("shqxdj") %>' /> </ItemTemplate> </asp:TemplateField>

protected void BTNxzyh_Click(object sender, EventArgs e) { this.TXTtest.Text = ((Button)sender).CommandArgument.ToString();

}

2、可以首先获取Button当前行的Index值,然后根据Index值取得当前行某列的值,或当前行DataKeys来获取数据。

A、ButtonField控件中加CommandName属性

aspx页面:

<asp:ButtonField ButtonType="Button" HeaderText="退报名" Text="退报名"CommandName="Btcxtbm" />

CS页面:

  protected void GVxkall_RowCommand(object sender, GridViewCommandEventArgs e) { int rowIndex = Int32.Parse((String)e.CommandArgument); if (e.CommandName == "Bttkall") { string m_whfs, m_xsxh, m_xq, m_lbdm, m_message; int m_kcxh; m_whfs = "学分制选课"; m_xq = (string)ViewState["xkxq"]; m_xsxh = (string)ViewState["xsxh"]; m_kcxh = 0; m_lbdm = GVxkall.DataKeys[rowIndex].Values[0].ToString(); SqlParameter[] parameters = new SqlParameter[5]; parameters[0] = new SqlParameter("@_whfs", m_whfs); parameters[1] = new SqlParameter("@_xh", m_xsxh); parameters[2] = new SqlParameter("@_xq", m_xq); parameters[3] = new SqlParameter("@_lbdm", m_lbdm); parameters[4] = new SqlParameter("@_kcxh", m_kcxh); DataSet dsalltk = DbHelperSQL.RunProcedure("pr_gx_ty_tx", parameters, "alltkmp"); m_message = dsalltk.Tables["alltkmp"].Rows[0]["message"].ToString(); this.Response.Write(" <script language=javascript>alert('" + m_message + "'); </script> "); BindView6GVxkall(); } }

注意:1、这里注意一下,只有ButtonField控件时,在GVxkall_RowCommand(object sender, GridViewCommandEventArgs e)事件中,e.CommandArgument中才会记录当前行的index值,而如果是模板中Button,是不会自动记录当前行的Index值,你只能像前面第1个写的那样,手动的为模板中的Button加入CommandArgument属性。

如果要想点击模板列中的Button,想获取当前行的RowIndex刚需要:

1.RowDataBound,把行号邦定到按钮的属性上:btn.Attributes["id"] = (e.Row.RowIndex) 2.gridview里面的按钮点击时,执行RowCommand

在这个事件里面e,转换成按钮,拿到(e as Button).Attributes["id"]

2、如果是在HeaderTemplate或FooterTemplate添加Button,如果不给这个Button加CommandArgument属性,因为点击它也要触发Gridview的RowCommand事件,因为RowCommand事件中有 int rowIndex = Int32.Parse((String)e.CommandArgument);这句话,而那Button中没有CommandArgument属性就会报错,解决方法就是给这个Button加CommandArgument属性,而且是int型的,如 <FooterTemplate> <asp:Button ID="BTNshjbxg" runat="server" OnClick="BTNshjbxg_Click" Text="修改审核级别" CommandArgument="0" /> </FooterTemplate>

这样就可以解决了,其实也可以把int rowIndex = Int32.Parse((String)e.CommandArgument)这句放在每个if判断里面,这样就不会一开始就去执行它也就不会报错

为了统一格式,便于查看,GridView中所有的Button都可以加CommandName 属性,然后在RowCommand事件中编写Button点击后的命令,而不用再写Button_Click事件了。

protected void GVshjb_RowCommand(object sender, GridViewCommandEventArgs e) { int rowIndex = Int32.Parse((String)e.CommandArgument); if (e.CommandName == "Xzyh") { //this.TXTtest.Text = GVshjb.DataKeys[rowIndex].Values[0].ToString(); this.TXTtest.Text = e.CommandArgument.ToString(); int m_shqxdj = int.Parse(e.CommandArgument.ToString()); string m_zcdm = ""; string m_whfs = "按权限等级查询教师信息";

SqlParameter[] parameters = new SqlParameter[3]; parameters[0] = new SqlParameter("@_whfs", m_whfs); parameters[1] = new SqlParameter("@_dm", m_shqxdj); parameters[2] = new SqlParameter("@_zcdm", m_zcdm);

DataSet dslcnew = DbHelperSQL.RunProcedure("pr_jsdm_sele", parameters, "nlcmp");

this.GVqxdjyh.DataSource = dslcnew; this.GVqxdjyh.DataBind();

} }

原文地址:https://www.cnblogs.com/yangwujun/p/5051839.html