Regex inside of a tag

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
 
J

John Timney \(ASP.NET MVP\)

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


--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director
 
T

tshad

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
 
G

Guest

meaning

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

Sreejith
 
T

tshad

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
 
G

Guest

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

tshad

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top