我想实现选定的功能(如gridview)
aspx代码:
<%@ Page Language="C#" CodeFile="Default.aspx.cs" Inherits="_Default" Debug="true" EnableEventValidation="true"%><!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>
<asp:DataList ID="DataList1" runat="server" OnSelectedIndexChanged="DataList1_SelectedIndexChanged" CellPadding="4" ForeColor="#333333">
<ItemTemplate>
<div>
123
<asp:Button ID="Button1" runat="server" Text="Button" CommandName="select" /></div>
</ItemTemplate>
<SelectedItemTemplate>
kkkk
</SelectedItemTemplate>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<AlternatingItemStyle BackColor="White" ForeColor="#284775" />
<ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
</asp:DataList> </div>
</form>
</body>
</html>
aspx.cs代码:
using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
static string conn = SqlHelper.ConnectionStringLocalTransaction; static string select = "SELECT * FROM Table1"; static SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.Text, select, null);
protected void Page_Load(object sender, EventArgs e)
{
Bind();
} public void Bind()
{
DataList1.DataSource = dr;
DataList1.DataBind();
} protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
{
DataList1.DataBind();
}
}其中,数据库 表为Table1,有name列现在运行这个程序,点击按钮后,会出错
但是如果取消dr的绑定,换成数据源控件,再绑到datalist上,就可以正常运行了。。
不知道自己哪里写错了,大家帮我改下,谢谢
aspx代码:
<%@ Page Language="C#" CodeFile="Default.aspx.cs" Inherits="_Default" Debug="true" EnableEventValidation="true"%><!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>
<asp:DataList ID="DataList1" runat="server" OnSelectedIndexChanged="DataList1_SelectedIndexChanged" CellPadding="4" ForeColor="#333333">
<ItemTemplate>
<div>
123
<asp:Button ID="Button1" runat="server" Text="Button" CommandName="select" /></div>
</ItemTemplate>
<SelectedItemTemplate>
kkkk
</SelectedItemTemplate>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<AlternatingItemStyle BackColor="White" ForeColor="#284775" />
<ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
</asp:DataList> </div>
</form>
</body>
</html>
aspx.cs代码:
using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
static string conn = SqlHelper.ConnectionStringLocalTransaction; static string select = "SELECT * FROM Table1"; static SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.Text, select, null);
protected void Page_Load(object sender, EventArgs e)
{
Bind();
} public void Bind()
{
DataList1.DataSource = dr;
DataList1.DataBind();
} protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
{
DataList1.DataBind();
}
}其中,数据库 表为Table1,有name列现在运行这个程序,点击按钮后,会出错
但是如果取消dr的绑定,换成数据源控件,再绑到datalist上,就可以正常运行了。。
不知道自己哪里写错了,大家帮我改下,谢谢
{
Bind();
}
第一次加载时才绑定数据源
回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page EnableEventValidation="true" %> 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。
这个错误在返回sqldatareader时也出现过
是不是datalist中的按钮的命令要写下?我想切换到selectitemtemplate模板
你上面的代码错误很大
<asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand">
<SelectedItemTemplate>
这里是选择摸板
</SelectedItemTemplate>
<ItemTemplate>
<dir>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("BookName") %>'></asp:Label>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("BookID") %>'></asp:Label>
<asp:Button ID="Button2" runat="server" Text="Button" CommandName="select" />
</dir>
</ItemTemplate>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<AlternatingItemStyle BackColor="White" ForeColor="#284775" />
<ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
</asp:DataList>
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "select")
{
this.DataList1.SelectedIndex = e.Item.ItemIndex;
Bind();
}
} protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Bind();
}
}这是调试通过了的
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
} public void Bind()
{
string conn = SqlHelper.ConnectionStringLocalTransaction; string select = "SELECT * FROM Table1"; SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.Text, select, null);
DataList1.DataSource = dr; // Insus.NET建议改为DataSet
DataList1.DataBind();
} protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
{
DataList1.DataBind();
}