嗨,这是我的aspx页面将一些值加载到用户控件
protected void Page_Load(object sender, EventArgs e)
{
}
这是我在加载并发送点击事件
中的值的用户控件protected void BtnFind_Click(object sender, EventArgs e)
{
Usr_BPOP BPOP = (Usr_BPOP)Page.LoadControl("~/Usr_BPOP.ascx");
BPOP.Date = txtDate.Text.Trim();
BPOP.DocNo = txtDocNo.Text.Trim();
BPOP.Code = txtCode.Text.Trim();
BPOP.Name = txtName.Text.Trim();
BPOP.Partcode = txtPartNo.Text.Trim();
if (chkReprint.Checked)
{
BPOP.BtnReprintVisible = true;
BPOP.BtnSaveVisible = false;
}
divControls.Controls.Clear();
PlaceHolder1.Controls.Add(BPOP);
}
这是我的Usr_BPOP.ascx:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
btnReprint.Click += new EventHandler(btnReprint_Click);
}
btnReprint.Visible = false;
btnSave.Visible = BtnSaveVisible;
btnReprint.Visible = BtnReprintVisible;
if (btnReprint.Visible == false)
{
btnReprint.Text = "Print";
btnReprint.Visible = true;
}
table = new DataTable();
table.Columns.Add("DocNum", typeof(string));
table.Columns.Add("DocEntry", typeof(string));
table.Columns.Add("LineNum", typeof(string));
table.Columns.Add("PartNo", typeof(string));
table.Columns.Add("ItemDesc", typeof(string));
table.Columns.Add("QTR", typeof(string));
table.Columns.Add("QTP", typeof(string));
table.Columns.Add("Chk", typeof(bool));
table.Columns.Add("BarCode", typeof(string));
Datalayer dl = new Datalayer();
DataTable dttable = new DataTable();
if (!BtnSaveVisible && BtnReprintVisible)
BtnSaveVisible = true;
dttable = dl.GetPOItem(date, docNo, code, name, partcode, BtnReprintVisible, !BtnSaveVisible).Tables[0];
foreach (DataRow dr in dttable.Rows)
{
table.Rows.Add(dr["DocNum"].ToString(), dr["DocEntry"].ToString(), dr["LineNum"].ToString(), dr["PartNo"].ToString(),
dr["ItemDesc"].ToString(), dr["QTR"].ToString(), dr["QTP"].ToString(), Convert.ToBoolean(dr["Chk"]), dr["Barcode"].ToString());
}
if (table != null && table.Rows.Count > 0)
{
grdlistofitems.DataSource = table;
Session["Table"] = table;
grdlistofitems.DataBind();
}
else
{
}
}
这是重新打印按钮点击事件,当我把这个事件cilck它没有触发时:
void btnReprint_Click(object sender, EventArgs e)
{
}
答案 0 :(得分:1)
由于您没有设置控件的ID,因此每次将控件添加到页面时都会重新生成控件的ID。生成的ID可能不相同,因此无法识别事件的发件人。所以你要做的第一件事就是明确地分配一个ID:
Usr_BPOP BPOP = (Usr_BPOP)Page.LoadControl("~/Usr_BPOP.ascx");
BPOP.ID = "SomeID";
其次,事件处理程序的赋值应该在创建控件的时候完成 - 也就是说,在每个请求上,无论这是否是回发都无关紧要 - 否则ASP.NET将无法确定是什么方法应该在事件被触发时调用:
protected void Page_Load(object sender, EventArgs e)
{
// No check for postback here
btnReprint.Click += new EventHandler(btnReprint_Click);
更新。还有一个原因导致此代码的行为不符合预期。 BPOP控件仅在btnFind
点击时添加到页面中。如果回发是由其他任何内容引起的,包括btnReprint
,则在响应页面上生成BPOP控件根本不会添加到页面中。如果页面上没有控件 - 显然无法触发其方法,包括事件处理程序。
这是针对这种情况的快速而肮脏的修复方法。它应该应用于添加BPOP控件的页面代码:
protected void Page_Load(object sender, EventArgs e)
{
bool? addBPOP = ViewState["AddBPOP"] as bool?;
if (addBPOP.HasValue && addBPOP.Value)
{
AddBPOP();
}
}
protected void BtnFind_Click(object sender, EventArgs e)
{
AddBPOP();
ViewState["AddBPOP"] = true;
}
protected void AddBPOP()
{
Usr_BPOP BPOP = (Usr_BPOP)Page.LoadControl("~/Usr_BPOP.ascx");
BPOP.ID = "BPOPID";
BPOP.Date = txtDate.Text.Trim();
BPOP.DocNo = txtDocNo.Text.Trim();
BPOP.Code = txtCode.Text.Trim();
BPOP.Name = txtName.Text.Trim();
BPOP.Partcode = txtPartNo.Text.Trim();
if (chkReprint.Checked)
{
BPOP.BtnReprintVisible = true;
BPOP.BtnSaveVisible = false;
}
divControls.Controls.Clear();
PlaceHolder1.Controls.Add(BPOP);
}
答案 1 :(得分:0)
变化:
void btnReprint_Click(object sender, EventArgs e)
{
}
到
protected void btnReprint_Click(object sender, EventArgs e)
{
}