HtmlSelect control

  • Thread starter Thread starter Jonathan
  • Start date Start date
J

Jonathan

Hi everyone,
I have a problem with a HtmlSelect control that I am loading dynamically
in javascript. Here is my code:

<asp:DropDownList id="MyDropDownList1" onChange="LoadContent(this);"
runat="server">
<asp:ListItem value="0">&nbsp;</asp:ListItem>
<asp:ListItem value="1">MyChoice1</asp:ListItem>
<asp:ListItem value="2">MyChoice2</asp:ListItem>
</asp:DropDownList>

<select id="MyList2" runat="server">
<option></option>
</select>

<asp:imageButton id="Save" imageURL="./Images/Save.gif"
onclick="Save_click" runat="server" />

<script language="javascript">
function LoadContent(elem)
{
if(elem.options[elem.selectedIndex].value==1)
{
var optionText = "MyText1";
var optionValue = 0;
var optionObject = new Option(optionText,optionValue);
optionRank = document.Myform.MyList2.options.length;
document.Myform.MyList2.options[optionRank]=optionObject;


optionText = "MyText2";
optionValue = 1;
optionObject = new Option(optionText,optionValue);
optionRank = document.Myform.MyList2.options.length;
document.Myform.MyList2.options[optionRank]=optionObject;
}

if(elem.options[elem.selectedIndex].value==2)
{
var optionText = "SomeText1";
var optionValue = 0;
var optionObject = new Option(optionText,optionValue);
optionRank = document.Myform.MyList2.options.length;
document.Myform.MyList2.options[optionRank]=optionObject;


optionText = "SomeText2";
optionValue = 1;
optionObject = new Option(optionText,optionValue);
optionRank = document.Myform.MyList2.options.length;
document.Myform.MyList2.options[optionRank]=optionObject;
}
}
</script>

<script language="vb" runat="server">
Sub Save_click( ByVal Sender as Object, ByVal As
ImageClickEventArgs )

//Get my value from MyList2 here

End Sub
</script>

Basically, my javascript function loads MyList2 (HtmlSelect control)
depending on what choice I made in my MyDropDownList1. I want to get the
value I selected in MyList2 when clicking on my Save ImageButton but i
don't know how to. I tried MyList2.Value but it is empty or i tried
MyList2.selectedIndex and it returns me 0. What am I doing wrong?

Any helps is greatly appreciated!

Jonathan
 
Hi everyone,
I have a problem with a HtmlSelect control that I am loading
dynamically in javascript. Here is my code:

<asp:DropDownList id="MyDropDownList1" onChange="LoadContent(this);"
runat="server">
<asp:ListItem value="0">&nbsp;</asp:ListItem>
<asp:ListItem value="1">MyChoice1</asp:ListItem>
<asp:ListItem value="2">MyChoice2</asp:ListItem>
</asp:DropDownList>

<select id="MyList2" runat="server">
<option></option>
</select>

<asp:imageButton id="Save" imageURL="./Images/Save.gif"
onclick="Save_click" runat="server" />

<script language="javascript">
function LoadContent(elem)
{
if(elem.options[elem.selectedIndex].value==1)
{
var optionText = "MyText1";
var optionValue = 0;
var optionObject = new Option(optionText,optionValue);
optionRank = document.Myform.MyList2.options.length;
document.Myform.MyList2.options[optionRank]=optionObject;


optionText = "MyText2";
optionValue = 1;
optionObject = new Option(optionText,optionValue);
optionRank = document.Myform.MyList2.options.length;
document.Myform.MyList2.options[optionRank]=optionObject;
}

if(elem.options[elem.selectedIndex].value==2)
{
var optionText = "SomeText1";
var optionValue = 0;
var optionObject = new Option(optionText,optionValue);
optionRank = document.Myform.MyList2.options.length;
document.Myform.MyList2.options[optionRank]=optionObject;


optionText = "SomeText2";
optionValue = 1;
optionObject = new Option(optionText,optionValue);
optionRank = document.Myform.MyList2.options.length;
document.Myform.MyList2.options[optionRank]=optionObject;
}
}
</script>

<script language="vb" runat="server">
Sub Save_click( ByVal Sender as Object, ByVal As
ImageClickEventArgs )

//Get my value from MyList2 here

End Sub
</script>

Basically, my javascript function loads MyList2 (HtmlSelect control)
depending on what choice I made in my MyDropDownList1. I want to get
the value I selected in MyList2 when clicking on my Save ImageButton
but i don't know how to. I tried MyList2.Value but it is empty or i
tried MyList2.selectedIndex and it returns me 0. What am I doing
wrong?

Any helps is greatly appreciated!

Jonathan

It's ok, i found it.
For those interested, here is what i did:

1- i change my HtmlSelect control to classic html (no more runat=server)
<select name="MyList2">
<option></option>
</select>

2-on the Save_Click (after post to the server), i get my value by doing
a classic request

request.form("MyList2")


Jonathan
 
Back
Top