ASP.NET 2.0: llamar a una función de JavaScript desde onChange con AutoPostBack = true
-
10-07-2019 - |
Pregunta
Tengo un ASP.NET DDL que se ve así cuando veo la fuente:
<select name="testControl" onchange="DoCustomStuff();setTimeout('__doPostBack(\'testControl\',\'\')', 0)" id="testControl">
Se ve así en la página .cs:
<asp:DropDownList ID="testControl" runat="server" onchange="DoCustomStuff()" OnSelectedIndexChanged="testControl_Changed" AutoPostBack="true" />
¿Alguien puede ver un problema con el uso de onchange y AutoPostBack = " true " en un DDL como este? Pregunto porque tenemos algunos usuarios para quienes DoCustomStuff () no parece ser llamado correctamente, y me pregunto si sería posible ejecutar __doPostBack () antes de que DoCustomStuff () complete su trabajo.
Solución
Intente adjuntar manualmente la referencia de devolución de datos de esa manera:
Page.ClientScript.RegisterClientScriptBlock(
typeof(_Default),
"PageScripts",
string.Format("function DoCustomStuff() { /* Your Code Here */ {0} }", Page.ClientScript.GetPostBackEventReference(testControl, string.Empty))
);
testControl.Attributes["onchange"] = "DoCustomStuff();";
esto le proporciona la referencia del cliente de devolución de datos:
Page.ClientScript.GetPostBackEventReference(testControl, string.Empty))
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow