Jquery Validation And Update Panel Together not working - asp.net

Here by adding this code my problem of getting page posted back in spite of having validation error is solved
([blog]: Jquery Validation And Update Panel Together)
but it introduced a new one, now by selecting the master dropdown list my child dropdown is not populating I have checked from code behind the targeted method to populate the child dropdown is not firing at all….
<asp:UpdatePanel ID="updRole" runat="server" UpdateMode="Conditional"> <ContentTemplate>
<script type="text/javascript" >
Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest (instance_initializeRequest);
function instance_initializeRequest(sender, args) {
if (!Validator())
{args.set_cancel(true);
}
}
</script>
</ContentTemplate>
</asp:UpdatePanel>
This piece of code I have in master page where the validator() function is fired in .ready() event fired by default but the problem was by adding update panel in any content page is method is firing(Validation error is also showing for 2 sec) but page is getting posted back .
<script type="text/javascript">
// only for demo purposes
$.validator.setDefaults({
invalidHandler: function (form, validator) {
}
});
$().ready(function () {
Validator();
});
function Validator() {
var container = $('div.container');
// validate the form when it is submitted
var validator = $("#form1").validate({
errorContainer: container,
errorLabelContainer: $("ul", container),
wrapper: 'li',
meta: "validate"
});
}
</script>

Script in the head:
<script type="text/javascript">
// only for demo purposes
$().ready(function () {
$.validator.setDefaults({
invalidHandler: function (form, validator) { }
});
var container = $("div.container");
$("#form1").validate({
errorContainer: container,
errorLabelContainer: $("ul", container),
wrapper: 'li',
meta: "validate"
});
});
</script>
Script just below the ScriptManager control:
Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(instance_initializeRequest);
function instance_initializeRequest(sender, args) {
if (!$("#form1").validate().form()) {
args.set_cancel(true);
}
}

Related

how to add section Scripts to asp.net mvc Razor View without layout page

My mvc view layout is null. how can i add script inside view.
#{
Layout = null;
}
#section Scripts{
<script type="text/javascript">
$(document).ready(function () {
$('#txtChild1').on('change', function () {
$("#C1Age1").show();
});
});
</script>
}
Error show
cannot resolve section 'Scripts'
If you use Layout = null then you don't need to use #section Scripts any more. you can simply use script tag.
Example:
#{
Layout = null;
}
<script type="text/javascript">
$(document).ready(function () {
$('#txtChild1').on('change', function () {
$("#C1Age1").show();
});
});
</script>
If you are not using layout page then you need not to add section. add scripts as you add them on a html page.
#{
Layout = null;
}
<script type="text/javascript">
$(document).ready(function () {
$('#txtChild1').on('change', function () {
$("#C1Age1").show();
});
});
</script>
You can also add script block without section if it is not mandatory.

jquery doesn't work after ajax auto refresh the updatepanel

<script>
!window.jQuery && document.write('<script src="jquery-1.4.3.min.js"><\/script>');
</script>
//this the jquery I'm using
<script type="text/javascript" src="jquery.fancybox-1.3.4.pack.js"></script>
<script type="text/javascript">
//the function calls the query
$(document).ready(function x() {
/*
* Examples - images
*/
//set the position of title on image
$("a#example7").imagebox({
'titlePosition': 'inside'
});
});
</script>
During Asynchronous page postback by updatepanels, $(document).ready function does n't get fired, so have to execute the code of $(document).ready in some other way.
you can execute any of these two ways:
1st way:
<script type="text/javascript">
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);
function InitializeRequest(sender, args) {
//do any action that you want to be executed before postbacks
}
function EndRequest(sender, args) {
$("a#example7").imagebox({
'titlePosition': 'inside'
});
}
</script>
2nd way i.e. using RegisterStartupScript:
<script type="text/javascript">
function CreateImageBox() {
$("a#example7").imagebox({
'titlePosition': 'inside'
});
}
</script>
in c# code use:
ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanelTab.GetType(), "createimagebox", "CreateImageBox();", true);

Using jquery-ui dialog as a confirm dialog with an ASP:LinkButton (how to invoke the postbck)

I'd like to use jQuery UI's dialog to implement a confirm dialog which is shown when the user clicks a delete-link (implemented using an asp:LinkButton).
I'm using code as shown below (copied from the jquery ui documentation):
<!-- the delete link -->
<asp:LinkButton ID="btnDelete" runat="server" Text="Delete"
OnClick="btnDelete_Click" CssClass="btnDelete"></asp:LinkButton>
<!-- the confirm-dialog -->
<div id="dialog-confirm-delete" title="Delete?" style="display:none;">
<p>Are you sure you want to permanently deleted the selected items?</p>
</div>
<script>
$(document).ready(function () {
// setup the dialog
$('#dialog-confirm-delete').dialog({
autoOpen: false,
modal: true,
buttons: {
"Delete all items": function () {
$(this).dialog("close");
// ===>>> how to invoke the default action here
},
Cancel: function () { $(this).dialog("close"); }
}
});
// display the dialog
$('.btnDelete').click(function () {
$('#dialog-confirm-cancel').dialog('open');
// return false to prevent the default action (postback)
return false;
});
});
</script>
So in the click event handler, I have to prevent the default action of the LinkButton (the postback) and instead display the dialog.
My question is: how can I then invoke the default action (the postback) of the delete link to perform the postback in case the user clicked the "Delete all items" button in the dialog?
OK, here's my approach (it works, but it might not be the best solution):
$(document).ready(function () {
$('#dialog-confirm-cancel').dialog({
autoOpen: false,
modal: true,
buttons: {
"Delete all items": function () {
// invoke the href (postback) of the linkbutton,
// that triggered the confirm-dialog
eval($(this).dialog('option', 'onOk'));
$(this).dialog("close");
},
Cancel: function () { $(this).dialog("close"); }
}
});
$('.btnDelete').click(function () {
$('#dialog-confirm-delete')
// pass the value of the LinkButton's href to the dialog
.dialog('option', 'onOk', $(this).attr('href'))
.dialog('open');
// prevent the default action, e.g., following a link
return false;
});
});
If you're not doing anything more than confirming you can add an attribute to the button.
<asp:LinkButton ID="btnDelete" runat="server" Text="Delete"
OnClientClick="if(!confirm('Are you sure?'))return false;" CssClass="btnDelete"></asp:LinkButton>
If you look at the Project Awesome on Codeplex it has a generic implementation of a Confirm Dialog that you can inspect for your scope.
So you prevented the default action of the link(following the link), right? So adding location.replace('path/to/file'); after $(this).dialog('close'); would solve your problem.
Not sure I understood your question right though.
Try adding $("#yourformid").submit(); at this spot // ===>>> how to invoke the default action here.
According to the docs: "... the default submit action on the form will be fired, so the form will be submitted."
Edit
You can try to do something like this:
$('.btnDelete').click(function (event, confirmed) {
if (confirmed) {
return true;
} else {
$('#dialog-confirm-cancel').dialog('open');
// prevent the default action, e.g., following a link
return false;
}
});
And then in your delete all items function:
"Delete all items": function () {
$(this).dialog("close");
$('.btnDelete').trigger("click", [true]);
},
$('#dialog-confirm-delete').dialog({
autoOpen: false,
modal: true,
buttons: {
Cancel: function() {
$(this).dialog("close");
},
"Delete all items": function() {
$(this).dialog("close");
// ===>>> how to invoke the default action here
}
}
});
If you are using a LinkButton you can do this:
__doPostBack("<%= lnkMyButton.UniqueID %>", "");

ASP.NET <Body onload="initialize()">

I am creating an ASP.NET custom control.
I want to set the body onload event of the ASPX page where the control resides.
Please note I cannot rely on the body tag in the ASPX page having runat="server".
any ideas??
Cheers.
Inline javascript! Just incase you can't use jQuery
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function')
{
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
} } }
addLoadEvent(initialize);
link to read http://www.webreference.com/programming/javascript/onloads/
Credits goto http://simonwillison.net/
If you include jQuery in your project you can use this:
jQuery(document).ready(function ()
{
// page load code here
});
You can run these multiple times on a page, ie handy within a control
Try this,
<script type="text/javascript">
window.onload = initialize();
function initialize() {
//your stuff
}
</script>

ASP.NET Button causes a postback

I have an asp.net button on the page. I am trying to do this:
$("#btn").click(function(e) {
e.preventDefault();
alert('hi');
}
But the alert never shows up. I have to use an ASP.NET button only and not a HTML one.
Try to have the method return false as well and in your buttons onclick you add the javascript. Like this:
$("#btn").click(function(e) {
e.preventDefault();
alert('hi');
return false;
}
or add it as an onclientclick function like this:
function ClickFunction() {
alert('hi');
return false;
}
<asp:Button OnClientClick="return Clickfunction();" />
Make sure you reference the ClientID of the button, not the "normal" ID.
$("#<%= MyControl.ClientID %>").click(function() {
alert(...);
return false;
}
Also make sure this is inside a document.ready() handler:
$(document).ready(function() {
$("#<%= MyControl.ClientID %>").click(function() {
alert(...);
return false;
}
});
Ok I got the answer over here
Using jQuery to Prevent ASP.NET Server Side Events from Occuring
I was missing the $(document).ready(function() . So silly of me!

Resources