disable controls recursively

by pietman 13. October 2011 11:24

 

private void RecursDisableCtrls(Control ctr)
        {
                    if (ctr.Controls.Count > 0)
                        foreach (Control ee in ctr.Controls)
                        {
                            RecursDisableCtrls(ee);
                        }
                    {
                        if (ctr is WebControl)
                        {
                            if ((ctr as WebControl).ID == "ButtonCancel") //exclude this butt e.g.
                                return;
                            (ctr as WebControl).Enabled = false;
                        }
                    }
        }

 

Tags:

c#

Collection was modified; enumeration operation may not execute

by pietman 27. June 2011 11:26

I get an error as follows:
Collection was modified; enumeration operation may not execute

for the following code:

 

foreach (Comment comment in comments)
{
    if (comment.IP == IPtoBan)
    {
        p.RemoveComment(comment);
    }
}

 

you can fix this by replacing the first line with this:

foreach (Comment comment in new System.Collections.Generic.List<Comment>(comments))

Tags:

c# | Errors

URI manipulation

by pietman 6. June 2011 18:12

use a regex: const string LocalLinkRegex = @"href[\s]*=[\s]*[""'][.\/]?[\w\.?=%&=\-@/$,]+[""']";

//normally it's: const string WWWRegex = @"(?<Protocol>\w+):\/\/(?<Domain>[\w@][\w.:@]+)\/?[\w\.?=%&=\-@/$,]*";

            R = new Regex(LocalLinkRegex, RegexOptions.IgnoreCase);
            Matches = R.Matches(wpr.PageContent);

if e.g. :

 url = 'http://msdn.microsoft.com/en-us/magazine/cc300497.aspx' and

 sss = 'href="hh227291.aspx"'

then UUU.AbsoluteUri  will be 'http://msdn.microsoft.com/en-us/magazine/hh227291.aspx'

also works for sss = 'href="../hh227291.aspx"' or 'href="/hh227291.aspx"'



string sss = Match.ToString();
Uri UUU;
try
{
    if (url == "")
        UUU = new Uri(sss.ToLower());
    else
    {
        int offsetFirstQuote = sss.IndexOf("\"");
        if (offsetFirstQuote == -1)
            offsetFirstQuote = sss.IndexOf("\'");

        int offsetLastQuote = sss.LastIndexOf("\"");
        if (offsetLastQuote == -1)
            offsetLastQuote = sss.LastIndexOf("\'");

        UUU = new Uri(new Uri(url), sss.ToLower().Substring(offsetFirstQuote+1,offsetLastQuote-offsetFirstQuote-1));
    }
}
catch { continue; }

Tags:

c#

About ...

pietman celliersPietman Celliers
Bitlink  Ltd
bitlinkit.com