Thursday, March 22, 2012

Looping over my WebControls

Hi everyone,

I'm struggling with something....I'd like to loop over all of my page's
HyperLink controls, and I'm not sure how to do it.

Here's what I'm looking for, in pseudocode:

foreach (System.Web.UI.WebControls.HyperLink hLink in this.Controls)
{

hLink.Target= "fill in value here using custom algorithm";

}

Anyone know of a good way to do this? Thanks very much.try yhis....
For Each cntrl As Control In e.Item.Cells(i).Controls
If TypeOf cntrl Is System.Web.UI.WebControls.HyperLink
Then
CType(cntrl, HyperLink).Target= "yahoo.com"
End If
Next

--
http://pathidotnet.blogspot.com
=====
vInAypAtHi
o__
--_,>/'_--
(_) \(_)
-----

"Jim Bancroft" wrote:

> Hi everyone,
> I'm struggling with something....I'd like to loop over all of my page's
> HyperLink controls, and I'm not sure how to do it.
> Here's what I'm looking for, in pseudocode:
> foreach (System.Web.UI.WebControls.HyperLink hLink in this.Controls)
> {
> hLink.Target= "fill in value here using custom algorithm";
> }
> Anyone know of a good way to do this? Thanks very much.
>
>
plese change this
e.Item.Cells(i).Controls

--
http://pathidotnet.blogspot.com
=====
vInAypAtHi
o__
--_,>/'_--
(_) \(_)
-----

"vinay" wrote:

> try yhis....
> For Each cntrl As Control In e.Item.Cells(i).Controls
> If TypeOf cntrl Is System.Web.UI.WebControls.HyperLink
> Then
> CType(cntrl, HyperLink).Target= "yahoo.com"
> End If
> Next
>
> --
> http://pathidotnet.blogspot.com
> =====
> vInAypAtHi
> o__
> --_,>/'_--
> (_) \(_)
> -----
>
> "Jim Bancroft" wrote:
> > Hi everyone,
> > I'm struggling with something....I'd like to loop over all of my page's
> > HyperLink controls, and I'm not sure how to do it.
> > Here's what I'm looking for, in pseudocode:
> > foreach (System.Web.UI.WebControls.HyperLink hLink in this.Controls)
> > {
> > hLink.Target= "fill in value here using custom algorithm";
> > }
> > Anyone know of a good way to do this? Thanks very much.
If you are looking to parse all controls on the page then you probably need
to use a recursive function that would scan the child controls of each
control.

I have a sample recursive function on my website that does this;
http://www.societopia.net/samples/Findcontrols.aspx

--

"Jim Bancroft" wrote:

> Hi everyone,
> I'm struggling with something....I'd like to loop over all of my page's
> HyperLink controls, and I'm not sure how to do it.
> Here's what I'm looking for, in pseudocode:
> foreach (System.Web.UI.WebControls.HyperLink hLink in this.Controls)
> {
> hLink.Target= "fill in value here using custom algorithm";
> }
> Anyone know of a good way to do this? Thanks very much.
>
>
Yes, the recursive call is the way to go, except that you might want to code
the type checking as

else if ( cntrl is System.Web.UI.Controls.HyperLink )
instead of
else if ( cntrl.GetType().FullName.IndexOf ("HyperLink") !=-1 )

--
- Shuvro
SDE, MSFT

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.

"societopia.net" <societopianet@.discussions.microsoft.com> wrote in message
news:E30BBEDE-EFED-413F-BEF2-DE3BF157468B@.microsoft.com...
> If you are looking to parse all controls on the page then you probably
> need
> to use a recursive function that would scan the child controls of each
> control.
> I have a sample recursive function on my website that does this;
> http://www.societopia.net/samples/Findcontrols.aspx
> --
>
> "Jim Bancroft" wrote:
>>
>> Hi everyone,
>>
>> I'm struggling with something....I'd like to loop over all of my page's
>> HyperLink controls, and I'm not sure how to do it.
>>
>> Here's what I'm looking for, in pseudocode:
>>
>> foreach (System.Web.UI.WebControls.HyperLink hLink in this.Controls)
>> {
>>
>> hLink.Target= "fill in value here using custom algorithm";
>>
>> }
>>
>> Anyone know of a good way to do this? Thanks very much.
>>
>>
>>
>
Thanks guys...I'll take these suggestions and run with them.

-Jim
I believe you meant: System.Web.UI.WebControls.HyperLink (instead of
System.Web.UI.Controls.HyperLink). Thanks for the suggestion. I modified
the code sample accordingly.

--

"Shuvro Mazumder [MSFT]" wrote:

> Yes, the recursive call is the way to go, except that you might want to code
> the type checking as
> else if ( cntrl is System.Web.UI.Controls.HyperLink )
> instead of
> else if ( cntrl.GetType().FullName.IndexOf ("HyperLink") !=-1 )
> --
> - Shuvro
> SDE, MSFT
> This posting is provided "AS IS" with no warranties, and confers no rights.
> Use of included script samples are subject to the terms specified at
> http://www.microsoft.com/info/cpyright.htm.
>
> "societopia.net" <societopianet@.discussions.microsoft.com> wrote in message
> news:E30BBEDE-EFED-413F-BEF2-DE3BF157468B@.microsoft.com...
> > If you are looking to parse all controls on the page then you probably
> > need
> > to use a recursive function that would scan the child controls of each
> > control.
> > I have a sample recursive function on my website that does this;
> > http://www.societopia.net/samples/Findcontrols.aspx
> > --
> > "Jim Bancroft" wrote:
> >>
> >> Hi everyone,
> >>
> >> I'm struggling with something....I'd like to loop over all of my page's
> >> HyperLink controls, and I'm not sure how to do it.
> >>
> >> Here's what I'm looking for, in pseudocode:
> >>
> >> foreach (System.Web.UI.WebControls.HyperLink hLink in this.Controls)
> >> {
> >>
> >> hLink.Target= "fill in value here using custom algorithm";
> >>
> >> }
> >>
> >> Anyone know of a good way to do this? Thanks very much.
> >>
> >>
> >>
> >>
>
Yes, I meant System.Web.UI.WebControls.HyperLink. Note that this will not
work if the anchor object <a> is used with (runat=server). In that case,
you'll want to change your if loop to include a type checking for this
object.

If the object is not server side, I believe I saw a client side javascript
example that does the same recursively.

--
- Shuvro
SDE, MSFT

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.

"societopia.net" <societopianet@.discussions.microsoft.com> wrote in message
news:A8FA08C6-6F61-4378-BBB4-B978C199885E@.microsoft.com...
>I believe you meant: System.Web.UI.WebControls.HyperLink (instead of
> System.Web.UI.Controls.HyperLink). Thanks for the suggestion. I modified
> the code sample accordingly.
> --
> "Shuvro Mazumder [MSFT]" wrote:
>> Yes, the recursive call is the way to go, except that you might want to
>> code
>> the type checking as
>>
>> else if ( cntrl is System.Web.UI.Controls.HyperLink )
>> instead of
>> else if ( cntrl.GetType().FullName.IndexOf ("HyperLink") !=-1 )
>>
>> --
>> - Shuvro
>> SDE, MSFT
>>
>> This posting is provided "AS IS" with no warranties, and confers no
>> rights.
>> Use of included script samples are subject to the terms specified at
>> http://www.microsoft.com/info/cpyright.htm.
>>
>>
>>
>> "societopia.net" <societopianet@.discussions.microsoft.com> wrote in
>> message
>> news:E30BBEDE-EFED-413F-BEF2-DE3BF157468B@.microsoft.com...
>>> > If you are looking to parse all controls on the page then you probably
>> > need
>> > to use a recursive function that would scan the child controls of each
>> > control.
>>> > I have a sample recursive function on my website that does this;
>> > http://www.societopia.net/samples/Findcontrols.aspx
>>> > --
>>>> > "Jim Bancroft" wrote:
>>> >>
>> >> Hi everyone,
>> >>
>> >> I'm struggling with something....I'd like to loop over all of my
>> >> page's
>> >> HyperLink controls, and I'm not sure how to do it.
>> >>
>> >> Here's what I'm looking for, in pseudocode:
>> >>
>> >> foreach (System.Web.UI.WebControls.HyperLink hLink in this.Controls)
>> >> {
>> >>
>> >> hLink.Target= "fill in value here using custom algorithm";
>> >>
>> >> }
>> >>
>> >> Anyone know of a good way to do this? Thanks very much.
>> >>
>> >>
>> >>
>> >>
>>
>>
>

0 comments:

Post a Comment