html页面:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="merID" HeaderText="商品ID" />
<asp:BoundField DataField="merName" HeaderText="商品名称" />
<asp:BoundField DataField="providerid" HeaderText="供应商ID" />
<asp:BoundField DataField="categoryID" HeaderText="分类" />
</Columns>
</asp:GridView>
cs文件代码:
public partial class GridViewSample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)//添加判断会报错
{
BusinessLogical.DataSource dataSource = new BusinessLogical.DataSource();
this.GridView1.DataSource = dataSource.GetMerchandises("");
this.GridView1.DataBind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
PostBackOptions myPostBackOptions = new PostBackOptions(this);
myPostBackOptions.AutoPostBack = false;
myPostBackOptions.RequiresJavaScriptProtocol = true;
myPostBackOptions.PerformValidation = false;
String evt = Page.ClientScript.GetPostBackClientHyperlink(sender as System.Web.UI.WebControls.GridView, "Select$" + e.Row.RowIndex.ToString());
if (e.Row.RowType == DataControlRowType.DataRow)
{
//单击事件
e.Row.Attributes.Add("onclick", evt);
//当鼠标停留时更改背景色
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#A9A9A9'");
//当鼠标移开时还原背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
//设置悬浮鼠标指针形状为"小手"
e.Row.Attributes["style"] = "Cursor:hand";
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Write(this.GridView1.SelectedIndex);
}
}
Page_Load中不加IsPostBack判断,正常;否则,网页就报错
群策群力,大家帮帮忙,谢谢了,^_^
[此贴子已经被作者于2007-8-12 9:00:12编辑过]