protected void gviewEmployee_SelectedIndexChanged(object sender, EventArgs e) { Label txtMsg = new Label(); switch (gviewEmployees.Rows[gviewEmployees.SelectedIndex].RowState) { case DataControlRowState.Selected: txtMsg.Text = "你选择奇数行,行状态为" + gviewEmployees.Rows[gviewEmployees.SelectedIndex].RowState + ": " + gviewEmployees.Rows[gviewEmployees.SelectedIndex].Cells[3].Text; break; case DataControlRowState.Alternate|DataControlRowState.Selected: txtMsg.Text="你选择偶数行,行状态为"+gviewEmployees.Rows[gviewEmployees.SelectedIndex].RowState+": "+gviewEmployees.Rows[gviewEmployees.SelectedIndex].Cells[3].Text; break; } Page.Controls.Add(txtMsg); }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["style"] = "Cursor:hand"; } }
原来最好的办法是这样: //建立奇数行与偶数行的onmouseover及onmouseout的颜色变换 if (Convert.ToInt16(ViewState["LineNo"]) == 0) { e.Row.BackColor = Color.FromArgb(255, 251, 214); e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFBD6';this.style.color='blue'"); e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#C0C0FF';this.style.color='#ffffff'");
ViewState["LineNo"] = 1; } else { e.Row.BackColor = Color.White; e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF';this.style.color='blue'"); e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#C0C0FF';this.style.color='#ffffff'");
ViewState["LineNo"] = 0; }
break;