我有一个返回字符串类型值的函数,我需要调用该函数并在下面的文本字段中显示值
函数是:
Public Function getemailaddress() As String
标签为:
<asp:Label Text='<% HERE I NEED TO CALL THE FUNCTION AND SHOW RETURNED
VALUE %>' runat="server" ID="EmailAddress2" />
该标签放置在数据列表中,但我无法访问其后面的代码。
答案 0 :(得分:1)
根据您当前的情况,我假设您在方法和标签更改代码位于.aspx文件中的情况下,通过后面的代码将数据填充到DataList中,因此我编写了以下代码
Default.aspx
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="ms_temp._Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="mainForm" runat="server">
<asp:DataList ID="lv" runat="server" OnItemDataBound="lv_ItemDataBound">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" />
</ItemTemplate>
</asp:DataList>
</form>
</body>
</html>
<script runat="server">
Function GetEmailAddress(name As String) As String
Return name & "@gmail.com"
End Function
Protected Sub lv_ItemDataBound(sender As Object, e As DataListItemEventArgs)
Dim lbl As Label = e.Item.FindControl("lblName")
Dim name As String = e.Item.DataItem
lbl.Text = name & ": " & GetEmailAddress(name)
End Sub
</script>
Default.aspx.vb
Imports System.Collections.Generic
Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim list = New List(Of String) From {"John", "Allen", "William"}
lv.DataSource = list
lv.DataBind()
End Sub
End Class
输出
John: John@gmail.com
Allen: Allen@gmail.com
William: William@gmail.com
DataList“ lv”在页面加载时被填充了一些随机名称。
我已经创建了“ GetEmailAddress”方法,您想在该方法上调用该方法来更改DataList的标签文本。在“ lv_ItemDataBound”事件上调用此方法并更改label的值。
希望有帮助