Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# UserControl单击事件未触发_C#_Asp.net_User Controls - Fatal编程技术网

C# UserControl单击事件未触发

C# UserControl单击事件未触发,c#,asp.net,user-controls,C#,Asp.net,User Controls,嗨,这是我的aspx页面,正在向用户控件加载一些值 protected void Page_Load(object sender, EventArgs e) { } 这是在find click事件中加载和发送值的usercontrol protected void BtnFind_Click(object sender, EventArgs e) { Usr_BPOP BPOP = (Usr_BPOP)Page.Load

嗨,这是我的aspx页面,正在向用户控件加载一些值

protected void Page_Load(object sender, EventArgs e)
        {

        }
这是在find click事件中加载和发送值的usercontrol

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.NET将无法确定在触发事件时应调用什么方法:

protected void Page_Load(object sender, EventArgs e)
{
    // No check for postback here
    btnReprint.Click += new EventHandler(btnReprint_Click);
更新。还有一个原因导致此代码的行为不符合预期。BPOP控件仅在
btnFind
单击时添加到页面。如果回发是由任何其他原因引起的,包括
btnreport
,则响应页面上的生成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)
{
}


btnReprint.Click+=neweventhandler(btnReprint\u Click)我认为你需要把它放在PageInit上,而不是PageLoadevent必须放在你想触发的页面上it@andreii我试过这段代码,但它不起作用,它在我第二次工作时起作用clicking@WebDeveloper,又发现了一个可能的问题,请查找帖子的更新。
Page\u Load
太晚了,无法重新添加控件-您需要在
OnInit
期间重新添加控件,或者重写
CreateChildControls
,因为这是专为它而设计的。@RichardFriend,控件几乎可以在页面生命周期的任何地方添加,甚至在
PreRender
上。时刻
控制。添加(控制1)
被执行
control1
被添加到页面的控件树中,所有必要的生命周期事件都被连续调用。@Andrei更正,但如果您希望它们正确地持久化viewstate,我将使用OnInit来避免不必要的伪造:Access修饰符在这里并不重要,因为处理程序的分配已经完成以编程方式。
protected void btnReprint_Click(object sender, EventArgs e)
{
}