Reputation: 907
I have to fetch the html select control value from the aspx code behind...I have done the following coding so far:
The following code snippet is the jquery to populate the dropdown:
<script type="text/javascript">
$(document).ready(function () { // load jQuery 1.5
function loadfail() {
alert("Error: Failed to read file!");
}
function parse(document) {
$(document).find("combo").each(function () {
var optionLabel = $(this).find('text').text();
var optionValue = $(this).find('value').text();
$('#combo1').append(
'<option value="' + optionValue + '">' + optionLabel + '</option>'
);
});
}
$.ajax({
url: 'dropdown.xml', // name of file with our data
dataType: 'xml', // type of file we will be reading
success: parse, // name of function to call when done reading file
error: loadfail // name of function to call when failed to read
});
});
</script>
The following code snippet is the html select declaration:
<select id="combo1" ></select>
Can anybody please help me how to get the selected value of the dropdown in aspx code behind.
Regards,
Upvotes: 0
Views: 1209
Reputation: 907
instead I have used the following trick
in .aspx page I have used this
<select id="combo1" name="a">
in .aspx.cs page I have used this
string strValue = Page.Request.Form["a"].ToString();
This gives me the index no and rest sorting I will do...
Upvotes: 0
Reputation: 468
Your dropdown should be added runat=server
<select id="combo1" runat = "server"></select>
and your script need to be changed, because you changed html control to server control. so that your script will be like this,
function parse(document) {
$(document).find($("#<%=combo1.ClientID %>")).each(function () {
var optionLabel = $(this).find('text').text();
var optionValue = $(this).find('value').text();
$('#').append(
'<option value="' + optionValue + '">' + optionLabel + '</option>'
);
});
}
Upvotes: 1