新手入门碰到难题,请大神们帮帮忙!关于gridview
新手入门遇到难题,请大神们帮帮忙!!关于gridview
具体代码是这样的
protected void Button2_Click(object sender, EventArgs e)
{
int row = ((GridViewRow)((Button)sender).NamingContainer).RowIndex;
string Bno = GridView1.Rows[row].Cells[0].ToString();
Convert.ToInt32(Bno);
string No = Session["No"].ToString();
Convert.ToInt32(No);
DateTime time = DateTime.Today;
SqlConnection b = new SqlConnection(ConfigurationManager.ConnectionStrings["bookConnectionString"].ConnectionString);
string sql2 = "insert into lend(Bno,No,Ltime) values('"+Bno+"','"+No+"',"+time+")";
SqlDataAdapter da = new SqlDataAdapter(sql2, b);
DataSet ds = new DataSet();
try
{
da.Fill(ds, "lend");
}
catch (SqlException ex)
{
throw new Exception(ex.Message, ex);
}
finally
{
Response.Write("<script>alert('借阅成功!')</script>");
Console.ReadLine();
Response.Redirect("adminhomepage.aspx");
}
button2是一个gridview中ItemTemplate的一个按钮,点击后发生了如下错误
回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page EnableEventValidation="true" %> 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。
------解决方案--------------------
试试
try
{
da.Fill(ds, "lend");
Response.Write("<script>alert('借阅成功!');window.location.href='adminhomepage.aspx'</script>");
}
catch (SqlException ex)
{
throw new Exception(ex.Message, ex);
}
------解决方案--------------------
不要用response.write 来写,如果要弹出js框的话,你注册脚本来执行js就可以了
ScriptManage.registerstartupscript注册alert('借阅成功!');window.location.href='adminhomepage.aspx就可以了。
------解决方案--------------------
应该不是这样写事件的,你的BUTTON2在GRIDVIEW中的模板里,HTML解析有多条数据时你的BUTTON2就有多个了,就会有问题了.
应该用AJAX处理事件,或写在绑定事件中触发事件BUTTON2.试一下.
------解决方案--------------------
这种应该用 item_command吧
具体代码是这样的
protected void Button2_Click(object sender, EventArgs e)
{
int row = ((GridViewRow)((Button)sender).NamingContainer).RowIndex;
string Bno = GridView1.Rows[row].Cells[0].ToString();
Convert.ToInt32(Bno);
string No = Session["No"].ToString();
Convert.ToInt32(No);
DateTime time = DateTime.Today;
SqlConnection b = new SqlConnection(ConfigurationManager.ConnectionStrings["bookConnectionString"].ConnectionString);
string sql2 = "insert into lend(Bno,No,Ltime) values('"+Bno+"','"+No+"',"+time+")";
SqlDataAdapter da = new SqlDataAdapter(sql2, b);
DataSet ds = new DataSet();
try
{
da.Fill(ds, "lend");
}
catch (SqlException ex)
{
throw new Exception(ex.Message, ex);
}
finally
{
Response.Write("<script>alert('借阅成功!')</script>");
Console.ReadLine();
Response.Redirect("adminhomepage.aspx");
}
button2是一个gridview中ItemTemplate的一个按钮,点击后发生了如下错误
回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page EnableEventValidation="true" %> 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。
------解决方案--------------------
试试
try
{
da.Fill(ds, "lend");
Response.Write("<script>alert('借阅成功!');window.location.href='adminhomepage.aspx'</script>");
}
catch (SqlException ex)
{
throw new Exception(ex.Message, ex);
}
------解决方案--------------------
不要用response.write 来写,如果要弹出js框的话,你注册脚本来执行js就可以了
ScriptManage.registerstartupscript注册alert('借阅成功!');window.location.href='adminhomepage.aspx就可以了。
------解决方案--------------------
应该不是这样写事件的,你的BUTTON2在GRIDVIEW中的模板里,HTML解析有多条数据时你的BUTTON2就有多个了,就会有问题了.
应该用AJAX处理事件,或写在绑定事件中触发事件BUTTON2.试一下.
------解决方案--------------------
这种应该用 item_command吧