1

I'm new to ASP and was hoping to get some guidance on how to make my literal accessible in my Code Behind and then change it to the text of a passed in parameter.

I have an resources.ascx file that displays a list of people (pulled from a database). That is working fine and it looks something like this:

Full name

T: (888-888-8888)

F: (888-888-8888)

The problem, however, is that I now want it conditionally say "Toll Free" instead of "F:" for one page.

In the people.aspx page, I'm passing in "Toll Free" to the resource:

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

resources.ascx The repeater outputs all the people from the database to the page.

<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> 

In the resources.ascx.vb file, I wanna do something like this but FaxNumberLabel (the literal I declared in resources.ascx) isn't accessible or hasn't been declared.

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 

What am I missing that connects the literal to the code behind?

3 Answers 3

1

The problem is that the Literal is inside a Repeater so you may potentially have lots of them. The best way is to access them inside the OnDataItemBound event of your repeaters:

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 

Note: Excuse any bad syntax, it's been over 4 years since I touched VB!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I found somewhat of an answer with more searching but didn't completely solve it until I got your answer. I was missing the Handles Repeater1.ItemDataBound part and I was having trouble figuring out where RepeaterItemEventArgs comes from.
0

You can simply use like this -

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

Comments

0

In my case, I am using a button (btnRedirect) in each row.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.