閱讀1007 返回首頁    go 阿裏雲 go 技術社區[雲棲]


GridView使用技巧小結

①添加鼠標移動事件
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow ) { e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#C0C0FF';this.style.cursor='hand';"); //當鼠標移走時,還原該行的背景色。 e.Row.Attributes.Add("onmouseout", this.style.backgroundColor=currentcolor"); } }
②添加雙擊事件
【客戶端】 function ReKey(k) { alert("姓名"+k); } 【服務器端】 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if(e.Row.RowType==DataControlRowType.DataRow) { e.Row.Attributes.Add("ondblclick", "ReKey('" + e.Row.Cells[2].Text+"')"); } }
 ③添加鍵盤事件
【客戶端】 <mce:script language=javascript><!-- function GridViewItemKeyDownEvent(d) { window.alert("事件類型: GridViewItemKeyDownEvent 作用對象:" + d); } // --></mce:script> 【服務器端】 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if(e.Row.RowType==DataControlRowType.DataRow) { //鍵盤事件 e.Row.Attributes.Add("OnKeyDown", "GridViewItemKeyDownEvent('" + e.Row.Cells[1].Text + "')"); } }
 ④添加修改背景顏色事件(根據內容判斷)
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (e.Row.Cells[8].Text == "USA") { //e.Row.BackColor = System.Drawing.Color.Red; e.Row.Cells[8].BackColor = System.Drawing.Color.Red; } } }
⑤添加全選效果
protected void CheckBox2_CheckedChanged(object sender, EventArgs e) { int i; if (((CheckBox)sender).Checked) { for (i = 0; i < GridView1.Rows.Count; i++) { ((CheckBox)GridView1.Rows[i].FindControl("CheckBox1")).Checked = true; } } else { for (i = 0; i < GridView1.Rows.Count; i++) { ((CheckBox)GridView1.Rows[i].FindControl("CheckBox1")).Checked =false; } } }
  ⑥添加刪除確認效果
<asp:TemplateField HeaderText="刪除" ShowHeader="False"> <ItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete" OnClientClick='return confirm("確認要刪除嗎?")' Text="刪除"></asp:LinkButton> </ItemTemplate> </asp:TemplateField>
⑦導出到Excel文件方法
protected void Button1_Click(object sender, EventArgs e) { Response.Clear(); Response.Buffer = true; Response.Charset = "GB2312"; Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls"); // 如果設置為GetEncoding("GB2312");導出的文件將會出現亂碼。 Response.ContentEncoding = System.Text.Encoding.UTF7; Response.ContentType = "application/ms-excel";//設置輸出文件類型為excel文件。 System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); this.GridView1.RenderControl(oHtmlTextWriter); Response.Output.Write(oStringWriter.ToString()); Response.Flush(); Response.End(); }
⑧不使用數據源控件的GridView。(隻顯示標題,不顯示內容)
思路:添加一空行; private void AddDummyData(DataSet ds) { // Add a dummy row DataTable dt = ds.Tables[0]; DataRow newRow = dt.NewRow(); dt.Rows.Add(newRow); } 
 

 

最後更新:2017-04-02 04:01:43

  上一篇:go GNU通用公共授權(GNU General Public License)中英文版全文
  下一篇:go 文件上傳