e.CommandArgument输入字符串的格式不正确?

本文介绍在ASP.NET2.0中使用GridView控件时,如何解决模版列TemplateField中LinkButton的RowCommand事件中获取当前行索引的问题。通过在RowCreated事件中设置LinkButton的CommandArgument属性为行索引,可以在RowCommand事件中正确获取到行索引。

 http://www.cnblogs.com/xiaofengfeng/archive/2010/09/23/1833627.html

 

ASP.NET2.0中的GRIDVIEW控件在使用TemplateField中的LinkButton时如何在RowCommand事件中找到当前行index的方法
  ASP.NET2.0中的GRIDVIEW控件真是非常奇怪,不知道MS是怎么考虑的,在GRIDVIEW里,行索引被放在了CommandArgument里面,而不是像DataGrid那样可以利用this.MyDataGrid.DataKeys[e.Item.ItemIndex].ToString()方便的取出主键值,
同时我们注意到,如果使用默认的CommandField,
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
 则可以在RowCommand中使用如下代码取出行号:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//编辑按扭
if (e.CommandName == "Edit")
{
      //设置编辑行高亮显示
      this.GridView1.EditRowStyle.BackColor = Color.FromName("#F7CE90");
//string index= this.GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString();
      int index = Convert.ToInt32(e.CommandArgument);
      GridViewRow row = GridView1.Rows[index];
      string xh3 = row.Cells[3].Text;
}
}

但问题是,CommandField的可操控性是很底的,我们一般习惯于使用模版列来订制操作按钮,如下:
<asp:TemplateField HeaderText="操作" ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="更新"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="编辑" ></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Select" Text="选择"></asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" CommandName="Delete" Text="删除" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>


随之而来,问题出现了,运行报错:输入字符串的格式不正确, Convert.ToInt32(e.CommandArgument)中e.CommandArgument转换为字符串为空。当我们把CommandField转换为模版列时,默认的CommandArgument属性丢失了!!!
思考了两天,翻阅了网上的资料,最后在MSDN文档中发现,呈现 GridView 控件之前,必须先为该控件中的每一行创建一个 GridViewRow 对象。在创建 GridView 控件中的每一行时,将引发 RowCreated 事件。这使我们可以提供一个这样的事件处理方法,即每次发生此事件时都执行一个自定义例程(如在行中添加自定义内容,当然也可以添加e.CommandArgument属性为模版列里的LinkButton)。
GridViewRowEventArgs 对象将被传给事件处理方法,随之我们可以访问正在创建的行的属性。若要访问行中的特定单元格,可以使用 GridViewRowEventArgs 对象的 Cells 属性。使用 RowType 属性可确定正在创建的是哪一种行类型(标题行、数据行等等)。
好了,原来我们可以利用RowCreated事件来为模版列中LinkButton写入CommandArgument事件。
下面的代码示例演示如何使用 RowCreated 事件将正在创建的行的索引存储在该行中所包含的 LinkButton 控件的 CommandArgument 属性中。这允许您确定在用户单击 LinkButton 控件按钮时包含该控件的行的索引。

/// <summary>
/// 为模版列LinkButton写入CommandArgument事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
/ Retrieve the LinkButton control from the first column.
LinkButton LinkButton1 = (LinkButton)e.Row.FindControl("LinkButton1");
// Set the LinkButton's CommandArgument property with the row's index.
LinkButton1.CommandArgument = e.Row.RowIndex.ToString();
}

}

好了,其余的代码不变,在GridView1_RowCommand中的代码不变.

 

前端代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="listview增删改查._default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <style type="text/css"> .divItem { padding: 5px; } .divBtns { width:130px; } .divInfo { width:350px; padding:0px 10px 0px 10px; border-left:1px solid #dadada; border-right:1px solid #dadada; } .divInfo2 { width:200px; } .divBtns, .divInfo, .divInfo2 { float:left; } .clr { clear:both; } </style> </head> <body> <form id="form1" runat="server"> <div> <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource2" ItemPlaceholderID="itemholder"> <LayoutTemplate> <div runat="server" id="itemholder"></div> </LayoutTemplate> <ItemTemplate> <div id="divItem"> <div class="divBtns"> <asp:Button ID="btn_edit" runat="server" Text="编辑" CommandName="edit" /><br/> <asp:Button ID="btn_delete" runat="server" Text="删除" CommandName="delete" /> <asp:Button ID="btn_delmak" runat="server" Text="标记删除" CommandName="lvcmd" CommandArgument='<%#Eval("se_id") %>' onCommand="mycmd" /> </div> <div class="divInfo"> <h2><%#Eval("se_name") %></h2> <p><%#Eval("se_memo") %></p> </div> <div class="divInfo2"> <asp:CheckBox ID="chk_enable" runat="server" Checked='<%#Eval("se_enable") %>' Enabled="false" Text="有效性" /><br/> <p>排序:<%#Eval("se_order") %></p> </div> <div class="clr"></div> </div> </ItemTemplate> </asp:ListView> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString9 %>" DeleteCommand="DELETE FROM [T_SERIES] WHERE [se_id] = ?" InsertCommand="INSERT INTO [T_SERIES] ([se_id], [se_name], [se_memo], [se_order], [se_enable], [se_delete]) VALUES (?, ?, ?, ?, ?, ?)" ProviderName="<%$ ConnectionStrings:ConnectionString9.ProviderName %>" SelectCommand="SELECT * FROM [T_SERIES] WHERE SE_DELETE=FALSE" UpdateCommand="UPDATE [T_SERIES] SET [se_name] = ?, [se_memo] = ?, [se_order] = ?, [se_enable] = ?, [se_delete] = ? WHERE [se_id] = ?"> <DeleteParameters> <asp:Parameter Name="se_id" Type="Int32" /> </DeleteParameters> <InsertParameters> <asp:Parameter Name="se_id" Type="Int32" /> <asp:Parameter Name="se_name" Type="String" /> <asp:Parameter Name="se_memo" Type="String" /> <asp:Parameter Name="se_order" Type="Int32" /> <asp:Parameter Name="se_enable" Type="Boolean" /> <asp:Parameter Name="se_delete" Type="Boolean" /> </InsertParameters> <UpdateParameters> <asp:Parameter Name="se_name" Type="String" /> <asp:Parameter Name="se_memo" Type="String" /> <asp:Parameter Name="se_order" Type="Int32" /> <asp:Parameter Name="se_enable" Type="Boolean" /> <asp:Parameter Name="se_delete" Type="Boolean" /> <asp:Parameter Name="se_id" Type="Int32" /> </UpdateParameters> </asp:SqlDataSource> </div> </form> </body> </html> 后端代码: using System; using System.Collections.Generic; using System.Data.OleDb; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace listview增删改查 { public partial class _default : System.Web.UI.Page { string str_cnn = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source="; string str_sourcefile = "mdb\\TB-HLINKS.accdb"; OleDbConnection cnn; OleDbCommand cmd; OleDbDataReader datar; string str_sql; protected void Page_Load(object sender, EventArgs e) { } protected void mycmd(object sender, CommandEventArgs e) { string str_conn = str_cnn + MapPath(str_sourcefile); cnn = new OleDbConnection(str_conn); cnn.Open(); str_sql = "update T_SERIES set se_delete =true where " + "se_id =" + e.CommandArgument.ToString(); cmd = new OleDbCommand(str_sql, cnn); cmd.ExecuteNonQuery(); cnn.Close(); ListView1.DataBind(); } } } 能更改数据,什么原因呢?
最新发布
08-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值