Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript Filled DropdownList gets Cleared on PostBack 1

Status
Not open for further replies.

jmro20

Programmer
Aug 3, 2009
6
PR
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:
<asp:DropDownList ID="cbo_vol_cancer_endo5"
runat="server"
Height="21px"
Width="140px"
AutoPostBack="False"
EnableViewState="false">
<asp:ListItem>0</asp:ListItem>
</asp:DropDownList>


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>
 
Javascript runs on the client browser. It runs whilst the page is visible - but doesn't "remember" anything between page reloads. That is your problem.

A solution is to build up the correct dropdown items server-side (based on the posted data) or to track these dropdown items using a cookie. The values from the cookie can be read from page to page (within the same domain)... effectively giving you a way to store small bits of data between page loads using javascript.

Welcome to the journey.

Cheers,
Jeff

[tt]Visit my blog [!]@[/!] Visit Code Couch [!]@[/!] [/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top