Client-Side Script After UpdatePanel Refresh
How to run client-side javascript after running an UpdatePanel refresh
In ASP.Net, the <asp:UpdatePanel>
allows us a quick an easy way to refresh screen content with server-side changes without the need for a full page post-back.
It is very useful to be able to run javascript on the client once the refresh has happened, and this can be achieved using the following code...
If ScriptManager.GetCurrent(Page).IsInAsyncPostBack Then
Dim script as string = "callFunction();"
ScriptManager.RegisterStartupScript(Me.Page,
Me.Page.GetType(), "script", script, True)
End If
Added 29/06/2018 12:40
UpdatePanel Button Click via Javascript
How to trigger a button click causing an UpdatePanel refresh
In ASP.Net, the <asp:UpdatePanel>
allows us a quick an easy way to refresh screen content with server-side changes without the need for a full page post-back.
Sometimes it's necessary to trigger that refresh by using javascript, and in order to do that, we can use the following...
__doPostBack(document.getElementById("btnRefresh").name, "");
Or with jQuery...
__doPostBack($("#btnRefresh").prop("name"), "");
This is assuming that the <asp:Button runat="server" id="btnRefresh" />
is contained within the UpdatePanel, and that the button has not been set as a <Triggers><asp:PostBackTrigger/></Triggers>
.
ASP.NET Fire Validation
Manually firing a single validator using client-side script
In ASP.Net the use of validators is common place, including the <asp:CustomValidator>
.
If a validator is linked to a control, then that validator is fired when the control is changed... but what about when multiple controls are used on a single custom validator? How do you make sure the validator shows the correct value when you change any of those controls?
One answer is to make all those controls call an extra function... which itself then uses the inbuilt ValidatorValidate()
provided by ASP.Net.
Markup...
<asp:TextBox runat="server" id="txtFirst" onchange="textboxChanged();" />
<asp:TextBox runat="server" id="txtSecond" onchange="textboxChanged();" />
<asp:CustomValidator runat="server" ID="cusVal" ErrorMessage="Oops"
ClientValidationFunction="doCustomVal" />
Javascript...
function textboxChanged() {
ValidatorValidate(document.getElementById("<%=cusVal.ClientID%>"));
}
function doCustomVal(src, args) {
args.IsValid = (
document.getElementById("<%=txtFirst.ClientID%>").value != "" &&
document.getElementById("<%=txtSecond.ClientID%>").value != "");
}
Added 04/06/2018 15:58