替换Code Behind中的ASP Literal文本

时间:2015-10-09 18:12:47

标签: asp.net .net vb.net

我是ASP的新手,并希望获得一些关于如何在我的Code Behind中访问文字然后将其更改为传入参数文本的指导。

我有一个resources.ascx文件,显示人员列表(从数据库中提取)。这工作正常,它看起来像这样:

  

全名

     

:(888-888-8888)

     

F:(888-888-8888)

然而,问题是我现在希望它有条件地说"免费电话"而不是" F:"一页。

people.aspx 页面中,我传入"免费电话"到资源:

<%@ Register Src="~/UserControls/resources.ascx" TagName="Resources" TagPrefix="ucResources" %>    
<ucResources:Resources ID="Resources1" FaxNumberAlias="Toll Free" runat="server" />

resources.ascx 转发器将数据库中的所有人输出到页面。

<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
    <div class="sectioncontent">
        <b><%#Eval("EmployeeFirstName")%> <%#Eval("EmployeeLastName)%></b>
        T: <%#Eval("Phone")%>
        <br>
        <asp:Literal runat="server" ID="FaxNumberLabel">F:</asp:Literal> <%#Eval("Fax")%><br>
    </div>
    <br />
</ItemTemplate>

resources.ascx.vb 文件中,我想做类似的事情,但FaxNumberLabel(我在resources.ascx中声明的文字)是不可访问的或者没有被访问过声明。

Public Property FaxNumberAlias() As String
    Get
        Return _FaxNumberAlias
    End Get
    Set(ByVal value As String)
        _FaxNumberAlias = value
    End Set
End Property

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not String.IsNullOrEmpty(_FaxNumberAlias) Then
        FaxNumberLabel.Text = _FaxNumberAlias
    End If
    PopulateRepeater()
End Sub

我错过了将文字连接到后面的代码?

2 个答案:

答案 0 :(得分:1)

问题是Literal位于Repeater内,所以你可能会有很多。{1}}。最好的方法是在转发器的OnDataItemBound事件中访问它们:

Protected Sub Repeater1_OnDataItemBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles Repeater1.OnDataItemBound
    If (e.Item.ItemType = ListItemType.Item) Or _
        (e.Item.ItemType = ListItemType.AlternatingItem) Then

        Dim litFaxNumberLabel As Literal = e.Item.FindControl("FaxNumberLabel")

        litFaxNumberLabel.Text = _FaxNumberAlias

    End If

End Sub

注意:请原谅任何错误的语法,自从我触及VB以来已经超过4年了!

答案 1 :(得分:0)

您可以像这样使用 -

        protected void DataDisplay_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            LinkButton lit = (LinkButton)e.Item.FindControl("LinkButton2");
            if (lit.Text == "0")
            {
                int a = Convert.ToInt32(lit.Text);
                if (a == 0)
                {
                    if (a == 0)
                    {
                        lit.Text = "Your Text";
                    }
                }
            }
        }
    }
相关问题