asp.net c#事件处理程序第二次不工作

时间:2014-06-03 05:00:25

标签: c# asp.net eventhandler

我有一个asp.net网页,其中包含将在运行时填充的面板

protected void Page_Load(object sender, EventArgs e)
{

    buildStructure(1);
}

这是方法

public void buildStructure(int level_id)
{

    pMain.Controls.Clear();
    //Response.Write(@"<script language='javascript'>alert('" + level_id + "');</script>");

    DataUtility DU = new DataUtility(@"****");
    DataTable dt = DU.GetDataTable("SELECT * FROM dbo.PRStructure_Main WHERE level_id = "+level_id);
    int curr_level = 1;
    int curr_child = 1;
    int totalchild = 0;
    if (dt.Rows.Count > 0)
    {
        Panel pLevel = new Panel();
        pLevel.CssClass = "level";

        Panel pItem = new Panel();
        pItem.CssClass = "item-ceo";

        Label lItem = new Label();
        lItem.Text = dt.Rows[0].ItemArray[2].ToString();

        pItem.Controls.Add(lItem);
        pLevel.Controls.Add(pItem);
        pMain.Controls.Add(pLevel);


        Panel pLevelLine = new Panel();
        pLevelLine.CssClass = "level";

        Panel pItemLine = new Panel();
        pItemLine.CssClass = "item-line-ceo";

        Panel pLine = new Panel();
        pLine.CssClass = "horizontal-line";

        pItemLine.Controls.Add(pLine);
        pLevelLine.Controls.Add(pItemLine);
        pMain.Controls.Add(pLevelLine);

        Panel pLevelLine2 = new Panel();
        pLevelLine2.CssClass = "level";

        Panel pLevel2 = new Panel();
        pLevel2.CssClass = "level";

        dt = DU.GetDataTable("SELECT * FROM dbo.PRStructure_Main WHERE level_parent = "+(Convert.ToInt32( dt.Rows[0].ItemArray[0].ToString())));
        lbItem2 = new LinkButton[dt.Rows.Count];
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            Panel pItemLine2 = new Panel();
            Panel pLine2 = new Panel();
            if (i == 0)
            {
                pItemLine2.CssClass = "item-line-level2-first";
                pLine2.CssClass = "horizontal-line2-first";
            }
            else if (i == dt.Rows.Count - 1)
            {
                pItemLine2.CssClass = "item-line-level2-last";
                pLine2.CssClass = "horizontal-line2-last";
            }
            else
            {
                pItemLine2.CssClass = "item-line-level2-middle";
                pLine2.CssClass = "horizontal-line2-middle";
            }
            pItemLine2.Controls.Add(pLine2);
            pLevelLine2.Controls.Add(pItemLine2);




            Panel pItem2 = new Panel();
            pItem2.CssClass = "item-level2";

            Panel pItemContent2 = new Panel();
            pItemContent2.CssClass = "item-level2-content";

            lbItem2[i] = new LinkButton();
            lbItem2[i].Text = dt.Rows[i].ItemArray[2].ToString();

            int current_level1 = (int)dt.Rows[i].ItemArray[0];
            //lbItem2.OnClientClick = "alert('" + current_level1 + "')";
            //lbItem2.Click += new EventHandler((s,e) => evHandler(s,e, current_level1));

            lbItem2[i].Click += new System.EventHandler(delegate(Object o, EventArgs a)
            {
                evHandler(o, a, current_level1);
            });
            pItemContent2.Controls.Add(lbItem2[i]);
            //pLevel.Controls.Add(lbItem);

            DataTable dt2 = DU.GetDataTable("SELECT * FROM dbo.PRStructure_Main WHERE level_parent = " + dt.Rows[i].ItemArray[0]);
            Panel pMenuLevel = new Panel();
            pMenuLevel.CssClass = "menu-level2";

            //<div class="menu-level2-items"><a href="#">Assets Integrity Management</a></div>


            for (int j = 0; j < dt2.Rows.Count; j++)
            {


                Panel pMenuLevelItems = new Panel();
                pMenuLevelItems.CssClass = "menu-level2-items";

                LinkButton lbMenuItem = new LinkButton();
                lbMenuItem.Text = dt2.Rows[j].ItemArray[2].ToString();
                int current_level2 = (int)dt2.Rows[j].ItemArray[0];

                //lbMenuItem.Click += new EventHandler(delegate (Object o, EventArgs ee) { evHandler(s, ee,current_level2)});

                lbMenuItem.Click += new EventHandler(delegate (Object o, EventArgs a) 
                    {
                        evHandler(o, a, current_level2);
                    });


                pMenuLevelItems.Controls.Add(lbMenuItem);
                DataTable dt3 = DU.GetDataTable("SELECT * FROM dbo.PRStructure_Main WHERE level_parent = " + dt2.Rows[j].ItemArray[0]);


                Panel pSubMenuLevel = new Panel();
                pSubMenuLevel.CssClass = "sub-menu-level2";

                //  <div class="sub-menu-level2-items"><a href="#"> Business Application Section </a></div>
                for (int k = 0; k < dt3.Rows.Count; k++)
                {
                    Panel pSubMenuLevelItems = new Panel();
                    pSubMenuLevelItems.CssClass = "menu-level2-items";

                    LinkButton lbSubMenuItem = new LinkButton();
                    lbSubMenuItem.Text = dt3.Rows[k].ItemArray[2].ToString();
                    int current_level3 = (int)dt3.Rows[k].ItemArray[0];

                    lbMenuItem.Click += new EventHandler((s, e) => evHandler(s, e, current_level3));
                    pSubMenuLevelItems.Controls.Add(lbSubMenuItem);

                    pSubMenuLevel.Controls.Add(pSubMenuLevelItems);
                }

                    pMenuLevelItems.Controls.Add(pSubMenuLevel);

                pMenuLevel.Controls.Add(pMenuLevelItems);

            }

            pItemContent2.Controls.Add(pMenuLevel);

            pItem2.Controls.Add(pItemContent2);
            pLevel2.Controls.Add(pItem2);

        }

        pMain.Controls.Add(pLevelLine2);
        pMain.Controls.Add(pLevel2);
    }

}

我在本节中遇到了问题

                        lbMenuItem.Click += new EventHandler((s, e) => evHandler(s, e, current_level3));

这是处理程序方法

public void evHandler(Object s,EventArgs e, int someData){

    //Response.Write(@"<script language='javascript'>alert('" + someData + "');</script>");
    ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),"err_msg","alert('" + someData + "');",
     true);

    buildStructure(someData);
}

第一次正常工作

但是当我再次点击它时会加载页面。

3 个答案:

答案 0 :(得分:0)

我认为您的page_load应该如下所示

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack){
        buildStructure(1);
    }

}

答案 1 :(得分:0)

发布回来时,您需要重新创建完全相同的控件。如果页面仅加载了一个控件,那么它如何知道为不存在的控件引发click事件。

编辑: 根据我对asp.net页面生命周期的理解,您必须在恢复回发数据之前创建控件,这是页面控制事件触发的方式。因此,如果您有一个控件说LinkBut​​tonA是在页面上动态创建的并且您单击它,那么在回发期间要在服务器上触发click事件,必须在恢复回发数据之前重新创建它,因此请尝试重新创建动态Page_Init中的控件而不是Page_Load,并尝试保持对方法的输入&#34; buildStructure&#34;相同。假设您使用buildStructure从事件处理程序调用它(&#34; fifth_level&#34;)确保Page_Init执行相同的buildStructure(&#34; fifth_level&#34;)。

答案 2 :(得分:0)

Microsoft建议在preint上创建动态控件,因此您需要在preinit上创建相同的控件,如下所示

http://msdn.microsoft.com/en-us/library/ms178472.aspx

protected override void OnPreInit(EventArgs e)
{


}