vasco16 Posted August 12, 2013 at 10:07 AM Report #521489 Posted August 12, 2013 at 10:07 AM Bom dia, gostaria de saber como posso controlar um evento onserverclick. Eu quero que um botão faça uma pesquisa, mas que não faça refresh à página.. O objectivo é apresentar os dados numa tabela. E o que acontece é que ao usar onserverclick é sempre feito um refresh..
nelsonr Posted August 12, 2013 at 10:12 AM Report #521490 Posted August 12, 2013 at 10:12 AM Boas, se tiveres tudo a funcionar e apenas queres ver-te livre do refresh visivel, podes colocar o que pretendes atualizar dentro de um UpdatePanel http://msdn.microsoft.com/en-us/library/bb399001%28v=vs.100%29.aspx Ate podes depois adicionar um UpdateProgress para mostrar conteúdo enquanto a aplicação faz refresh (do tipo: Atualizando...) http://msdn.microsoft.com/en-us/library/bb398821%28v=vs.100%29.aspx
vasco16 Posted August 12, 2013 at 10:14 AM Author Report #521491 Posted August 12, 2013 at 10:14 AM (edited) Boas, se tiveres tudo a funcionar e apenas queres ver-te livre do refresh visivel, podes colocar o que pretendes atualizar dentro de um UpdatePanel http://msdn.microsoft.com/en-us/library/bb399001%28v=vs.100%29.aspx Ate podes depois adicionar um UpdateProgress para mostrar conteúdo enquanto a aplicação faz refresh (do tipo: Atualizando...) http://msdn.microsoft.com/en-us/library/bb398821%28v=vs.100%29.aspx Eu estou a trabalhar com collapse , quando carrego no botão o refresh fecha-me os menus abertos.. Com UpdatePanel irei conseguir controlar isso? O objectivo é mesmo executar o código do evento sem ser feito o refresh.. Obrigado pela ajuda 🙂 Edited August 12, 2013 at 10:16 AM by vasco16
nelsonr Posted August 12, 2013 at 10:52 AM Report #521492 Posted August 12, 2013 at 10:52 AM Se o botão estiver dentro do updatepanel, o refresh será feito apenas no conteúdo do update panel, não afectando o resto da página. Se tiveres o menu fora do update panel, não deverá ser afectado.
vasco16 Posted August 12, 2013 at 11:16 AM Author Report #521496 Posted August 12, 2013 at 11:16 AM (edited) Fiz também um pequeno teste fora do projecto: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="X.Y.WebForm1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> </head> <body> <form id="form2" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <fieldset> <legend>UpdatePanel</legend> <asp:Label ID="Label1" runat="server" Text="Panel created."></asp:Label><br /> <asp:DropDownList ID="DropDownList2" runat="server" AppendDataBoundItems="true" DataTextField="Company_Name" DataValueField="id"> <asp:ListItem Text="CA" Value="0" Selected="True" /> <asp:ListItem Text="FB" Value="1" /> </asp:DropDownList> </fieldset> <asp:Button ID="Button2" runat="server" onclick="Button1_Click" Text="ButtonUP" /> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="Button1" /> </Triggers> </asp:UpdatePanel> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> </div> <asp:DropDownList ID="DropDownListSubContractors" runat="server" AppendDataBoundItems="true" DataTextField="Company_Name" DataValueField="id"> <asp:ListItem Text="AA" Value="0" Selected="True" /> <asp:ListItem Text="BB" Value="1" /> </asp:DropDownList> </form> </body> </html> Mas não consigo reproduzir o efeito aqui. Ele refresca sempre o conteudo dentro do UpdatePanel. Edited August 12, 2013 at 03:45 PM by vasco16
nelsonr Posted August 12, 2013 at 11:30 AM Report #521501 Posted August 12, 2013 at 11:30 AM Fiz um teste e funciona bem. Coloquei uma input text, em que ao entrares na pagina, preenches lá texto. Ao clicar no botão pesquisar, caso não tenhas o update panel, o conteudo da input text é limpa (porque a página faz refresh) Depois de colocar o update panel, já não limpa. Exemplo sem o update panel: <form id="form1" runat="server"> <input id="Text1" type="text" /> <!-- Botão Pesquisar --> <a class="brand" href="#"> <button id="Button1" runat="server" class="btn btn-large btn-white" onserverclick="Button1_Click" data-toggle="modal" type="button"> <i class="icon-search icon-blue"></i> Pesquisar </button> </a> <asp:Label ID="label1" runat="server"></asp:Label> </form> Exemplo com o update panel: <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> <input id="Text1" type="text" /> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <!-- Botão Pesquisar --> <a class="brand" href="#"> <button id="Button1" runat="server" class="btn btn-large btn-white" onserverclick="Button1_Click" data-toggle="modal" type="button"> <i class="icon-search icon-blue"></i> Pesquisar </button> </a> <asp:Label ID="label1" runat="server"></asp:Label> </ContentTemplate> </asp:UpdatePanel> </form> Do lado do servidor, o evento do click tem isto (C#) protected void Button1_Click(object sender, EventArgs e) { label1.Text = "server called"; }
vasco16 Posted August 12, 2013 at 01:05 PM Author Report #521510 Posted August 12, 2013 at 01:05 PM (edited) Aqui em ambos os casos a input box é limpa... Estou a testar com google chrome. Edited August 12, 2013 at 01:07 PM by vasco16
vasco16 Posted August 12, 2013 at 01:23 PM Author Report #521512 Posted August 12, 2013 at 01:23 PM (edited) <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="X.Y.WebForm1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title></title> </head> <body> <form id="form1" runat="server"> <div> <h2 style="color: Green">UpdatePanel in ASP.NET 4, C#</h2> <input id="Text1" type="text" /> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:Button runat="server" ID="update_Button" Text="Generate Random Number" Width="187px" onclick="update_Button_Click" /> <br /> <br /> <asp:Label runat="server" ID="Label1" Font-Bold="True" Font-Names="Cambria" Font-Size="X-Large" ForeColor="#003300" /> <input id="Text3" type="text" /> </ContentTemplate> </asp:UpdatePanel> <input id="Text4" type="text" /> </div> <input id="Text2" type="text" /> </form> <input id="Text5" type="text" /> </body> </html> Todas as inputbox são limpas após clicar no botão. Edited August 12, 2013 at 01:41 PM by vasco16
nelsonr Posted August 12, 2013 at 01:39 PM Report #521514 Posted August 12, 2013 at 01:39 PM (edited) Testei o teu exemplo e apenas o conteudo da text3 é limpo (Firefox e Chrome). Do lado do C#, tens alguma coisa sem ser o evento update_Button_Click? Edited August 12, 2013 at 01:40 PM by nelsonr
vasco16 Posted August 12, 2013 at 01:45 PM Author Report #521515 Posted August 12, 2013 at 01:45 PM (edited) Testei o teu exemplo e apenas o conteudo da text3 é limpo (Firefox e Chrome). Do lado do C#, tens alguma coisa sem ser o evento update_Button_Click? O page_load sem nada. Neste momento até tirei tudo. Só tenho: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace X.Y { public partial class WebForm1 : System.Web.UI.Page { protected void update_Button_Click(object sender, EventArgs e) { Label1.Text = DateTime.Now.ToLongTimeString(); } } } Edited August 12, 2013 at 01:46 PM by vasco16
vasco16 Posted August 12, 2013 at 02:57 PM Author Report #521520 Posted August 12, 2013 at 02:57 PM Testei o teu exemplo e apenas o conteudo da text3 é limpo (Firefox e Chrome). Do lado do C#, tens alguma coisa sem ser o evento update_Button_Click? Como testas? Fazes mesmo F5 ou fazes enter na barra de endereços? É que ambos os casos no google chrome perco a informação das inputbox. No caso do firefox nenhuma é limpa. A versão do Chrome que tenho é a Version 29.0.1547.22 Acho que até estou a fazer as coisas certas, mas está a faltar-me algo..
nelsonr Posted August 12, 2013 at 03:22 PM Report #521521 Posted August 12, 2013 at 03:22 PM Testei com F5, que abre automaticamente o Firefox. Depois copiei o endereço para o Chrome A nivel de código não me parece que falte nada, mas também não estou a ver o porquê de não estar a funcionar ai. A unica diferença que vejo será o namespace, que tenho o default (WebApplication1) Testei também tanto no VS2010 como no VS2012, com as frameworks 4.0 e 4.5.
vasco16 Posted August 12, 2013 at 03:43 PM Author Report #521522 Posted August 12, 2013 at 03:43 PM (edited) Testei com F5, que abre automaticamente o Firefox. Depois copiei o endereço para o Chrome A nivel de código não me parece que falte nada, mas também não estou a ver o porquê de não estar a funcionar ai. A unica diferença que vejo será o namespace, que tenho o default (WebApplication1) Testei também tanto no VS2010 como no VS2012, com as frameworks 4.0 e 4.5. Eu não consigo sequer reproduzir um exemplo que funcione. Mas no projecto estou a usar MasterPage, vou ter problemas depois? Estou a usar VS2012 com framework 4.5. Aqui vão os exemplos que testei: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="X.Y.WebForm2" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <input id="Text1" type="text" /> <!-- Botão Pesquisar --> <a class="brand" href="#"> <button id="Button1" runat="server" class="btn btn-large btn-white" onserverclick="Button1_Click" data-toggle="modal" type="button"> <i class="icon-search icon-blue"></i> Pesquisar </button> </a> <asp:Label ID="label1" runat="server"></asp:Label> </div> </form> </body> </html> using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace X.Y { public partial class WebForm2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { label1.Text = "server called"; } } } <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="X.Y.WebForm3" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> <input id="Text1" type="text" /> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <!-- Botão Pesquisar --> <a class="brand" href="#"> <button id="Button1" runat="server" class="btn btn-large btn-white" onserverclick="Button1_Click" data-toggle="modal" type="button"> <i class="icon-search icon-blue"></i> Pesquisar </button> </a> <asp:Label ID="label1" runat="server"></asp:Label> </ContentTemplate> </asp:UpdatePanel> </div> </form> </body> </html> using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace X.Y { public partial class WebForm3 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { label1.Text = "server called"; } } } Está tudo correcto, certo? Ambos apresentam o mesmo comportamento... Introduzo dados na intput , carrego no botão e faz refresh. Ao fazer refresh perco o que tinha na inputbox. Independentemente de usar UpdatePanel ou não o que está a fazer refresh são as chamadas aos métodos do servidor. Assim faz refresh: <asp:UpdatePanel ID="UpdatePanel1" runat="server" > <ContentTemplate> <!-- Botão Pesquisar --> <a class="brand" href="#"> <button id="Button1" class="btn btn-large btn-white" onserverclick="Button1_Click" runat="server" data-toggle="modal" type="button"> <i class="icon-search icon-blue"></i> Pesquisar </button> </a> <!-- Fim Botão Pesquisa --> </ContentTemplate> </asp:UpdatePanel> Assim não faz: <asp:UpdatePanel ID="UpdatePanel1" runat="server" > <ContentTemplate> <!-- Botão Pesquisar --> <a class="brand" href="#"> <button id="Button1" class="btn btn-large btn-white" onclick="Button1_Click" runat="server" data-toggle="modal" type="button"> <i class="icon-search icon-blue"></i> Pesquisar </button> </a> <!-- Fim Botão Pesquisa --> </ContentTemplate> </asp:UpdatePanel> O problema é que metendo o código no aspx: <script runat="server"> protected void Button1_Click(object Source, EventArgs e) { System.Diagnostics.Debug.WriteLine("CLICK CLICK CLICK CLICK \n"); } </script> Não executa nada... Edited August 12, 2013 at 03:53 PM by vasco16
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now