2016-10-26 55 views
0

当焦点进入HTML下拉列表时,我无法调用Server Side LoadStudent()方法。在Html和asp.net中加载服务器端函数onfocus

服务器端的编码

Imports System.Data.SqlClient 

Public Class FeeEntry 
    Public Sub LoadStudent() 
     MsgBox("Called") 
     Using cmd As New SqlCommand("SELECT StudentName FROM Student") 
      cmd.CommandType = CommandType.Text 
      cmd.Connection = con 
      'con.Open() 
      DropDownList1.DataSource = cmd.ExecuteReader() 
      DropDownList1.DataTextField = "StudentName" 
      DropDownList1.DataValueField = "StudentName" 
      DropDownList1.DataBind() 
      con.Close() 
     End Using 
    End Sub 

End Class 

HTML代码

<asp:DropDownList ID = "DropDownList1" runat="server" onfocus="LoadStudent()" CssClass ="form-control"> 
      </asp:DropDownList> 

回答

0

onfocus事件将是一个主要客户端的事件,这是用来触发JavaScript和其他客户端基于行为。

你可能会想要做的是调用Page_Load事件中LoadStudent()方法在您的网页上最初的装入和取出以前onfocus属性:

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load 
     ' If it is the first time the page is being loaded, bind your data ' 
     If Not IsPostBack Then 
      LoadStudent() 
     End If 
End Sub 

如果这是不可行的,你可能会想要创建另一个服务器端事件来执行您的加载,并在您的DropDownList上设置AutoPostBack="True"以及您决定使用的事件。

举例来说,如果你有包含所有个别学生的单独列表,你可以调用LoadStudent()事件时,这些改变之一:

<asp:DropDownList ID="StudentsList" AutoPostBack="True" OnSelectedIndexChanged="LoadStudents">...</asp>DropDownList> 
+0

兄弟下拉列表将被填充将按照所选学生课,所以它必须写在onfocus或onblur我不能写在页面加载事件 –

+0

你可以设置一个事件,当你的具体学生选择被更改,将更新DropDownList?假设你的学生在一个单独的列表中,只需设置一个事件来确定它何时发生变化,然后调用LoadStudent()方法。 –

相关问题