DataGrid 模板列的问题(在线等)

liaoxiaohua1981 liaoxiaohua1981 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我在DataGrid添加了一个模板列,然后再编辑模板列时加了一个Button控件,
暂时没有加任何事件,可我在点这个Button后,出来一个错误:

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

这是怎么回事?

.cs代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class webtest1 : System.Web.UI.Page
{
private string sqlStr = System.Configuration.ConfigurationManager.AppSettings["Sqlstr"].ToString();
private DataSet ds;
private DataView dv;
public static SqlConnection sqlConn;
protected void Page_Load(object sender, EventArgs e)
{
sqlConn = new SqlConnection(sqlStr);
bindData();
if (!Page.IsPostBack)
{ }

}
private void bindData()
{
// using (sqlConn)
// {
sqlConn.Open();
SqlCommand cmd = new SqlCommand("SPBookInfo", sqlConn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter parameter1 = new SqlParameter("@BookAdress", SqlDbType.VarChar, 20);
parameter1.Value = "B";
cmd.Parameters.Add(parameter1);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
ds = new DataSet();
da.Fill(ds);
dv = ds.Tables[0].DefaultView;
Session["dv"] = dv;
BookDG.DataSource = dv;

BookDG.DataBind();
da.Dispose();
sqlConn.Close();

// }
}


protected void BookDG_ItemCommand(object source, DataGridCommandEventArgs e)
{
if (e.CommandName == "ghgfh")
{
bindData();

}
}
}

.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="webtest1.aspx.cs" Inherits="webtest1" %>

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

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div style="text-align: center">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="left" colspan="3" style="height: 120px" valign="top">
<asp:DataGrid ID="BookDG" runat="server" Height="120px" Width="313px" OnItemCommand="BookDG_ItemCommand" >
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
 <asp:Button ID="Button1" runat="server" CommandName="ghgfh" Text="Button" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid></td>
</tr>
<tr>
<td align="center" style="width: 100px; height: 19px" valign="bottom">
</td>
<td align="center" style="width: 100px; height: 19px" valign="bottom">
</td>
<td align="center" style="width: 100px; height: 19px" valign="bottom">
</td>
</tr>
</table>
</div>

</div>
</form>
</body>
</html>

...全文
评论 2
收藏 已收藏
分享
返回顶部
liaoxiaohua1981
.NET社区 62,271 成员 668,968 内容