Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am new to jquery, Please help me resolve the below issue.

I have a modal popup and inside the popup i have two asp buttons(Search and close). Onclick of the search button i want to perform a validation and then fire the server button click event and on click of the close button i want to close the dialog.

Problems faced: 1. Onclick of search the jquery validation is happening but the server side event is not getting fired. 2. Onclick of close, the dialog is getting closed only for the first time but after click of the search button once, the close JQUERY is not getting fired.

Below is the code:

button to open dialog

ASP.NET
<asp:Button ID="btnOpenDialog" runat="server" Text="Change"/>


jquery to open dialog

JavaScript
$("[id*=btnOpenDialog]").live("click", function () {
$("#modal_dialog").dialog({
title: "Details",
modal: true,
width: "700px"              
});
return false;
}) 


The dialog with the asp buttons

ASP.NET
<div id="modal_dialog" style="display:none">
<asp:Label ID="lblError" runat="server" Visible="true" ForeColor="Red"></asp:Label>                           

<asp:Label ID="lblLastName" runat="server" Text="Last Name"></asp:Label>
<asp:TextBox ID="txtLastName" Width="100px"  runat="server"></asp:TextBox>
<asp:Label ID="lblFirstName" runat="server" Text="First Name"> </asp:Label>                            
<asp:TextBox ID="txtFirstName" Width="100px" runat="server"></asp:TextBox>

<asp:Button ID="btnSearch" UseSubmitBehavior="false" runat="server" Text="Search" OnClick="btnSearch_Click"/>   
<asp:Button ID ="btnClose" Text="Close" runat="server"></asp:Button>      

</div>


The btnSearch JQUERY

JavaScript
$("[id*=btnSearch]").live("click", function (e) {          
//e.preventDefault();
var firstName = $("#<%= txtFirstName.ClientID %>").val();
var lastName = $("#<%= txtLastName.ClientID %>").val();

if(firstName == "" & lastName == "")
{                          
$("#<%= lblError.ClientID %>").text("Enter minimum two characters in either first name or last name");              
return false;
}
else
{
$("#<%= lblError.ClientID %>").text("");
$("#hdnFirstNameInitiator").val($("#<%= txtFirstName.ClientID %>").val());
$("#hdnLastNameInitiator").val($("#<%= txtLastName.ClientID %>").val());  
return true;
}
})

The close button JQUERY

JavaScript
$("[id*=btnClose]").live("click", function (e) {
//e.preventDefault();              
$("#modal_dialog").dialog('close');
return true;
})


Also if possible please brief me about appendTo function in jquery.

Please Help.

Thanks in advance,
Posted

1 solution

Please try below mentioned steps.

Step 1: Check whether your scripts having ay script error by using chrome dev tools

Step 2 : Don't use live method (which is outdated).Instead of use on method with your events.

Step 3 ; Use is as below.

C#
$("[id*=btnOpenDialog]").off('click').on('click', function(){
$("#modal_dialog").dialog({
title: "Details",
modal: true,
width: "700px"
});
return false;
})


XML
<div id="modal_dialog" style="display:none">
<asp:Label ID="lblError" runat="server" Visible="true" ForeColor="Red"></asp:Label>

<asp:Label ID="lblLastName" runat="server" Text="Last Name"></asp:Label>
<asp:TextBox ID="txtLastName" Width="100px"  runat="server"></asp:TextBox>
<asp:Label ID="lblFirstName" runat="server" Text="First Name"> </asp:Label>
<asp:TextBox ID="txtFirstName" Width="100px" runat="server"></asp:TextBox>

<asp:Button ID="btnSearch" UseSubmitBehavior="false" runat="server" Text="Search" OnClick="btnSearch_Click"/>
<asp:Button ID ="btnClose" Text="Close" runat="server"></asp:Button>

</div>




C#
$("[id*=btnSearch]").off('click').on('click', function(e){

var firstName = $("#<%= txtFirstName.ClientID %>").val();
var lastName = $("#<%= txtLastName.ClientID %>").val();

if(firstName == "" & lastName == "")
{
e.preventDefault();
$("#<%= lblError.ClientID %>").text("Enter minimum two characters in either first name or last name");
return false;
}
else
{
$("#<%= lblError.ClientID %>").text("");
$("#hdnFirstNameInitiator").val($("#<%= txtFirstName.ClientID %>").val());
$("#hdnLastNameInitiator").val($("#<%= txtLastName.ClientID %>").val());
return true;
}
})




C#
$("[id*=btnClose]").off('click').on('click', function(e){

$("#modal_dialog").dialog('close');
return false;
})



I hope this will help to you. :)
 
Share this answer
 
v6
Comments
Vasanth Thilakendran 19-Jan-14 2:20am    
@Sampath, Is the return statements in the btnClose and btnSearch JQUERY's correct ??
Sampath Lokuge 19-Jan-14 2:24am    
Sorry,I have updated it.It should be 'false'.It means 'prevent it from bubbling up'.
Vasanth Thilakendran 19-Jan-14 2:25am    
And I have to fire a server button click event which is not getting fired OnClick="btnSearch_Click" as mentioned in the question
Sampath Lokuge 19-Jan-14 2:36am    
If so you have to use 'OnClick' event on your server button (I updated my answer) and without using 'e.preventDefault()' method.After that both client and server side events must fire.Check that.
Sampath Lokuge 19-Jan-14 2:41am    
And also check whether your javascripts are inside the '$(document).ready(function () {}' method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900