Regex inside of a tag

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

Is there a way to use Regex inside of a tag, such as asp:label?

I tried something like this but can't make it work:

<asp:label id="Phone" text=Regex.Replace('<%# Container.DataItem("Phone")
%>',"(\d{3})(\d{3})(\d{4})","($1) $2-$3") runat="server"/>

I have this inside my Repeater and want it to filter the field during bind.
I can do it afterwards by just looping through the repeater items, but that
is extra work and time.

Thanks,

Tom
 
try wrapping it around the Container.DataItem("Phone") section, rather than
the text section


--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director
 
John Timney (ASP.NET MVP) said:
try wrapping it around the Container.DataItem("Phone") section, rather
than the text section

I thought that was what I was doing? Did I miss something?

Tom
 
meaning

<asp:label id="Phone" text='<%# Regex.Replace(Container.DataItem("Phone")
,"(\d{3})(\d{3})(\d{4})","($1) $2-$3") %>' runat="server"/>

Sreejith
 
Sreejith Ram said:
meaning

<asp:label id="Phone" text='<%# Regex.Replace(Container.DataItem("Phone")
,"(\d{3})(\d{3})(\d{4})","($1) $2-$3") %>' runat="server"/>

That worked great.

Here is what I was doing it. I first get the data in my Repeater:

<asp:label ID="Wages" text='<%#
Regex.Replace(Container.DataItem("Wages"),"\-|\,",""),"(\d{3})(\d{3})(\d{4})
","($1) $2-$3" %>' runat="server"/>


Then I go through the RepeaterItem list and filter the phone numbers.

for each oItem as RepeaterItem in ExperienceRepeater.items
oLabel = Ctype(oItem.FindControl("Phone"),Label)
oLabel.Text = Regex.Replace(Regex.Replace(oLabel.Text,"\-|\
",""),"(\d{3})(\d{3})(\d{4})","($1) $2-$3")
next

If I replace the Regex line into the Repeater, as you show:

<asp:label ID="Wages" text='<%#
Regex.Replace(Regex.Replace(Container.DataItem("Phone"),"\-|\
",""),"(\d{3})(\d{3})(\d{4})","($1) $2-$3" %>' runat="server"/>

It works fine.

What about a BoundColumn?

I tried changing my Bound Column:

<asp:BoundColumn DataField=ContactPhone
HeaderText="Phone"
ReadOnly="true"
Visible="true"
ItemStyle-VerticalAlign="Top"
SortExpression="ContactPhone">

and replaced it with:

<asp:BoundColumn DataField='Regex.Replace(Regex.Replace(ContactPhone,"\-|\
",""),"(\d{3})(\d{3})(\d{4})","($1) $2-$3")'
HeaderText="Phone"
ReadOnly="true"
Visible="true"
ItemStyle-VerticalAlign="Top"
SortExpression="ContactPhone">

but got the error:

A field or property with the name
'Regex.Replace(Regex.Replace(ContactPhone,"\-|\
",""),"(\d{3})(\d{3})(\d{4})","($1) $2-$3")' was not found on the selected
datasource.

Do I have to use a TemplateColumn and use an asp:Label?

Thanks,

Tom
 
<asp:BoundColumn DataField='Regex.Replace(Regex.Replace(ContactPhone,"\-|\
",""),"(\d{3})(\d{3})(\d{4})","($1) $2-$3")'
HeaderText="Phone"
ReadOnly="true"
Visible="true"
ItemStyle-VerticalAlign="Top"
SortExpression="ContactPhone">

Above wouldnt work.. wrong usage..
I believe, you will be able to create a TemplateColumn as below

<asp:TemplateColumn>

<ItemTemplate>
<asp:label id="Phone" text='<%#
Regex.Replace(Container.DataItem("Phone")
,"(\d{3})(\d{3})(\d{4})","($1) $2-$3") %>' runat="server"/>
</ItemTemplate>

<asp:TemplateColumn>


Sreejith
 
Sreejith Ram said:
<asp:BoundColumn DataField='Regex.Replace(Regex.Replace(ContactPhone,"\-|\

Above wouldnt work.. wrong usage..
I believe, you will be able to create a TemplateColumn as below

You're right.

That seems to be the case. I found that since I have been leaning more
towards formatting on the client side, that I making more use of the
templateColumn and less of the BoundColumn.

Thanks,

Tom
 
Back
Top