0
Good afternoon, I need to update components that are inside a UpdatePanel
page aspx
after insertion via Ajax
. The Ajax
performs a function on code-behind
in C#
, and happens to reload the entire page.
Ajax running 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 into 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 is updated whole not only 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’d have to use a trigger to update only the UpdatePanel
after the command success:
of Ajax
!?
You have a
<asp:ScriptManager>
in your form?– Leandro Angelo
@Leandroangelo, yes he inherits from the master page, without
<asp:ScriptManager>
would give compilation error– Evandro
Why don’t you do everything for
UpdatePanel
?– novic
@Virgilionovic all by
UpdatePanel
how so? you could exemplify for me!?– Evandro
Updatepanel, already has everything you need to update the screen, only the code snippet, would not need to do an ajax and then have it update ...
– novic
@Virgilionovic I’ll try using the event
txtPost.TextChanged+=new EventHandler(txtPost_TextChanged);
because he’s inside aRepeater
– Evandro
I’m mounting an example @Evandro, keep trying there.!
– novic