Hi,
I have a dropdown list that I add the items from javascript depending on a selection made on a radiobuttonlist. The problem is that when the user clicks a button that do a PostBack, when the page reload the dropdownlist items are gone. I am new to javascript and I don't know how to control this. Please help me.
This is the code I am using:
On my PageLoad Event
Dim s As New System.Text.StringBuilder
s.AppendLine(" ddlClearItems('" & cbo_vol_cancer_endo5.ClientID.ToString & "');")
s.AppendLine("")
s.AppendLine(" var val = GetRadioButtonValue( document.getElementById('" & rb_CancerEndo5.ClientID & "'));")
s.AppendLine(" if (val == 'A') {")
s.AppendLine(" ddlAddItem('" & cbo_vol_cancer_endo5.ClientID.ToString & "','$400', '400');")
s.AppendLine(" ddlAddItem('" & cbo_vol_cancer_endo5.ClientID.ToString & "','$600', '600');")
s.AppendLine(" ddlAddItem('" & cbo_vol_cancer_endo5.ClientID.ToString & "','$800', '800');")
s.AppendLine(" ddlAddItem('" & cbo_vol_cancer_endo5.ClientID.ToString & "','$1,000', '1000');")
s.AppendLine(" }")
s.AppendLine(" else if (val == 'B') {")
s.AppendLine(" ddlAddItem('" & cbo_vol_cancer_endo5.ClientID.ToString & "','$100', '100');")
s.AppendLine(" ddlAddItem('" & cbo_vol_cancer_endo5.ClientID.ToString & "','$200', '200');")
s.AppendLine(" ddlAddItem('" & cbo_vol_cancer_endo5.ClientID.ToString & "','$300', '300');")
s.AppendLine(" ddlAddItem('" & cbo_vol_cancer_endo5.ClientID.ToString & "','$400', '400');")
s.AppendLine(" ddlAddItem('" & cbo_vol_cancer_endo5.ClientID.ToString & "','$500', '500');")
s.AppendLine(" ddlAddItem('" & cbo_vol_cancer_endo5.ClientID.ToString & "','$600', '600');")
s.AppendLine(" }")
s.AppendLine("")
rb_CancerEndo5.Attributes.Add("OnClick", s.ToString)
These are my javascript functions on the aspx
<script type="text/javascript">
function ddlClearItems(id) {
var ddl = document.getElementById(id);
if (ddl != null) {
ddl.innerHTML = '';
}
}
function ddlAddItem(id, text, value) {
var list = document.getElementById(id);
var newListItem = document.createElement('OPTION');
newListItem.text = text;
newListItem.value = value;
list.add(newListItem);
}
function GetRadioButtonValue(rblTable) {
var value = ''
for (var count = 0; count < rblTable.rows.length; count++) {
if (rblTable.rows[count].cells[0].getElementsByTagName('input').length > 0) {
var rbn = rblTable.rows[count].cells[0].getElementsByTagName('input')[0];
if (rbn != null) {
if (rbn.checked == true) {
value = rbn.value;
break;
}
}
}
}
return value
}
</script>
This is my dropdownlist:
<aspropDownList ID="cbo_vol_cancer_endo5"
runat="server"
Height="21px"
Width="140px"
AutoPostBack="False"
EnableViewState="false">
<asp:ListItem>0</asp:ListItem>
</aspropDownList>
And this is my radiobuttonlist:
<asp:RadioButtonList ID="rb_CancerEndo5"
runat="server"
Height="27px"
style="margin-left: 51px;"
Width="270px"
AutoPostBack="false"
EnableViewState="False">
<asp:ListItem Value="A">Opción A (Beneficio máximo de 12 meses)</asp:ListItem>
<asp:ListItem Value="B">Opción B (Beneficio máximo de 6 meses)</asp:ListItem>
</asp:RadioButtonList>
I have a dropdown list that I add the items from javascript depending on a selection made on a radiobuttonlist. The problem is that when the user clicks a button that do a PostBack, when the page reload the dropdownlist items are gone. I am new to javascript and I don't know how to control this. Please help me.
This is the code I am using:
On my PageLoad Event
Dim s As New System.Text.StringBuilder
s.AppendLine(" ddlClearItems('" & cbo_vol_cancer_endo5.ClientID.ToString & "');")
s.AppendLine("")
s.AppendLine(" var val = GetRadioButtonValue( document.getElementById('" & rb_CancerEndo5.ClientID & "'));")
s.AppendLine(" if (val == 'A') {")
s.AppendLine(" ddlAddItem('" & cbo_vol_cancer_endo5.ClientID.ToString & "','$400', '400');")
s.AppendLine(" ddlAddItem('" & cbo_vol_cancer_endo5.ClientID.ToString & "','$600', '600');")
s.AppendLine(" ddlAddItem('" & cbo_vol_cancer_endo5.ClientID.ToString & "','$800', '800');")
s.AppendLine(" ddlAddItem('" & cbo_vol_cancer_endo5.ClientID.ToString & "','$1,000', '1000');")
s.AppendLine(" }")
s.AppendLine(" else if (val == 'B') {")
s.AppendLine(" ddlAddItem('" & cbo_vol_cancer_endo5.ClientID.ToString & "','$100', '100');")
s.AppendLine(" ddlAddItem('" & cbo_vol_cancer_endo5.ClientID.ToString & "','$200', '200');")
s.AppendLine(" ddlAddItem('" & cbo_vol_cancer_endo5.ClientID.ToString & "','$300', '300');")
s.AppendLine(" ddlAddItem('" & cbo_vol_cancer_endo5.ClientID.ToString & "','$400', '400');")
s.AppendLine(" ddlAddItem('" & cbo_vol_cancer_endo5.ClientID.ToString & "','$500', '500');")
s.AppendLine(" ddlAddItem('" & cbo_vol_cancer_endo5.ClientID.ToString & "','$600', '600');")
s.AppendLine(" }")
s.AppendLine("")
rb_CancerEndo5.Attributes.Add("OnClick", s.ToString)
These are my javascript functions on the aspx
<script type="text/javascript">
function ddlClearItems(id) {
var ddl = document.getElementById(id);
if (ddl != null) {
ddl.innerHTML = '';
}
}
function ddlAddItem(id, text, value) {
var list = document.getElementById(id);
var newListItem = document.createElement('OPTION');
newListItem.text = text;
newListItem.value = value;
list.add(newListItem);
}
function GetRadioButtonValue(rblTable) {
var value = ''
for (var count = 0; count < rblTable.rows.length; count++) {
if (rblTable.rows[count].cells[0].getElementsByTagName('input').length > 0) {
var rbn = rblTable.rows[count].cells[0].getElementsByTagName('input')[0];
if (rbn != null) {
if (rbn.checked == true) {
value = rbn.value;
break;
}
}
}
}
return value
}
</script>
This is my dropdownlist:
<aspropDownList ID="cbo_vol_cancer_endo5"
runat="server"
Height="21px"
Width="140px"
AutoPostBack="False"
EnableViewState="false">
<asp:ListItem>0</asp:ListItem>
</aspropDownList>
And this is my radiobuttonlist:
<asp:RadioButtonList ID="rb_CancerEndo5"
runat="server"
Height="27px"
style="margin-left: 51px;"
Width="270px"
AutoPostBack="false"
EnableViewState="False">
<asp:ListItem Value="A">Opción A (Beneficio máximo de 12 meses)</asp:ListItem>
<asp:ListItem Value="B">Opción B (Beneficio máximo de 6 meses)</asp:ListItem>
</asp:RadioButtonList>