Good afternoon, I need to update components that are within UpdatePanel
page aspx
after insertion via Ajax
. The Ajax
executes a function in code-behind
in C#
, and reloading of the entire page happens.
Ajax executing the insert:
<script type="text/javascript">
function FunctionComment(post_id, texto) {
if (event.keyCode == 13) {
var adv_id = $('#<%= hdfAdvogadoID.ClientID %>').val();
try {
$.ajax({
type: "POST",
url: 'Default.aspx/Comentar',
data: "{'ComentarioTexto':'" + texto
+ "','AdvogadoID':'" + adv_id + "','PostID':'" + post_id + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: ShowMessage(" Comentadado!", 'Success', "icon fa fa-check"),
error: function (msg) {
alert(msg.d);
}
});
} catch (err) { }
}
}
</script>
Method responsible for entering the data in the database:
[System.Web.Services.WebMethod]
public static string Comentar(string ComentarioTexto, int AdvogadoID, int PostID)
{
if (HttpContext.Current != null)
{
ComentarioCRUD.SalvaComentario(ComentarioTexto, AdvogadoID, PostID);
NotificationCRUD.AlterarFullNotificationComent();
}
return string.Empty;
}
The code works perfectly but after running the page it is updated whole not just the UpdatePanel.
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:Repeater runat="server" ID="Repeater1"
OnItemDataBound="RptTime_ItemDataBound">
<ItemTemplate>
<codigos>....</codigos>
<codigos>....</codigos>
<asp:Repeater ID="RptComentario"
OnItemDataBound="RptComentario_ItemDataBound" runat="server">
<asp:TextBox CssClass="form-control input-sm"
placeholder="Pressione enter para postar comentário"
ID="txtPost" runat="server" AutoPostBack="false" autocomplete="off"
onkeypress='<%# String.Format("FunctionComment(\"{0}\", this.value);", Eval("PostID")) %>'>
</asp:TextBox>
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
You would have to use a trigger to only update UpdatePanel
after the command success:
of Ajax
!?