【问题标题】:Replace text of ASP Literal in Code Behind替换代码后面的 ASP Literal 文本
【发布时间】:2015-10-09 18:12:47
【问题描述】:

我是 ASP 新手,希望获得一些指导,了解如何在我的代码隐藏中使我的文字可访问,然后将其更改为传入参数的文本。

我有一个显示人员列表的 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 中声明的文字)不可访问或尚未声明。 p>

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

我遗漏了什么将文字与背后的代码联系起来?

【问题讨论】:

    标签: asp.net .net vb.net


    【解决方案1】:

    问题在于LiteralRepeater 内,因此您可能有很多。最好的方法是在中继器的 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 年多了!

    【讨论】:

    • 谢谢!我通过更多搜索找到了一些答案,但直到我得到你的答案才完全解决。我错过了Handles Repeater1.ItemDataBound 部分,我无法弄清楚RepeaterItemEventArgs 的来源。
    【解决方案2】:

    你可以像这样简单地使用 -

            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";
                        }
                    }
                }
            }
        }
    

    【讨论】:

      【解决方案3】:

      就我而言,我在每一行中都使用了一个按钮 (btnRedirect)。

             RepeaterItem item = btnRedirect.NamingContainer as RepeaterItem;
             var type = item.FindControl("IdUsed") as Literal;
             var literalValue = type.Text;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-11-30
        • 1970-01-01
        • 2017-11-05
        • 1970-01-01
        • 2012-05-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多