简体   繁体   English

UserControl Click事件未触发

[英]UserControl Click event not firing

Hi this is my aspx page loading some values to the user control 嗨,这是我的aspx页面,将一些值加载到用户控件中

protected void Page_Load(object sender, EventArgs e)
        {

        }

this is the usercontrol where i am loading and sending the values in find click event 这是用户控件,我正在其中加载并发送查找点击事件中的值

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);


        }

this is my Usr_BPOP.ascx: 这是我的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
            {


            }




        }

this is the reprint button click event when i cilck this event it is not firing: 当我点击此事件未触发时,这是重印按钮单击事件:

void btnReprint_Click(object sender, EventArgs e)
        {
}

Since you are not setting the ID of the control, it is generated anew every time the control added to the page. 由于您没有设置控件的ID,因此每次将控件添加到页面时都会重新生成。 The generated ID might not be the same, and therefore the sender of the event cannot be recognized. 生成的ID可能不相同,因此无法识别事件的发送者。 So first thing you should do is assign an ID explicitly: 因此,您应该做的第一件事就是明确分配ID:

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

Secondly, assignment of the event handler should be done very time the control is created - that is, on every request, does not matter whether this is a postback or not - otherwise ASP.NET will not be able to determine what method should be called when the event is fired: 其次,事件处理程序的分配应该在控件创建时就完成-也就是说,在每个请求上,是否返回都无关紧要-否则ASP.NET将无法确定应调用哪种方法事件触发时:

protected void Page_Load(object sender, EventArgs e)
{
    // No check for postback here
    btnReprint.Click += new EventHandler(btnReprint_Click);

Update. 更新。 There is one more reason why this code does not behave as expected. 此代码无法按预期方式运行还有另一个原因。 The BPOP control is added to the page only on btnFind click. 仅在btnFind单击时,BPOP控件才添加到页面。 When the postback is caused by anything else, including btnReprint , on the response page generation BPOP control is not added to the page at all. 如果回发是由其他任何原因引起的,包括btnReprint ,则在响应页面生成中,根本不会将BPOP控件添加到该页面上。 If there is no control on the page - obviously its methods, including event handlers, cannot be triggered. 如果页面上没有控件-显然不能触发其方法(包括事件处理程序)。

Here is quick and dirty fix for this situation. 这是针对这种情况的快速而肮脏的修复程序。 It should be applied to the page code where BPOP control is added: 它应该应用于添加了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);
}

Change: 更改:

void btnReprint_Click(object sender, EventArgs e)
{
}

To

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM