2016-10-26 3 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や他のクライアントを起動するために使用され、主にクライアント側のイベントを、になるだろうベースのアクション。 、あなたにそれが不可能な場合

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 

は何あなたがそうしたいと思うだろうことは、その最初の負荷にあなたのページにPage_Loadイベント内LoadStudent()メソッドを呼び出すと、前のonfocus属性を削除していますロードを実行する別のサーバー側イベントを作成し、使用するイベントとともにDropDownListにAutoPostBack="True"を設定することをお勧めします。

あなたは、個々の学生のすべてが含まれて別のリストを持っていた場合は、これらのいずれかが変更されたときたとえば、あなたがLoadStudent()イベントを呼び出すことができます。

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

ブラザーのドロップダウンリストには、選択ごとに取り込まれます生徒クラスなので、onfocusやonblurで書かなければなりません。私はページロードイベントでそれを書くことができません。 –

+0

DropDownListを更新する特定の学生の選択が変更されたときにイベントを設定できますか?あなたの学生が別のリストにあると仮定して、変更された時を決定するイベントを設定し、 'LoadStudent()'メソッドを呼び出します。 –

関連する問題