点击事件未触发

本文关键字:事件 | 更新日期: 2023-09-27 18:09:08

嗨,这是我的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
            {

            }


        }

这是重印按钮点击事件,当我点击这个事件时,它不会触发:

void btnReprint_Click(object sender, EventArgs e)
        {
}

点击事件未触发

由于没有设置控件的ID,因此每次将控件添加到页面中时都会重新生成该ID。生成的ID可能不相同,因此无法识别事件的发送者。首先要做的是显式地赋值一个ID:

Usr_BPOP BPOP = (Usr_BPOP)Page.LoadControl("~/Usr_BPOP.ascx");
BPOP.ID = "SomeID";

其次,事件处理程序的分配应该在每次创建控件的时候完成-也就是说,在每个请求中,无论这是否是回发-否则ASP. js . js . js。. 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);
}

变化:

void btnReprint_Click(object sender, EventArgs e)
{
}

protected void btnReprint_Click(object sender, EventArgs e)
{
}