public class ExcelButton : System.Web.UI.WebControls.Button
{
public ExcelButton()
{
//
//TODO: 在此处添加构造函数逻辑
//
} private System.Data.DataTable myTable;
public System.Data.DataTable MyTable
{
get { return myTable; }
set { myTable = value; }
}
}如上,小弟建立一个class,继承了Button
在操作页面小第实例化一个ExcelButton 如下 ExcelButton ebtn = new ExcelButton();
DataTable dtMain = SetPrinInfo(table);
ebtn.MyTable = dtMain;
ebtn.Text = "打印";
ebtn.Click += new EventHandler(ebtn_Click);
但他在前台却给我生成了个这 如下:<td colspan="16" rowspan="1" align="right"><input type="submit" name="ctl147" value="打印" /></td>
求大侠指导,我就想知道为什么没有生成一个服务器控件呢?我改怎么做呢?
ASP.NET 2.0服务器控件之客户端功能
我的意思是它的Click 事件不管用,是因为我没有设置ID吗?
System.Web.UI.HtmlControls.HtmlTableRow trPrint = new System.Web.UI.HtmlControls.HtmlTableRow();
System.Web.UI.HtmlControls.HtmlTableCell tcPrint = new System.Web.UI.HtmlControls.HtmlTableCell();
tcPrint.ColSpan = 16;
tcPrint.RowSpan = 1;
tcPrint.Align = "right";
tcPrint.Controls.Add(ebtn);
trPrint.Cells.Add(tcPrint);
table.Rows.Add(trPrint);
这样加的控件
protected override void RenderChildren(System.Web.UI.HtmlTextWriter writer)
{
base.RenderChildren(writer);
}
class ExcelButtoncs:System.Web.UI.WebControls.Button
{ private System.Data.DataTable myTable;
public System.Data.DataTable MyTable
{
get { return myTable; }
set { myTable = value; }
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.Click += new EventHandler(ExcelButtoncs_Click);
} void ExcelButtoncs_Click(object sender, EventArgs e)
{
//在这里生成你的EXCEL
}
}
protected void ebtn_Click(object sender, EventArgs e)
{
ExcelButton ebtn = sender as ExcelButton;
if (ebtn != null)
{
DataTable dtone = ebtn.MyTable;
try
{
if (dtone != null && dtone.Rows.Count > 0)
{
Aspose.Cells.Workbook wk = new Aspose.Cells.Workbook();
wk.Open(System.Web.HttpContext.Current.Server.MapPath(@"~\Vacation\WordModel\PrintModel.xls"));
AddExcelSHeet1(wk, dtone);
string filename = Guid.NewGuid().ToString();
wk.Save(System.Web.HttpContext.Current.Server.MapPath(@"~\Vacation\Upload\" + filename + ".xls"));
filedown(System.Web.HttpContext.Current.Server.MapPath(@"~\Vacation\Upload\" + filename + ".xls"));
}
}
catch (Exception)
{ Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('导出出错了?');</script>");
}
}
}这个我知道,但是,这个button就是没有执行这个OnClick事件,我调试,根本没进去,只是刷新了下页面