When you try to change some selection in dropdown, Client side function ‘Validate()’ fires and returns ‘false’.
But irrespective of it return true or false, the selection gets changed in dropdown.
Solution Required:
But my requirement is if Validate() returns false then the selection should not get changed. Please help.
Regards,
Arun
client-side validationASP.NET 2.0Asp .netasp .net 1.1 web form controlsClient Side Web Development
Its a default behaviour of Client Side SelectedIndex Change event of DropdownList, It fire the selectedIndex Change event when index has been changed. so if you want to apply some validation on this event and only want to set the selected index if some condition
full fill. then you have to use a Hidden Field , and set its value = DropDownList1.SelectedIndex on server side, and in in Client Side Validtion Function, if it return false then set DropDownList SelectedIndex = Hidden Field value
arunmanglick
Member
23 Points
57 Posts
Problem - Stopping change of Selection in Drop down if JS returns false
May 11, 2007 06:50 AM|LINK
Let say you have a drop down. You have a JS attached with it.
// ------------------------------------------------------------------------------------------------------------------------
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" CausesValidation="True" onchange="return Validate();" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>aa</asp:ListItem>
<asp:ListItem>bb</asp:ListItem>
<asp:ListItem>cc</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="" OnClick="Button1_Click" Visible="True" /> // Will be hidden button
// ------------------------------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------------------------------
<script type="text/javascript">
function Validate()
{
num = 4; // Here, could be any compelx logic.
if(num > 5)
return true;
else
return false;
}
</script>
// ------------------------------------------------------------------------------------------------------------------------
</script>
What is wrong:
When you try to change some selection in dropdown, Client side function ‘Validate()’ fires and returns ‘false’.
But irrespective of it return true or false, the selection gets changed in dropdown.
Solution Required:
But my requirement is if Validate() returns false then the selection should not get changed.
Please help.
Regards,
Arun
client-side validation ASP.NET 2.0 Asp .net asp .net 1.1 web form controls Client Side Web Development
HostingASPNe...
All-Star
15882 Points
2977 Posts
Re: Problem - Stopping change of Selection in Drop down if JS returns false
May 11, 2007 08:38 AM|LINK
hi,
try this:
<script type="text/javascript">
function Validate()
{
ddl=document.frmMain.DropDownList1;
num = 4; // Here, could be any compelx logic.
if(num > 5)
return true;
else
ddl.options[ddl.selectedIndex].value = "oldSelectedValue"; // You should previously defined the oldSelectedValue in onLoad event;
return false;
}
</script>
Free ASP.NET Examples and source code.
HostingASPNe...
All-Star
15882 Points
2977 Posts
Re: Problem - Stopping change of Selection in Drop down if JS returns false
May 11, 2007 08:43 AM|LINK
hi,
try this:
<script type="text/javascript">
function Validate()
{
ddl=document.frmMain.DropDownList1;
num = 4; // Here, could be any compelx logic.
if(num > 5)
return true;
else
ddl.options[ddl.selectedIndex].value = "oldSelectedValue"; // You should previously defined the oldSelectedValue in onLoad event;
return false;
}
</script>
Free ASP.NET Examples and source code.
HostingASPNe...
All-Star
15882 Points
2977 Posts
Re: Problem - Stopping change of Selection in Drop down if JS returns false
May 11, 2007 08:48 AM|LINK
hi,
try this:
<script type="text/javascript">
function Validate()
{
ddl=document.frmMain.DropDownList1;
num = 4; // Here, could be any compelx logic.
if(num > 5)
return true;
else
ddl.options[ddl.selectedIndex].value = "oldSelectedValue"; // You should previously defined the oldSelectedValue in onLoad event;
return false;
}
</script>
Free ASP.NET Examples and source code.
HostingASPNe...
All-Star
15882 Points
2977 Posts
Re: Problem - Stopping change of Selection in Drop down if JS returns false
May 11, 2007 08:51 AM|LINK
hi,
try this:
<script type="text/javascript">
function Validate()
{
ddl=document.frmMain.DropDownList1;
num = 4; // Here, could be any compelx logic.
if(num > 5)
return true;
else
ddl.options[ddl.selectedIndex].value = "oldSelectedValue"; // You should previously defined the oldSelectedValue in onLoad event;
return false;
}
</script>
Free ASP.NET Examples and source code.
akhhttar
Contributor
6506 Points
963 Posts
Re: Problem - Stopping change of Selection in Drop down if JS returns false
May 11, 2007 08:54 AM|LINK
Hi Arun,
Its a default behaviour of Client Side SelectedIndex Change event of DropdownList, It fire the selectedIndex Change event when index has been changed. so if you want to apply some validation on this event and only want to set the selected index if some condition full fill. then you have to use a Hidden Field , and set its value = DropDownList1.SelectedIndex on server side, and in in Client Side Validtion Function, if it return false then set DropDownList SelectedIndex = Hidden Field value
try following
1: Declare a Hidden Field
<input id="Hidden1" name="Hidden1" type="hidden" runat="server" />2: Set Its Value Before Rendering
Protected
Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRenderHidden1.Value = DropDownList1.SelectedIndex
End Sub3:On Client SIde Validtion
<script type="text/javascript"> function Validate(){
num = 4;
// Here, could be any compelx logic. if(num > 5) return true; else{
document.form1.DropDownList1.selectedIndex = document.form1.Hidden1.value;
return false;}
}
</
script>Hope it works
Thank You,
Best Regards,
Muhammad Akhtar Shiekh
My Blog
HostingASPNe...
All-Star
15882 Points
2977 Posts
Re: Problem - Stopping change of Selection in Drop down if JS returns false
May 11, 2007 09:03 AM|LINK
hi,
try this:
<script type="text/javascript">
function Validate()
{
ddl=document.frmMain.DropDownList1;
num = 4; // Here, could be any compelx logic.
if(num > 5)
return true;
else
ddl.options[ddl.selectedIndex].value = "oldSelectedValue"; // You should previously defined the oldSelectedValue in onLoad event;
return false;
}
</script>
Free ASP.NET Examples and source code.
HostingASPNe...
All-Star
15882 Points
2977 Posts
Re: Problem - Stopping change of Selection in Drop down if JS returns false
May 11, 2007 09:06 AM|LINK
hi,
try this:
<script type="text/javascript">
function Validate()
{
ddl=document.frmMain.DropDownList1;
num = 4; // Here, could be any compelx logic.
if(num > 5)
return true;
else
ddl.options[ddl.selectedIndex].value = "oldSelectedValue"; // You should previously defined the oldSelectedValue in onLoad event;
return false;
}
</script>
Free ASP.NET Examples and source code.
HostingASPNe...
All-Star
15882 Points
2977 Posts
Re: Problem - Stopping change of Selection in Drop down if JS returns false
May 11, 2007 09:16 AM|LINK
Tere is some problem with the Forum, sorry for the replies...
Regards
Free ASP.NET Examples and source code.
arunmanglick
Member
23 Points
57 Posts
Re: Problem - Stopping change of Selection in Drop down if JS returns false
May 11, 2007 02:04 PM|LINK
Thanks & Solved.Actually I tried the same, but due to the 'AJAXified' page, it was producing error. Bu now it is solved. Thanks once again.
Regards,
Arun...