【问题标题】:Page_PreLoad() event is triggered before link button click event when I click on a link button当我单击链接按钮时,在链接按钮单击事件之前触发 Page_PreLoad() 事件
【发布时间】:2020-07-14 19:20:44
【问题描述】:

我在驱动器 E 中列出文件夹和文件,我使用隐藏字段来保留路径,我创建了一些代表文件夹的链接按钮,它们是在 Page_PreLoad() 事件中以编程方式创建的:

    protected void Page_PreLoad(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(hdfPath.Value))
        {
            path = "E:\\" + hdfPath.Value;
            directories = Directory.GetDirectories(path);
            files = Directory.GetFiles(path);
        }
        else
        {
            directories = Directory.GetDirectories("E:\\");
            files = Directory.GetFiles("E:\\");
        }
        for (int i = 0; i < directories.Length; i++)
        {
           LinkButton lkbLink = new LinkButton();
           lkbLink.Click += new EventHandler(btn_Click);
           void btn_Click(object sender, EventArgs e)
           {
              if (string.IsNullOrEmpty(hdfPath.Value))
                hdfPath.Value = folderName;
              else
                hdfPath.Value += "\\" + folderName;
           }
        }
    }

当我单击任何链接按钮时,首先触发 Page_PreLoad() 事件,然后触发 btn_Click() 事件,而我希望首先触发 btn_Click()。那么隐藏字段的相关值就属于前一步的值。

我选择的 ASP.Net 生命周期有问题吗?

如何在用户单击链接按钮时立即设置隐藏字段值,以便获取隐藏字段上的(正确)当前值?

【问题讨论】:

    标签: asp.net webforms


    【解决方案1】:

    生命周期没有问题,你也不能选择生命周期:)
    页面的 PreLoad-Event 总是在 control-events 之前。
    This 将为您详细介绍 asp.net 中的生命周期。

    你的情况有点复杂:

    1. 您需要动态添加一些按钮并将事件处理程序附加到它们 - 这必须在每次回发时完成,否则按钮处理程序将不会被触发。
    2. 然后在您的处理程序中,您需要在添加按钮之前再次删除这些按钮,并根据新路径添加新按钮。

    要实现这一点,请从您的代码中创建一个方法,例如:

        private void InitButtons()
        {
            if (!string.IsNullOrEmpty(hdfPath.Value))
            {
                path = "E:\\" + hdfPath.Value;
                directories = Directory.GetDirectories(path);
                files = Directory.GetFiles(path);
            }
            else
            {
                directories = Directory.GetDirectories("E:\\");
                files = Directory.GetFiles("E:\\");
            }
            for (int i = 0; i < directories.Length; i++)
            {
               LinkButton lkbLink = new LinkButton();
               lkbLink.Click += new EventHandler(btn_Click);
               void btn_Click(object sender, EventArgs e)
               {
                  if (string.IsNullOrEmpty(hdfPath.Value))
                    hdfPath.Value = folderName;
                  else
                    hdfPath.Value += "\\" + folderName;
                  // remove the old buttons here, if you need to
                  // RemoveOldButtons();
                  // call the Init-Buttons-Function again
                  InitButtons();
               }
            }
        }
    

    从您的按钮事件处理程序中调用此方法,如上所示,然后在您的加载事件处理程序中再次调用:

        protected void Page_PreLoad(object sender, EventArgs e)
        {
            InitButtons();
        }
    

    【讨论】:

    • 感谢您的回答,但解决方案不够明确。
    • 谢谢,但是每隔一段时间它仍然有效,所以我改用了ajax。
    猜你喜欢
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    相关资源
    最近更新 更多