【分享】自定义水晶报表工具栏导出按钮事件WinForm & WebForm版
1:WebForm版
这个比较简单,工具栏点击后是一系列js事件在运作,只要我们找到目标文件即可
以VS2008自带版本的水晶报表为例
部署路径
C:\inetpub\wwwroot\aspnet_client\system_web\2_0_50727\CrystalReportWebFormViewer4\js
C:\Windows\Microsoft.NET\Framework\v2.0.50727\ASP.NETClientFiles\CrystalReportWebFormViewer4\js
if( rpt ) { list += "<OPTION value=\"CrystalReports\">" + L_CrystalRptFormat + "</OPTION>"; }using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using CrystalDecisions.Shared;using CrystalDecisions.CrystalReports.Engine;using CrystalDecisions.Windows.Forms;namespace CrApp_OLAP2{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } //在Load时触发 private void Form1_Load(object sender, EventArgs e) { ReplaceExportButton(); } //自定义事件 private void myEvent000(object sender, EventArgs e) { MessageBox.Show("Here"); } //核心 private void ReplaceExportButton() { //遍历crystalReportViewer1控件里的控件 foreach (object ctl in crystalReportViewer1.Controls) { //取得控件名称 string sControl = ctl.GetType().Name.ToString().ToLower(); //取得工具条 if (sControl == "toolstrip") { ToolStrip tab1 = (ToolStrip)ctl; //遍历工具条Item for (int i = 0; i <= tab1.Items.Count - 1; i++) { //MessageBox.Show(tab1.Items[i].ToolTipText); //如果是导出按钮 if (tab1.Items[i].ToolTipText == "导出报表") { //先创建一个ToolStripButton准备替代现有Button ToolStripButton tbutton = new ToolStripButton(); //获取原导出按钮的按钮图片 Image img1 = tab1.Items[i].Image; //移除原导出按钮 tab1.Items.Remove(tab1.Items[i]); //设置新button属性 tbutton.Image = img1; tbutton.ToolTipText = "自定义导出报表按钮"; //在原位置上插入新Button tab1.Items.Insert(0,tbutton); //绑定自定义事件 tbutton.Click += new System.EventHandler(this.myEvent000); break; } } } } } }}