Question

Using javascript and asp.net. The AutoPostBack looks to be clearing the variable 'callBackReason' which I declare through javascript from the dropdownlist. Unfortunately I need the variable to be in Javascript. Is there a way to keep it declared even after the AutoPostBack?

Many thanks.

<asp:DropDownList AutoPostBack="true" OnSelectedIndexChanged="dropVehicleRequest_Changed" runat="server" ID="dropVehicleRequest"></asp:DropDownList>

<asp:DropDownList runat="server" ID="dropCallbackReason" SelectedIndexChanged="riskSeverityDropDown_SelectedIndexChanged" onChange="javascript:updateCallBackReason()" ClientIDMode="Static" >
     <asp:ListItem Text="-- Select Reason --" Value="1"></asp:ListItem>
     <asp:ListItem Text="Booking" Value="2"></asp:ListItem>
     <asp:ListItem Text="Discussing" Value="3"></asp:ListItem>
     <asp:ListItem Text="Contact" Value="4"></asp:ListItem>
</asp:DropDownList>

<script type="text/javascript">
    function updateCallBackReason() {
    callBackReason = document.getElementById('<%=dropCallbackReason.ClientID%>').options[document.getElementById('<%=dropCallbackReason.ClientID%>').selectedIndex].text;
    return callBackReason;
        }
</script>
Was it helpful?

Solution

Add a HiddenField to your page. then on updateCallBackReason() function call update document.getElementById('<%=dropCallbackReason.ClientID%>').selectedIndex to the hidden field. On form load you can set the dropCallbackReason selected index from hiddenfield.

Otherwise you can use updatepanel for implement this logic. Use updatepanel, hiddenfield, javascript __DoPostBack() call also. for detais search on google. then on updatepanel set the trigger as first dropdown.

OTHER TIPS

JavaScript is a client-side language, so if you do a PostBack you go to the server-side. If you want the dropdown value to be "default", you need to define it on server-side (onLoad).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top