【问题标题】:Load Server Side function onfocus in Html and asp.net在 Html 和 asp.net 中加载服务器端函数 onfocus
【发布时间】:2016-10-26 19:34:18
【问题描述】:

当焦点进入 HTML 下拉列表时,我无法调用服务器端 LoadStudent() 方法。

服务器端编码

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>

【问题讨论】:

    标签: asp.net vb.net html


    【解决方案1】:

    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>
    

    【讨论】:

    • Brother 下拉列表将根据所选学生班级进行填充,因此必须在 onfocus 或 onblur 中写入,我无法在页面加载事件中写入
    • 当您的特定学生选择发生更改时,您能否设置一个事件来更新 DropDownList?假设您的学生在一个单独的列表中,只需设置一个事件以确定它何时更改并调用LoadStudent() 方法。
    猜你喜欢
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-23
    相关资源
    最近更新 更多