When I run this code on my desktop machine the dropdownlist works. When I run the code on the test server the page doesn't refresh with the result of the change in the dropdownlist on change function. When I step through in the visual studio debugger the
difference I can see is that the browserLink function doesn't get called after this linke <select name="DropDownList1" id="DropDownList1" onchange="changeCourse(event)">. I have active script enabled in the browser. Is there a javascript library I need to
install on the test machine or some script I need to enable.
I looked up browserLink on msdn and see that it is a .net core function. I see that on my desktop I have .net core 2.0 development tools, and .net core 1.0 - 1.1 development tools for web installed and I don't have those installed on the test server. But
those options are not in the test server Server Manager Select features. Could that be the problem?
courses.forEach(function (course) {
var value = course.RecordID + '|' + course.CourseCode + '|' + course.Title;
html += '<option value="' + value + '">' + course.Title + '</option>';
<tr>
<td class="auto-style10">Attendance Code: </td>
<td class="auto-style12">
<asp:TextBox ID="txtAttendanceCode" runat="server" ToolTip="Use the System Generated Random Code or click the Code Button to Generate a New Code, Youn can alsto Enter Your Own Code!"></asp:TextBox>
</td>
<td class="auto-style5">
<asp:Button ID="btnGenerateCode" runat="server" OnClick="BtnGenerateCode_Click" Text="Code" />
</td>
</tr>
</table>
<br />
<asp:Button ID="btnAdd" runat="server" Text="Save Code" OnClick="btnAdd_Click" />
<br />
<br />
<asp:Button ID="btnHelp" runat="server" Text="?" ToolTip="Instructions/Notes: You can use the system generated code or create your own. We recommend that it is user-friendly. All codes are unique to this course. Click Save Code to capture the attendance code
for this course session. Students must be in this class and on the Parker U wireless network to post their attendance. If students do not have a smartphone or laptop, you can allow them to log in to MyParker at the podium. " />
<br />
<br />
<asp:Label ID="lblMsg" runat="server" EnableViewState="False"></asp:Label>
<br />
<asp:Panel ID="Panel1" runat="server" Width="267px">
<%--<br />
Instructions/Notes:
<br />
-You can use the system generated code or create your own. We recommend that it is user-friendly.
<br />
-All codes are unique to this course.
<br />
-Click Save Code to capture the attendance code for this course session.
<br />
-Students must be in this class and on the Parker U wireless network to post their attendance.
<br />
-If students do not have a smartphone or laptop, you can allow them to log in to MyParker at the podium.--%>
</asp:Panel>
<asp:Label ID="Label2" runat="server" EnableViewState="False"></asp:Label>
<br />
<br />
<table>
<tr>
<%--<td class="auto-style4">Record ID: </td>--%>
<td class="auto-style2">
<asp:TextBox ID="txtRecordID" runat="server" Enabled="False" Visible="False"></asp:TextBox>
</td>
<td class="auto-style1">
<%-- will select next course on form load, by hostid, will go in form load</tr>--%>
</table>
<br />
<br />
</form>
<script type="text/javascript">
function load() {
var xhttp = new XMLHttpRequest();
xhttp.open('post', 'instructorcourse.aspx/GetCourseTitles', true);
xhttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhttp.setRequestHeader('Content-Type', 'application/json');
xhttp.onreadystatechange = function () {
if (xhttp.readyState == XMLHttpRequest.DONE && xhttp.status == 200) {
var $titlesDropDown = document.getElementById('DropDownList1'),
courses = JSON.parse(JSON.parse(this.responseText).d),
html = '';
courses.forEach(function (course) {
var value = course.RecordID + '|' + course.CourseCode + '|' + course.Title;
html += '<option value="' + value + '">' + course.Title + '</option>';
});
$titlesDropDown.innerHTML = html;
}
};
xhttp.send(JSON.stringify({ instructorId: '<%=txtInstructorID.Text%>' }));
}
document.addEventListener('DOMContentLoaded', load);
function changeCourse(e) {
var temp = e.target.value.split('|'),
recordID = parseInt(temp[0]),
courseCode = temp[1],
title = temp[2];
document.getElementById('<%=nameof(txtInstructorID)%>').value = recordID;
document.getElementById('<%=nameof(txtCourseCode)%>').value = courseCode;
document.getElementById('<%=nameof(txtCourseTitle)%>').value = title;
}
</script>
</body>
</html>
instructor cs script
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class updatecourses : System.Web.UI.Page
{
//MessageBox.Show("You are in the Form.Shown event.");
{
SqlConnection con = new SqlConnection(Database.ConnectionString);
try
{
var returnUrl = Request.Params["ReturnUrl"];
var hostID = Request.Params["HostID"];
if (string.IsNullOrWhiteSpace(hostID))
{
if (!string.IsNullOrWhiteSpace(returnUrl))
Response.Redirect(returnUrl);
throw new Exception("Variable \"HostID\" not found in query params");
}
con.Open();
var cmd = new SqlCommand("select * from OnGroundsWebSite.dbo.courses where InstructorID = @hostId", con);
//var hostID = Request.Params["HostID"];
cmd.Parameters.AddWithValue("hostId", hostID);
var dr = cmd.ExecuteReader();
if (!dr.Read())
Response.Redirect($"studentcourse.aspx?hostId={hostID}"); // won't need to redirect, instructor will click link on web site for Faculty (Instrcutor) or student
dr.Close();
//var query = string.IsNullOrWhiteSpace(txtRecordID.Text) ? "select * from courses where InstructorID = @instructorId" : "select * from courses where InstructorID = @instructorId and RecordID = @RecordID"; //---- should select from schedule
//var query = "select * from courses where InstructorID = @instructorId"; //need to join to get course and account info
var query = "select * from courses where InstructorID = @hostID"; //need to join to get course and account info
string characters = numbers;
int length = 5;
string otp = string.Empty;
for (int i = 0; i < length; i++)
{
string character = string.Empty;
do
{
int index = new Random().Next(0, 5);
character = characters.ToCharArray()[index].ToString();
} while (otp.IndexOf(character) != -1);
otp += character;
}
txtAttendanceCode.Text = otp;
}
}
string characters = numbers;
int length = 5;
string otp = string.Empty;
for (int i = 0; i < length; i++)
{
string character = string.Empty;
do
{
int index = new Random().Next(0, 5);
character = characters.ToCharArray()[index].ToString();
} while (otp.IndexOf(character) != -1);
otp += character;
}
txtAttendanceCode.Text = otp;
}
}
[WebMethod]
public static string GetCourseTitles(string instructorId)
{
var result = new List<dynamic>();
SqlConnection con = new SqlConnection(Database.ConnectionString);
try
{
con.Open();
//var cmd = new SqlCommand("select RecordID, CourseCode, CourseTitle from courses where InstructorID = @instructorId", con);
var cmd = new SqlCommand("select InstructorID, CourseCode, CourseTitle from courses where InstructorID = @instructorId", con);
cmd.Parameters.AddWithValue("instructorId", instructorId);
var reader = cmd.ExecuteReader();
while (reader.Read()) //reader, reading through results
{
result.Add(new
{
//RecordID = reader.GetInt32(1),
//RecordID = reader.GetInt64(1),
//HostID = reader.GetString(0), //runs with GetString, but should be RecordID
RecordID = reader.GetString(0), //InstructorID, getting by column id, assigning to variable for json function
//RecordID = reader.GetGuid(0),
//RecordID = reader.GetGuid(0),
CourseCode = reader.GetString(1),
Title = reader.GetString(2)
});
}
reader.Close();
}
protected void btnAdd_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(Database.ConnectionString);
try
{
con.Open(); //
SqlCommand cmd = new SqlCommand("insert into OnGroundsWebSite.dbo.InstructorCourse(RecordID,InstructorID,CourseID,CourseCode,CourseTitle,AttendanceCode) values(@RecordID,@InstructorID,@CourseID,@CourseCode,@CourseTitle,@AttendanceCode)", con);
//InstructorCourse, will have one record for instructors course
//attendance is Students attendance for course, multiple student records for InstructorCourse
//If instructor save and browse to page again, should it show attendance code saved, now shows new code
//if try to save, shows key violation
From your code, you bind a change event on a select.
<select name="DropDownList1" id="DropDownList1" onchange="changeCourse(event)">
</select> <%--on change, populates text boxes with result, then should call browserLink function, not calling now--%>
</select>
It seems you have one more </select> in your html.
And you fill the select with load method.
In your test server , have you seen the dropdownlist is filled?
And have you seen the changeCourse is called?
Since you are writing javascript code , I suggest you debug using F12 developer tool to check if there is any error message in your console in client and debug your javascript code.
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
18 Points
50 Posts
ASP.net, c#, website, dropdownlist, Not calling browserLink function
Apr 08, 2019 07:38 PM|Tom4IT|LINK
When I run this code on my desktop machine the dropdownlist works. When I run the code on the test server the page doesn't refresh with the result of the change in the dropdownlist on change function. When I step through in the visual studio debugger the difference I can see is that the browserLink function doesn't get called after this linke <select name="DropDownList1" id="DropDownList1" onchange="changeCourse(event)">. I have active script enabled in the browser. Is there a javascript library I need to install on the test machine or some script I need to enable.
I looked up browserLink on msdn and see that it is a .net core function. I see that on my desktop I have .net core 2.0 development tools, and .net core 1.0 - 1.1 development tools for web installed and I don't have those installed on the test server. But those options are not in the test server Server Manager Select features. Could that be the problem?
courses.forEach(function (course) {
var value = course.RecordID + '|' + course.CourseCode + '|' + course.Title;
html += '<option value="' + value + '">' + course.Title + '</option>';
instructor aspx listing
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="instructorcourse.aspx.cs" Inherits="updatecourses" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head3" runat="server">
<title>On Grounds</title>
<style type="text/css">
.auto-style1 {
width: 30px;
}
.auto-style2 {
width: 132px;
}
.auto-style5 {
width: 24px;
}
.auto-style7 {
width: 140px;
height: 26px;
}
.auto-style8 {
width: 24px;
height: 26px;
}
.auto-style9 {
width: 273px;
}
.auto-style10 {
width: 123px;
}
.auto-style11 {
width: 123px;
height: 26px;
}
.auto-style12 {
/*width: 140px;*/
}
</style>
</head>
<body>
<%--<script>JavaScript is Active</script>
<noscript>JavaScript is Disabled</noscript>--%>
<form id="form3" runat="server">
<h2 class="auto-style9">
<asp:Image ID="Image1" runat="server" Height="103px" Width="216px" ImageUrl="~/headerLogo.png" />
</h2>
<h2>Attendance</h2>
<h2>Intructor - Next Course</h2>
<table>
<tr>
<td class="auto-style10">Insctructor ID:</td>
<td class="auto-style12">
<asp:TextBox ID="txtInstructorID" runat="server" Enabled="False"></asp:TextBox></td>
<td class="auto-style5"></td>
</tr>
<tr>
<td class="auto-style11">Instructor Name:</td>
<td class="auto-style7">
<asp:TextBox ID="txtInstructorName" runat="server" Enabled="False"></asp:TextBox></td>
<td class="auto-style8"></td>
</tr>
<tr>
<td class="auto-style10">Course Code:</td>
<td class="auto-style12">
<asp:TextBox ID="txtCourseCode" runat="server" Enabled="False"></asp:TextBox></td>
<td class="auto-style5"> </td>
</tr>
<tr>
<td class="auto-style10">Course Title:</td>
<td class="auto-style12">
<asp:TextBox ID="txtCourseTitle" runat="server" Enabled="False"></asp:TextBox></td>
<td class="auto-style5">
<select name="DropDownList1" id="DropDownList1" onchange="changeCourse(event)">
</select> <%--on change, populates text boxes with result, then should call browserLink function, not calling now--%>
</select>
</td>
</tr>
<tr>
<td class="auto-style10">Course StartTime:</td>
<td class="auto-style12">
<asp:TextBox ID="txtStartTime" runat="server" Enabled="False"></asp:TextBox></td>
</tr>
<tr>
<td class="auto-style10">Attendance Code: </td>
<td class="auto-style12">
<asp:TextBox ID="txtAttendanceCode" runat="server" ToolTip="Use the System Generated Random Code or click the Code Button to Generate a New Code, Youn can alsto Enter Your Own Code!"></asp:TextBox>
</td>
<td class="auto-style5">
<asp:Button ID="btnGenerateCode" runat="server" OnClick="BtnGenerateCode_Click" Text="Code" />
</td>
</tr>
</table>
<br />
<asp:Button ID="btnAdd" runat="server" Text="Save Code" OnClick="btnAdd_Click" />
<br />
<br />
<asp:Button ID="btnHelp" runat="server" Text="?" ToolTip="Instructions/Notes: You can use the system generated code or create your own. We recommend that it is user-friendly. All codes are unique to this course. Click Save Code to capture the attendance code for this course session. Students must be in this class and on the Parker U wireless network to post their attendance. If students do not have a smartphone or laptop, you can allow them to log in to MyParker at the podium. " />
<br />
<br />
<asp:Label ID="lblMsg" runat="server" EnableViewState="False"></asp:Label>
<br />
<asp:Panel ID="Panel1" runat="server" Width="267px">
<%--<br />
Instructions/Notes:
<br />
-You can use the system generated code or create your own. We recommend that it is user-friendly.
<br />
-All codes are unique to this course.
<br />
-Click Save Code to capture the attendance code for this course session.
<br />
-Students must be in this class and on the Parker U wireless network to post their attendance.
<br />
-If students do not have a smartphone or laptop, you can allow them to log in to MyParker at the podium.--%>
</asp:Panel>
<asp:Label ID="Label2" runat="server" EnableViewState="False"></asp:Label>
<br />
<br />
<table>
<tr>
<%--<td class="auto-style4">Record ID: </td>--%>
<td class="auto-style2">
<asp:TextBox ID="txtRecordID" runat="server" Enabled="False" Visible="False"></asp:TextBox>
</td>
<td class="auto-style1">
<%-- will select next course on form load, by hostid, will go in form load</tr>--%>
</table>
<br />
<br />
</form>
<script type="text/javascript">
function load() {
var xhttp = new XMLHttpRequest();
xhttp.open('post', 'instructorcourse.aspx/GetCourseTitles', true);
xhttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhttp.setRequestHeader('Content-Type', 'application/json');
xhttp.onreadystatechange = function () {
if (xhttp.readyState == XMLHttpRequest.DONE && xhttp.status == 200) {
var $titlesDropDown = document.getElementById('DropDownList1'),
courses = JSON.parse(JSON.parse(this.responseText).d),
html = '';
courses.forEach(function (course) {
var value = course.RecordID + '|' + course.CourseCode + '|' + course.Title;
html += '<option value="' + value + '">' + course.Title + '</option>';
});
$titlesDropDown.innerHTML = html;
}
};
xhttp.send(JSON.stringify({ instructorId: '<%=txtInstructorID.Text%>' }));
}
document.addEventListener('DOMContentLoaded', load);
function changeCourse(e) {
var temp = e.target.value.split('|'),
recordID = parseInt(temp[0]),
courseCode = temp[1],
title = temp[2];
document.getElementById('<%=nameof(txtInstructorID)%>').value = recordID;
document.getElementById('<%=nameof(txtCourseCode)%>').value = courseCode;
document.getElementById('<%=nameof(txtCourseTitle)%>').value = title;
}
</script>
</body>
</html>
instructor cs script
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class updatecourses : System.Web.UI.Page
{
protected void Page_LoadComplete(object sender, EventArgs e)
{
//MessageBox.Show("You are in the Form.Shown event.");
{
SqlConnection con = new SqlConnection(Database.ConnectionString);
try
{
var returnUrl = Request.Params["ReturnUrl"];
var hostID = Request.Params["HostID"];
if (string.IsNullOrWhiteSpace(hostID))
{
if (!string.IsNullOrWhiteSpace(returnUrl))
Response.Redirect(returnUrl);
throw new Exception("Variable \"HostID\" not found in query params");
}
con.Open();
var cmd = new SqlCommand("select * from OnGroundsWebSite.dbo.courses where InstructorID = @hostId", con);
//var hostID = Request.Params["HostID"];
cmd.Parameters.AddWithValue("hostId", hostID);
var dr = cmd.ExecuteReader();
if (!dr.Read())
Response.Redirect($"studentcourse.aspx?hostId={hostID}"); // won't need to redirect, instructor will click link on web site for Faculty (Instrcutor) or student
dr.Close();
//string accountId = Request.Cookies["AccountIdentifier"].Value;
//if (string.IsNullOrWhiteSpace(accountId))
// Response.Redirect("/enteremail.aspx");
//var query = string.IsNullOrWhiteSpace(txtRecordID.Text) ? "select * from courses where InstructorID = @instructorId" : "select * from courses where InstructorID = @instructorId and RecordID = @RecordID"; //---- should select from schedule
//var query = "select * from courses where InstructorID = @instructorId"; //need to join to get course and account info
var query = "select * from courses where InstructorID = @hostID"; //need to join to get course and account info
//cmd.Parameters.AddWithValue("HostID", "Int");
cmd = new SqlCommand(query, con);
//cmd.Parameters.AddWithValue("instructorId", accountId);
//cmd.Parameters.AddWithValue("instructorId", hostID);
cmd.Parameters.AddWithValue("HostID", hostID);
cmd.Parameters.AddWithValue("RecordID", txtRecordID.Text);
cmd.Parameters.AddWithValue("now", DateTime.Now);
//cmd.Parameters.AddWithValue("Now", DateTime.Now);
dr = cmd.ExecuteReader();
if (dr.Read())
{
// display data in textboxes
txtRecordID.Text = dr["RecordID"].ToString();
txtInstructorID.Text = dr["InstructorID"].ToString();
//txtInstructorID.Text = dr["HostID"].ToString();
txtInstructorName.Text = dr["InstructorName"].ToString();
txtCourseCode.Text = dr["CourseCode"].ToString();
txtCourseTitle.Text = dr["CourseTitle"].ToString();
//txtStartTime.Text = dr["StartTime"].ToString();
txtAttendanceCode.Text = dr["AttendanceCode"].ToString();
//btnUpdate.Enabled = true;
//btnUpdate.Visible = true;
}
else
{
lblMsg.Text = "You do not have a class in the next hour.";
//btnUpdate.Enabled = false;
//btnUpdate.Visible = false;
}
dr.Close();
}
catch (Exception ex)
//catch (Exception)
{
lblMsg.Text = "Error --> " + ex.Message;
}
finally
{
con.Close();
}
if (string.IsNullOrWhiteSpace(txtAttendanceCode.Text))
{
//string alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//string small_alphabets = "abcdefghijklmnopqrstuvwxyz";
string numbers = "1234567890";
string characters = numbers;
int length = 5;
string otp = string.Empty;
for (int i = 0; i < length; i++)
{
string character = string.Empty;
do
{
int index = new Random().Next(0, 5);
character = characters.ToCharArray()[index].ToString();
} while (otp.IndexOf(character) != -1);
otp += character;
}
txtAttendanceCode.Text = otp;
}
}
}
protected void BtnGenerateCode_Click(object sender, EventArgs e)
{
{
//string alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//string small_alphabets = "abcdefghijklmnopqrstuvwxyz";
string numbers = "1234567890";
string characters = numbers;
int length = 5;
string otp = string.Empty;
for (int i = 0; i < length; i++)
{
string character = string.Empty;
do
{
int index = new Random().Next(0, 5);
character = characters.ToCharArray()[index].ToString();
} while (otp.IndexOf(character) != -1);
otp += character;
}
txtAttendanceCode.Text = otp;
}
}
[WebMethod]
public static string GetCourseTitles(string instructorId)
{
var result = new List<dynamic>();
SqlConnection con = new SqlConnection(Database.ConnectionString);
try
{
con.Open();
//var cmd = new SqlCommand("select RecordID, CourseCode, CourseTitle from courses where InstructorID = @instructorId", con);
var cmd = new SqlCommand("select InstructorID, CourseCode, CourseTitle from courses where InstructorID = @instructorId", con);
cmd.Parameters.AddWithValue("instructorId", instructorId);
var reader = cmd.ExecuteReader();
while (reader.Read()) //reader, reading through results
{
result.Add(new
{
//RecordID = reader.GetInt32(1),
//RecordID = reader.GetInt64(1),
//HostID = reader.GetString(0), //runs with GetString, but should be RecordID
RecordID = reader.GetString(0), //InstructorID, getting by column id, assigning to variable for json function
//RecordID = reader.GetGuid(0),
//RecordID = reader.GetGuid(0),
CourseCode = reader.GetString(1),
Title = reader.GetString(2)
});
}
reader.Close();
}
catch
{
}
finally
{
con.Close();
}
return JsonConvert.SerializeObject(result);
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(Database.ConnectionString);
try
{
con.Open(); //
SqlCommand cmd = new SqlCommand("insert into OnGroundsWebSite.dbo.InstructorCourse(RecordID,InstructorID,CourseID,CourseCode,CourseTitle,AttendanceCode) values(@RecordID,@InstructorID,@CourseID,@CourseCode,@CourseTitle,@AttendanceCode)", con);
//InstructorCourse, will have one record for instructors course
//attendance is Students attendance for course, multiple student records for InstructorCourse
//If instructor save and browse to page again, should it show attendance code saved, now shows new code
//if try to save, shows key violation
cmd.Parameters.AddWithValue("@RecordID", txtRecordID.Text);
cmd.Parameters.AddWithValue("@InstructorID", txtInstructorID.Text);
cmd.Parameters.AddWithValue("@CourseID", txtRecordID.Text);
cmd.Parameters.AddWithValue("@CourseCode", txtCourseCode.Text);
cmd.Parameters.AddWithValue("@CourseTitle", txtCourseTitle.Text);
cmd.Parameters.AddWithValue("@AttendanceCode", txtAttendanceCode.Text);
int count = cmd.ExecuteNonQuery();
if (count == 1)
//lblMsg.Text = "Attendance [" + txtCourseTitle.Text + "] has been added!";
lblMsg.Text = "Attendance Code [" + txtCourseCode.Text + "] has been recorded!";
else
lblMsg.Text = "Could not add Attendance!";
}
catch (Exception ex)
{
lblMsg.Text = "Error --> " + ex.Message;
}
finally
{
con.Close();
}
}
}
Contributor
3500 Points
1300 Posts
Re: ASP.net, c#, website, dropdownlist, Not calling browserLink function
Apr 09, 2019 02:14 AM|Ackerly Xu|LINK
Hi Tom4IT ,
Not clear about what browserLink function means.
From your code, you bind a change event on a select.
It seems you have one more </select> in your html.
And you fill the select with load method.
In your test server , have you seen the dropdownlist is filled?
And have you seen the changeCourse is called?
Since you are writing javascript code , I suggest you debug using F12 developer tool to check if there is any error message in your console in client and debug your javascript code.
https://developers.google.com/web/tools/chrome-devtools/javascript/
Best regards,
Ackerly Xu
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.