c# asp.net .net pageload

c# - ASP.NET: buscar evento de clic en page_load



pageload (6)

En c #, ¿cómo puedo verificar si se hizo clic en un botón de enlace en el método de carga de página?

Necesito saber si se hizo clic antes de que se dispare el evento click.


Compruebe el valor del parámetro de solicitud __EVENTTARGET para ver si es la identificación del botón de enlace en cuestión.


El UniqueID del botón estará en Request.Form ["__ EVENTTARGET"]


if( IsPostBack ) { // get the target of the post-back, will be the name of the control // that issued the post-back string eTarget = Request.Params["__EVENTTARGET"].ToString(); }


si eso no funciona. Prueba UseSubmitBehavior="false"


Con base en la respuesta aceptada y la respuesta de RealSteel, esta podría ser una respuesta más completa.

Primero agregue al .aspx un botón como este:

<asp:Button id="btnExport" runat="server" Text="Export" UseSubmitBehavior="false"/>

Luego, en el método Page_Load:

if(IsPostBack){ var eventTarget = Request.Params["__EVENTTARGET"] // Then check for the id but keep in mind that the name could be // something like ctl00$ContainerName$btnExport // if the button was clicked or null so take precautions against null // ... so it could be something like this var buttonClicked = eventTarget.Substring(eventTarget.LastIndexOf("$") + 1).Equals("btnExport") }


Acabo de tener el mismo problema, tengo que hacer un juicio lógico en el método Page_Load para tratar un evento diferente (en qué botón se hizo clic).

Me di cuenta del brazo para obtener el como el siguiente ejemplo.

El código fuente aspx frontal (tengo muchos botones con ID F2, F3, F6, F12.

<Button Style="display: none" ID="F2" runat="server" Text="F2:Cancel" OnClientClick="SeiGyo(this)" OnClick="F2_Click" /> <Button Style="display: none" ID="F3" runat="server" Text="F3:Return" OnClientClick="SeiGyo(this)" OnClick="F3_Click" /> <Button Style="display: none" ID="F6" runat="server" Text="F6:Run" OnClientClick="SeiGyo(this)" OnClick="F6_Click" /> <Button Style="display: none" ID="F12" runat="server" Text="F12:Finish" OnClientClick="SeiGyo(this)" OnClick="F12_Click" />

El código fuente back-end aspx.cs, lo que tengo que hacer es juzgar qué botón se hizo clic cuando se activó Page_Load. Parece un poco estúpido, pero funciona. Espero que sea útil para otros

Dictionary<string, string> dic = new Dictionary<string, string>(); foreach(var id in new string[]{"F2","F3","F6","F12"}) { foreach (var key in Request.Params.AllKeys) { if (key != null && key.ToString().Contains(id)) dic.Add(id, Request[key.ToString()].ToString()); } }