How can I programmatically call my .net button's onclick in page load?

后端 未结 5 1538
囚心锁ツ
囚心锁ツ 2021-01-16 23:06

I have a onclick event that is like:

public void OnMyButton_Click(object sender, EventArgs e)

How can I call this from within pageload?

5条回答
  •  礼貌的吻别
    2021-01-16 23:24

    this function is what you need:

    public static void DoClick(this System.Web.UI.WebControls.Button btn)
    {
        System.Reflection.MethodInfo m = typeof(System.Web.UI.WebControls.Button)
            .GetMethod("OnClick", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
        m.Invoke(btn, new object[] { EventArgs.Empty });
    }
    

提交回复
热议问题