|
 |
栏目导栏 |
|
| |
|
|
|
|
 |
资料搜索 |
|
| |
|
|
|
|
 |
热门文章 |
|
| |
|
|
|
|
 |
最新文章 |
|
| |
|
|
|
| |
| |
|
|
|
| |
| datagrid技巧之一:代码控制选中行的颜色 览和打印 |
|
大家都知道datagrid是一个使用频率很高的控件,在编写ASP.NET代码的时候,我们总希望能让用户选中指定的行以后,让那一行用不同的颜色显示,虽然datagrid样式也有这个功能,但是我们如何编写代码实现呢? gG5Linux联盟 gG5Linux联盟 在本例子中,我们首先动态产生1000行,然后当用户选中datagrid中的某一行的时候,那一行就会变为蓝色。代码如下: gG5Linux联盟 gG5Linux联盟 using System; gG5Linux联盟 using System.Collections; gG5Linux联盟 using System.ComponentModel; gG5Linux联盟 using System.Data; gG5Linux联盟 using System.Drawing; gG5Linux联盟 using System.Web; gG5Linux联盟 using System.Web.SessionState; gG5Linux联盟 using System.Web.UI; gG5Linux联盟 using System.Web.UI.WebControls; gG5Linux联盟 using System.Web.UI.HtmlControls; gG5Linux联盟 gG5Linux联盟 namespace WebApplication_rd gG5Linux联盟 { gG5Linux联盟 /// <summary> gG5Linux联盟 /// Demostrates how to have a datagrid server control be bookmarked gG5Linux联盟 /// </summary> gG5Linux联盟 public class datagrid : System.Web.UI.Page gG5Linux联盟 { gG5Linux联盟 gG5Linux联盟 protected System.Web.UI.WebControls.DataGrid DGProducts; gG5Linux联盟 int bookmarkIndex = 0; // The index of the row that should be scrolled to gG5Linux联盟 int itemCount = 0; // Counter for the amount of items on the page gG5Linux联盟 bool bookMark = true; // Controls whether or not the page is bookmarked gG5Linux联盟 gG5Linux联盟 #region DGProducts EventHandlers gG5Linux联盟 gG5Linux联盟 private void DGProducts_ItemDataBound(object source, DataGridItemEventArgs e) { gG5Linux联盟 gG5Linux联盟 if (bookMark) { gG5Linux联盟 gG5Linux联盟 LiteralControl anchor = new LiteralControl(); gG5Linux联盟 anchor.Text = "<a name=\"" + itemCount.ToString() + "\">"; gG5Linux联盟 itemCount ++; gG5Linux联盟 e.Item.Cells[0].Controls.Add(anchor); gG5Linux联盟 gG5Linux联盟 } gG5Linux联盟 gG5Linux联盟 } gG5Linux联盟 gG5Linux联盟 private void DGProducts_ItemCommand(object source, DataGridCommandEventArgs e) { gG5Linux联盟 gG5Linux联盟 if (e.CommandName == "Select") { gG5Linux联盟 gG5Linux联盟 e.Item.BackColor = Color.Blue; gG5Linux联盟 gG5Linux联盟 if (bookMark) { gG5Linux联盟 bookmarkIndex = e.Item.ItemIndex; gG5Linux联盟 this.InsertScriptBlock(); gG5Linux联盟 } gG5Linux联盟 } gG5Linux联盟 gG5Linux联盟 } gG5Linux联盟 gG5Linux联盟 gG5Linux联盟 #endregion gG5Linux联盟 gG5Linux联盟 #region EventHandlers gG5Linux联盟 private void Page_Load(object sender, System.EventArgs e) gG5Linux联盟 { gG5Linux联盟 gG5Linux联盟 this.Bind(); gG5Linux联盟 gG5Linux联盟 } gG5Linux联盟 gG5Linux联盟 #endregion gG5Linux联盟 gG5Linux联盟 #region User Defined gG5Linux联盟 gG5Linux联盟 private void InsertScriptBlock() { gG5Linux联盟 gG5Linux联盟 System.Text.StringBuilder jScript = new System.Text.StringBuilder(); gG5Linux联盟 jScript.Append("<script language=\"JavaScript\">"); gG5Linux联盟 jScript.Append("location.href=\"#"); gG5Linux联盟 jScript.Append(this.bookmarkIndex.ToString()); gG5Linux联盟 jScript.Append("\";"); gG5Linux联盟 jScript.Append("</script>"); gG5Linux联盟 gG5Linux联盟 this.RegisterClientScriptBlock("Bookmark", jScript.ToString()); gG5Linux联盟 gG5Linux联盟 } gG5Linux联盟 gG5Linux联盟 gG5Linux联盟 private void Bind() gG5Linux联盟 { gG5Linux联盟 gG5Linux联盟 DGProducts.DataSource = this.CreateDataSource(1000); gG5Linux联盟 DGProducts.DataBind(); gG5Linux联盟 gG5Linux联盟 } gG5Linux联盟 gG5Linux联盟 gG5Linux联盟 private DataTable CreateDataSource(int count) { gG5Linux联盟 gG5Linux联盟 DataTable table = new DataTable(); gG5Linux联盟 DataColumn column = null; gG5Linux联盟 DataRow row = null; gG5Linux联盟 gG5Linux联盟 // Create 5 columns gG5Linux联盟 for (int iCol = 0; iCol < 5; iCol++) { gG5Linux联盟 gG5Linux联盟 column = new DataColumn("Column: " + iCol.ToString(), typeof(string)); gG5Linux联盟 table.Columns.Add(column); gG5Linux联盟 gG5Linux联盟 } gG5Linux联盟 gG5Linux联盟 //Create Rows based on count variable gG5Linux联盟 for (int iRows = 0; iRows < count; iRows ++) { gG5Linux联盟 gG5Linux联盟 row = table.NewRow(); gG5Linux联盟 gG5Linux联盟 for (int iCol = 0; iCol < 5; iCol ++) { gG5Linux联盟 gG5Linux联盟 row[iCol] = "Value: " + iCol.ToString(); gG5Linux联盟 gG5Linux联盟 gG5Linux联盟 } gG5Linux联盟 gG5Linux联盟 table.Rows.Add(row); gG5Linux联盟 gG5Linux联盟 } gG5Linux联盟 gG5Linux联盟 return table; gG5Linux联盟 gG5Linux联盟 } gG5Linux联盟 gG5Linux联盟 gG5Linux联盟 #endregion gG5Linux联盟 gG5Linux联盟 #region Web Form Designer generated code gG5Linux联盟 override protected void OnInit(EventArgs e) gG5Linux联盟 { gG5Linux联盟 InitializeComponent(); gG5Linux联盟 base.OnInit(e); gG5Linux联盟 } gG5Linux联盟 gG5Linux联盟 private void InitializeComponent() gG5Linux联盟 { gG5Linux联盟 this.DGProducts.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DGProducts_ItemCommand); gG5Linux联盟 this.DGProducts.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DGProducts_ItemDataBound); gG5Linux联盟 this.Load += new System.EventHandler(this.Page_Load); gG5Linux联盟 gG5Linux联盟 } gG5Linux联盟 #endregion gG5Linux联盟 } gG5Linux联盟 gG5Linux联盟 }
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论 |
|
|
|
|
|