简体   繁体   English

有没有办法知道在 PageLoad 事件中是否按下了控件?

[英]Is there a way to know if a control was pressed at the PageLoad event?

I created 2 buttons programmatically.我以编程方式创建了 2 个按钮。 Now i want to know which one was pressed at the pageLoad time....The button will fire a page load event and then its own event..现在我想知道在 pageLoad 时间按下了哪个按钮......该按钮将触发一个页面加载事件,然后触发它自己的事件..

Before it fires its own event, I want to know at the page load event if which button was pressed.在它触发自己的事件之前,我想知道页面加载事件是否按下了哪个按钮。

Is there a way to find out?有没有办法找出来?

My buttons are like this我的按钮是这样的

   Button btnMoveThread = new Button();
        btnMoveThread.Text = "1";
        btnMoveThread.Command += ModeratorButtonPassTo_Click;

   Button btnMoveThread = new Button();
        btnMoveThread.Text = "2";
        btnMoveThread.Command += ModeratorButtonPassTo_Click;

This function wont work:这个 function 不起作用:

 public static System.Web.UI.Control GetPostBackControl(System.Web.UI.Page page)
    {
        Control control = null;
        string ctrlname = page.Request.Params["__EVENTTARGET"];
        if (ctrlname != null && ctrlname != String.Empty)
        {
            control = page.FindControl(ctrlname);
        }
        // if __EVENTTARGET is null, the control is a button type and we need to 
        // iterate over the form collection to find it
        else
        {
            string ctrlStr = String.Empty;
            Control c = null;
            foreach (string ctl in page.Request.Form)
            {
                // handle ImageButton controls ...
                if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
                {
                    ctrlStr = ctl.Substring(0, ctl.Length - 2);
                    c = page.FindControl(ctrlStr);
                }
                else
                {
                    c = page.FindControl(ctl);
                }
                if (c is System.Web.UI.WebControls.Button ||
                            c is System.Web.UI.WebControls.ImageButton)
                {
                    control = c;
                    break;
                }
            }
        }
        return control;
    }

You can determine this by identifying which control caused the postback, prior to it's event handler being called.您可以通过在调用事件处理程序之前识别导致回发的控件来确定这一点。 eg例如

string sender = page.Request.Params.Get("__EVENTTARGET");
Response.Write(sender);        

ASP.NET postback works by submitting a client side form with two additional values, __EVENTTARGET and __EVENTARGUMENT . ASP.NET 回发通过提交带有两个__EVENTTARGET__EVENTARGUMENT的客户端表单来工作。 ASP.NET uses these to determine which control to find, and which event to fire. ASP.NET 使用这些来确定要查找的控件以及要触发的事件。 You could do:你可以这样做:

if (IsPostBack)
{
    string controlId = Request["__EVENTTARGET"];
    // Do something with the control Id.
}

You need to add the buttons before the post back so that you can get access to them during the post back, probably in the INIT of the page.您需要在回发之前添加按钮,以便您可以在回发期间访问它们,可能在页面的 INIT 中。 I changed it so it would compile and added an ID property to the buttons:我对其进行了更改,以便它可以编译并向按钮添加一个 ID 属性:

    Button btnMoveThread = new Button();
    btnMoveThread.Text = "1";
    btnMoveThread.ID = "Thread1";
    btnMoveThread.Command += ModeratorButtonPassTo_Click;
    DivContent.Controls.Add(btnMoveThread);

    Button btnMoveThread2 = new Button();
    btnMoveThread2.Text = "2";
    btnMoveThread2.ID = "Thread2";
    btnMoveThread2.Command += ModeratorButtonPassTo_Click;
    DivContent.Controls.Add(btnMoveThread2);

Then, in your function you need to iterate over the keys of the request.form:然后,在您的 function 中,您需要遍历 request.form 的键:

    int KeyCount = page.Request.Form.AllKeys.Length;
    for (int i = 0; i < KeyCount; i++)
    {
        if (page.Request.Form.AllKeys[i] == "Thread1")
        {
            return page.FindControl("Thread1");
        }
        if (page.Request.Form.AllKeys[i] == "Thread2")
        {
            return page.FindControl("Thread2");
        }
    }

Since you are adding the buttons in code and not in the designer the find control will be null unless you add them back before the postback, like I suggested in the INIT of the page.由于您在代码中而不是在设计器中添加按钮,因此查找控件将为 null 除非您在回发之前将它们添加回来,就像我在页面的 INIT 中建议的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM