日期:2014-05-17  浏览次数:20529 次

DataGrid无法响应ItemCommand事件,求教
<asp:Button ID="see" runat="server" Text="查看密保卡" CommandName="See" />

 protected void Data_g_ItemCommand(object source, DataGridCommandEventArgs e)
  {
  if (e.CommandName == "See")
  {
  card.CardID = Data_g.DataKeys[e.Item.ItemIndex].ToString();
  DataSet ds = card.SelCardbyCardID();
  ds.Dispose();
  string url = card.RUL;
  this.Image1.ImageUrl = "mbimages/" + url + "";
  }
  }


情况:点击按钮 提示:回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page EnableEventValidation="true" %> 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。 

然后我加上了EnableEventValidation="false" 

再次点击按钮,ItemCommand事件没反映,我在里头设置了断点,没有跳进去,求教



------解决方案--------------------
4种方法都可以实现
如果你不行,就直接拷贝粘贴源代码存成test.aspx浏览

HTML code
<%@ Page Language="C#" EnableViewState="true" EnableEventValidation="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">


  public System.Data.DataTable CreateDataTable()
  {
    System.Data.DataTable dataTable1 = new System.Data.DataTable("BlogUser");
    System.Data.DataRow dr;
    dataTable1.Columns.Add(new System.Data.DataColumn("Id", typeof(System.Int32)));
    dataTable1.Columns.Add(new System.Data.DataColumn("Title", typeof(System.String)));
    for (int i = 0; i < 8; i++)
    {
      dr = dataTable1.NewRow();
      dr[0] = i;
      dr[1] = Guid.NewGuid().ToString();
      dataTable1.Rows.Add(dr);
    }
    return dataTable1;
  }

  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      Data_g.DataSource = this.CreateDataTable();
      Data_g.DataBind();
    }
  }
  protected void Data_g_ItemCommand(object source, DataGridCommandEventArgs e)
  {
    if (e.CommandName == "See")
    {
      Response.Write("See 引发");
    }

    if (e.CommandName == "SeeSee")
    {
      Response.Write("SeeSee 引发");
    }
    Response.Write("<li>该行Id=" + Data_g.DataKeys[e.Item.ItemIndex].ToString());
  }

  protected void see3_Click(object sender, EventArgs e)
  {
    Button s = sender as Button;
    Response.Write("<li>方法3得到的Id=" + s.CommandArgument);
  }

  protected void see4_Click(object sender, EventArgs e)
  {
    Button s = sender as Button;
    DataGridItem item = s.NamingContainer as DataGridItem;
    Response.Write("<li>方法4得到的Id" + Data_g.DataKeys[item.ItemIndex].ToString());
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <title>无标题页</title>
</head>
<body>
  <form id="form1" runat="server">
  <asp:DataGrid ID="Data_g" runat="server" OnItemCommand="Data_g_ItemCommand" DataKeyField="Id"
    AutoGenerateColumns="false">
    <Columns>
      <asp:ButtonColumn ButtonType="PushButton" CommandName="SeeSee" Text="查看密保卡方法1"></asp:ButtonColumn>
      <asp:TemplateColumn>
        <ItemTemplate>
          <asp:Button ID="see" runat="server" Text="查看密保卡方法2" CommandName="See" />
        </ItemTemplate>
      </asp:TemplateColumn>