【问题标题】:UserControl Click event not firing用户控件单击事件未触发
【发布时间】:2013-04-15 23:55:37
【问题描述】:

您好,这是我的 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)
        {
}

【问题讨论】:

  • btnReprint.Click += new EventHandler(btnReprint_Click); 我认为您需要将其放在 PageInit 上,而不是 PageLoad 上
  • 事件必须在你想要触发它的页面上

标签: c# asp.net user-controls


【解决方案1】:

由于您没有设置控件的 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);
}

【讨论】:

  • @andreii 已经尝试过这段代码,它不起作用,当我点击时它第二次起作用了
  • @WebDeveloper,又发现一个可能的问题,请查找帖子的更新。
  • Page_Load 为时已晚,无法重新添加控件 - 您需要在 OnInit 期间重新添加或覆盖 CreateChildControls,因为这是设计的目的。
  • @RichardFriend,控件几乎可以在页面生命周期的任何地方添加,甚至在PreRender 上。 Controls.Add(control1) 执行的那一刻,control1 被添加到页面的控制树中,并连续调用所有必要的生命周期事件。
  • @Andrei 正确,但如果你想让他们正确地保持视图状态,我会使用 OnInit 来避免捏造:msdn.microsoft.com/en-us/library/ms972976.aspx#viewstate_topic4
【解决方案2】:

变化:

void btnReprint_Click(object sender, EventArgs e)
{
}

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

【讨论】:

  • 访问修饰符在这里并不重要,因为处理程序的分配是以编程方式完成的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多