dropdownlist server side event not called when client side event is also
defined
There is a dropdownlist and an ImageButton in a .aspx page. When user
selects the country Australia, There is the need to show an ImageButton (
a Flag Image), which takes the user to its respective website on clicking
it. Rest other websites are internally hosted so no need to display Image
Button.
There is a Client side as well as server side event defined for the
dropdownlist.
Problem is that the server side event of this dropdownlist :
OnSelectedIndexChanged="ddlSortBy_SelectedIndexChanged" is not getting
called. What went wrong ?
Also, as observed when the Client side event is removed, then Server side
event is getting called. Why so ?
<asp:DropDownList ID="ddlCountries" runat="server"
AutoPostBack="true" onchange="return ddlCountriesSelected();"
OnSelectedIndexChanged="ddlCountries_IndexChanged">
<asp:ListItem Text="_Select_" Selected="True"></asp:ListItem>
<asp:ListItem Text="Australia"></asp:ListItem>
<asp:ListItem Text="Spain"></asp:ListItem>
<asp:ListItem Text="England"></asp:ListItem>
<asp:ListItem Text="India"></asp:ListItem>
<asp:ListItem Text="Germany"></asp:ListItem>
</asp:DropDownList>
<asp:ImageButton ID="CountriesImage" runat="server" />
Client side:
<body>
<form id="form1" runat="server" >
<script type="text/javascript">
function ddlCountriesSelected() {
var ddlCntrs = document.getElementById('<%=ddlCountries.ClientID %>');
// alert(ddlCntrs.selectedIndex); // debug purpose
if (ddlCntrs.selectedIndex == "1")
{
var img = document.getElementById('<%=ImageButton1.ClientID%>');
img.src
='Images/'+ddlCntrs.options[ddlCntrs.selectedIndex].value+'.png';
return true;
}
else
{
// code to hide the ImageButton
return false;
}
}
</script>
...
...
</body>
// Code Behind
protected void ddlCountries_IndexChanged(object sender,System.EventArgs e)
{
lbl1.Text = ddlSortBy.SelectedValue;
}
No comments:
Post a Comment