I am a bit new to both javascript / asp.net but have a small dilemma, I have this simple page here (it is going to be a product page with options). If you select an option it returns the ID to the label right now so I know what has been selected, I need to get these 2 option ID's, query a database so it returns me a SKU in the label at the bottom without doing a postback, I read somewhere I should use some kind of ashx file but I don't really know what that means, any kind of steer on the right way forward and code examples is greatly appreciated.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" />
<script type="text/javascript">
function SetOptions() {
var a = document.getElementById("ddOption1");
var selectedIDa = a.options[a.selectedIndex].value;
document.getElementById('option1').innerHTML = selectedIDa;
var b = document.getElementById("ddOption2");
var selectedIDb = b.options[b.selectedIndex].value;
document.getElementById('option2').innerHTML = selectedIDb;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div class="container">
<table class="table">
<tr>
<td>Color:</td>
<td>
<select name="ddOption1" id="ddOption1" onchange="SetOptions()">
<option selected="selected" value="0">-- Select --</option>
<option value="283">Blue</option>
<option value="299">White</option>
<option value="296">Red</option>
<option value="300">Yellow</option>
</select>
</td>
<td>
<label id="option1"></label>
</td>
</tr>
<tr>
<td>Size:</td>
<td>
<select name="ddOption2" id="ddOption2" onchange="SetOptions()">
<option selected="selected" value="0">-- Select --</option>
<option value="1">Small</option>
<option value="2">Medium</option>
<option value="3">Large</option>
</select>
</td>
<td>
<label id="option2"></label>
</td>
</tr>
</table>
<br />
<label id="sku">SKU = </label>
</div>
</form>
</body>
</html>
.ashxis an older ASP.net handler extension that is basically an endpoint you can call via AJAX, which is what you need in order to avoid doing a postback. The more modern replacement is ASP.net Web API; I would suggest to do some reading on that. Create the ASP.net Web API project which will have the endpoint (URL) that you will post data to from your JavaScript, and the backend of the WebAPI will query your database. For the AJAX call, there are .net AJAX components I believe, or look into thefetch()API in modern JavaScript, orjQuery.ajax()(jQuery ships with ASP.net by default).