C# – UserControl Click event not firing

asp.netcuser-controls

Hi this is my aspx page loading some values to the user control

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:

 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)
        {
}

Best Answer

Since you are not setting the ID of the control, it is generated anew every time the control added to the page. The generated ID might not be the same, and therefore the sender of the event cannot be recognized. So first thing you should do is assign an ID explicitly:

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:

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. 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. 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:

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);
}
Related Topic